0

I would like to understand what is going on here, more precisely why I do NOT receive a segmentation fault while writing on a memory location, which according to my understanding is not allocated.

Let's say I want to define a 2D array of int (testptr). One dimension (4) is allocated statically (as an "array"), the second dimension (2) dynamically (as a "pointer").

// First dimension 4 rows static
int *testptr[4];
for (i=0; i<4; i++)
    testptr[i] = calloc(2, sizeof(int));
// Second dimension 2 columns "dynamically" (in this example it is really just a constant)

Now I write to some locations:

testptr[0][0] = 5;
testptr[1][0] = 6;
testptr[2][1] = 7;
testptr[3][1] = 7;

All the above I expect to work fine, as they are within a 4x2 "array".

Now I write to a location which should not be allocated:

testptr[2][3] = 8;

And to make sure I write to many of them:

for (i=0; i<1000; i++)
    testptr[3][i] = i;

In none of these I get a segmentation fault nor other errors.

  1. Is it correct to say that we are writing on unallocated memory?
  2. Is it just luck that we are not receiving an error, as those unallocated memory locations are not reserved by other variables/processes?
  3. Can we assume that doing this will cause problems (segfaults) in other points of the programs?

Thanks for your answers.

m2oTech
  • 65
  • 7
  • 2
    Similar to [Is accessing a global array outside its bound undefined behavior?](http://stackoverflow.com/q/26426910/1708801) – Shafik Yaghmour Jan 08 '15 at 21:58
  • 1
    You're using calloc wrong, that should be `calloc(2, sizeof(int));` – tux3 Jan 08 '15 at 22:01
  • @tux3: maybe he wanted an array of 4 `short`s? Empirically, the result is the same; `calloc()` simply multiplies the two values to get an answer, and `2 * 4 == 4 * 2`. – Jonathan Leffler Jan 08 '15 at 22:07
  • @JonathanLeffler then please write `calloc(4, sizeof(short))`, you can't just assume that `sizeof(int)` will be 4. – tux3 Jan 08 '15 at 22:09
  • Did you try doing any more memory allocation, or memory release, after you finished your out of bounds trampling? That's a typical scenario for running into trouble. And the code that suffers is not necessarily close to the code that caused the trouble. But, as haccks said in his answer, you can't rely on undefined behaviour crashing your program. It can seem to work. It will continue to do so until it can cause maximum embarrassment before the most important people possible (and that isn't the community on SO; it is your CEO, CTO and CIO and your most important new customer). – Jonathan Leffler Jan 08 '15 at 22:17
  • I corrected the calloc statements. – m2oTech Jan 08 '15 at 23:12
  • Ok, thank you all for your answers. Which basically confirm my thoughts, i.e. that accessing unallocated memory puts you in a gamble, you may or may not have problems, if you do they are most likely very hard to debug. Please note I was not trying to do this in any program I write, was just wondering why there is no immediate reaction from the compiler or at runtime. The linked threads seem to answer this question too. Sorry for double posting, did not come up with using "out of bounds" keyword... – m2oTech Jan 08 '15 at 23:22

1 Answers1

3

Your program invokes undefined behavior. It will give either expected or unexpected results. There is no guarantee that accessing arrays out of bounds will produce a segmentation fault or it will gonna crash your hard disk!

haccks
  • 104,019
  • 25
  • 176
  • 264