devxlogo

General Protection Fault

General Protection Fault

Question:

#include #include void main(){  randomize();        const int max_num=1000;        int value[max_num]={0};        const num_word_per_line=8;        cout

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.

Answer:
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.
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist