1

I declared an integer array on heap like this

int* a = new int[100];

and then I initialized it like this

for(int i=0; i<100; i++)
    a[i] = i;

which worked perfectly but then I tried this

for(int i=0; i<1000; i++)
    a[i] = i;

which also worked perfectly.Since I am a newbie to c++ can anyone explain what's the problem in this above declaration because I think the declaration of integer array is not proper or might be anything else?

trincot
  • 317,000
  • 35
  • 244
  • 286
saurabh
  • 293
  • 2
  • 7
  • 19

6 Answers6

2

When you write past the end of an array you get undefined behaviour. The important word is undefined. It may depend on the compiler, the environment, the compiler flags, ...

In you example it worked because you wrote on a memory that is accessible, but you could (non limitative list) :

  • try to access unavailable memory and get a segmentation violation
  • try to write read only memory
  • ovewrite other variable belonging to your own code
  • erase your own stack

But as it is undefined behaviour nothing guarantees that this happens. You only know you must never do that.

With recent (decent OS) you cannot crash the system, but only your own application except if you run with admin or root privileges, in what case anything can happen. But with older systems like MS/DOS (or some embedded system) there are no inter application protection and system crash are likely to happen with such program.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Can it cause windows or linux crash? – user1436187 Dec 15 '14 at 07:25
  • when does a compiler shows segmentation fault behaviour – saurabh Dec 15 '14 at 07:29
  • @saurabh : with recent OS, you do not directly access real memory addresses, but the system maps *segments* (ie consecutive zones) of memory in the process address space. You get a segmentation fault (at runtime the compiler is no longer involved) when you try to access a non mapped address. – Serge Ballesta Dec 15 '14 at 07:33
  • could you please elaborate a non mapped address – saurabh Dec 15 '14 at 07:33
1

You have created an array of 100 ints on heap. But you are accessing memory much beyond that. C++ doesn't stop you from doing that, but it's an undefined behavior i.e you could get a crash, corrupt memory used by some other program etc.

ravi
  • 10,994
  • 1
  • 18
  • 36
0

You declared the variable with 100. But you are assigning the value up to 1000.

int* a = new int[100];

make that as 1000.

Edit:

If you are accessing the memory that is not declared for that variable it will be segmentation fault. Some of the times only it will work correctly.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
0

you just assigning some memory even you dont have permission to that. it can work. but, I am %100 sure, the program itself will break at some point.

Adem
  • 9,402
  • 9
  • 43
  • 58
0

C and c++ do not check for that kind of overflow. You are expected to never let that happen. Needless to say, this is 1/2 the reason that C and C++ have a reputation for being unforgiving.

You are lucky the program did not crash out violently. But it will, and very soon with that much smashing of other memory.

0

Technically "a" is just a pointer (as you have declared it).

Then you have made it point to the start of some array (on the heap in this case).

Then you are incrementing the pointer and setting the value. The compiler will not have a problem with this at all since "a" is not an array, but a pointer to a bit of memory that the compiler is told is an integer. So you could have your loop go on as long as you want, but it will start to overwrite memory used for other purposes, probably end up with a seg fault somewhere along the way.... or some other strange behavior :o

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • but I imposed a restriction on it by saying **new int[100]** so does that mean nothin – saurabh Dec 15 '14 at 07:32
  • no, "a" is just a "int *". You make it point to the memory address allocated by "new int[100]". You can make "a" point to anywhere in accessible memory, in this case it starts pointing to the start of your array, but by the end you have moved it way beyond this array. This is perfectly legal code as far as the compiler is aware (you may have reasons to do this.... it does not know). – code_fodder Dec 15 '14 at 07:37