0

I have this code written:

 Resident_x * infos_shorted; Residents=6; infos_shorted=(Resident_x *)malloc(Residents*(sizeof(Resident_x))); i=0; while ((infos_shorted+i)!=NULL){ printf ("%d\n", i); i++; } 

Although someone would expect that I have allocated 6 memory places, when I run it it keeps printing i 's until I terminate it manually.

Thats what I have done to find an answer to my main problem which is this:

I write :

 Resident_x * infos_shorted; Residents=6; infos_shorted[i].height[8]=7; infos_shorted=(Resident_x *)malloc(Residents*(sizeof(Resident_x))); for (i=0; i<=Residents+4; i++){ printf ("%d %d\n", infos_shorted[i].height, i); } 

And I get the infos_shorted[i].height[8] printed correctly. How is that;

2
  • 1
    Why are you expecting NULL in your allocation? malloc doesn't automatically terminate your memory with NULL so you'll likely loop forever. Commented Apr 22, 2013 at 19:40
  • 1
    @Eddy - It's worse than that: OP is expecting that incrementing a pointer beyond the allocation will make the pointer = NULL (rather than the contents) Commented Apr 22, 2013 at 19:45

2 Answers 2

1
while ((infos_shorted+i)!=NULL){ 

As soon as i > Residents, you have undefined behaviour. Nevertheless, infos_shorted + i will very probably evaluate to an address i*sizeof(Resident_x) bytes behind the value of infos_shorted. There is no reason to expect such an address to be a null pointer (until it wraps, but that is yet more undefined behaviour).

Sign up to request clarification or add additional context in comments.

9 Comments

If you read also the second code sample you will understand my problem. I allocate 6 memory places, but as it appears, I can use as much places as I want.
Is it really undefined even if you don't dereference it?
@Roddy Yes. It is undefined behaviour to just compute it. The standard doesn't restrict possible implementations much, and allows that weird things happen. De facto, on x86-descendants, it's harmless and the only real danger in it is overflow/wraparound.
@Roddy, yes pointer arithmetic is only defined as long as you stay within a valid object (or one element behind). The reason for that was probably segmented memory.
@Roddy No, &x[10] is okay, that's "one element past the end", that's explicitly allowed and guaranteed to work by the standard (computing the address, the dereference in &*(x + 10) is not evaluated due to the &).
|
0

you are expecting C runtime to behave how you would like it to behave; sadly it does not behave that way. malloc allocates you a chunk of memory, it is up to you to know where its start and end are

you must do

 const int NRES = 6; Resident_x * infos_shorted; Residents=NRES; infos_shorted=(Resident_x *)malloc(Residents*(sizeof(Resident_x))); i=0; for(int i = 0; i < NRES; i++){ ...info_shorted[i]; } 

5 Comments

Why should I do this? I wrote i<=Registers+4 just to point that the program has allocated more memory by itself.
The program has not allocated more memory. It's simply attempting to use memory that it hasn't allocated. The result of that attempt is undefined behavior; it could crash, or it could appear to behave as if the unallocated memory had actually be allocated, or any of a number of other arbitrarily good or bad things. The solution: Don't do that.
@Hobowpen - the program allocated what you asked for (6 residents); the fact that you can sometimes poke around elsewhere in memory is interesting but irrelevant. You end up with crashes and other unexplained behavior. Try running your program with electric fence (and maybe valgrind) they will catch all these errors for you
actually it depends whether you are trying to write your program or trying to understand the dynamics of machines, memory, compilers and runtimes. If the former - just trust us; if the latter then this margin is too small to enter the explanation - you need a text book or google time
@pm100 +1 for fermat reference ;-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.