Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
May 26, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

General Protection Fault


void main()
{  randomize();
        const int max_num=1000;
        int value[max_num]={0};
        const num_word_per_line=8;
        cout<<"The random values before sorting:  \n";  //assign the
random to
an array
        for (int i=0;i

This is a very simple, random numbers generation program. However, I get "General Protection Fault" every time I try to run it. When I change const int max_num=1000; to const int max_num=800;, it works. However, it wouldn't work when max_num is over 800. Can you tell me what causes this error and how can I fix this problem? I'm using Borland C++ 4.5, Windows 95, P200 MMX with 32MB RAM.

It would probably be helpful if you could have stepped through your program and determined which line causes the error. A general protection fault is actually enforced by the processor and reported by the operating system. All it really tells you is that one of the many rules set up by the operating system was violated.

So it's hard to get specific here, but I did notice that you are declaring your value array locally in your function. This means the data is allocated locally on the stack and it may be that you are simply running out of stack space. An easy fix would be to be to simply prepend the static keyword to your declaration.

static int value[max_num]={0};
This causes the variable to be allocated from your program's heap instead of the stack. If this doesn't fix the problem, then you'd need to dig a little deeper. Stepping through the program would probably be the next step.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date