Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why get general protection fault.

Status
Not open for further replies.

IHateTron

Programmer
May 15, 2002
8
0
0
GB
I just coded a program in C that compiles fine, but when I go to run it I get this:

General Protection Exception

Anybody have any idea why. Thx in advance.
 
Possibly writing into memory that you dont have

i.e.

char buffer[12];
buffer[12] = 'A';

Matt
 
Ok, this I didn't know:

I have this in my code:
#define MAXSTU 100
I then have this array:
char names_and_courses[MAXSTU][2][30];

So I was having the problem outlined above, couldn't find anywhere where I was trying to access elements past the end of my array anywhere like Matt suggested. Then I change MAXSTU to 50 and hey presto, problem solved. But why is this, perhaps somebody coukd let me know, thx.
 
Do you have a loop like
int i;
for (i = 0; i <= MAXSTU; i++)?

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Yes, why?
Can this loop not accomodate values of a certain size?

 
Because when declaring an array like
array[50], its indexes are in the range
0..49.
array[50] = something could cause a GPF. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
But I still dont see a problem. My array was initialised correctly.
So;
array[50] (indexes are 0..49)

and;
array[100] (indexes should be 0..99)
(No problem here, I understand this.)

And my loop should work with both array's regardless of their size. So why did I get a problem? I never have tried to write/use memory I didn't have. If that was the problem then surely the fact that it was changed from MAXSTU = 100 to MAXSTU = 50 but not changing the loop should make no difference and I would still be getting a general protection exception. But simply changing the value of MAXSTU from 100 to 50 solved it. Why?????????
Thx 4 your help BTW. Appreciated. :)
 
Clearly, your program is doing something illegal, but we can only speculate on what it's doing in the absence of the source code.

Try posting a *small* compilable program that exhibits the problem.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top