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<<"The random values before sorting:  
";  //assign therandom toan 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.

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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may