Why is the output of this code not 012? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
22nd May 2019, 2:57 AM
Sonic
Sonic - avatar
18 Answers
+ 10
jay after the increment, p and q definitely point to different elements. https://code.sololearn.com/c1ehusGBt0E4/?ref=app
22nd May 2019, 3:19 AM
Sonic
Sonic - avatar
+ 9
Idk about C and pointers, but my logic say: q is a pointer of p, when you free p, q falls out of value. I tried it by removing the line "free" and the result is 012
22nd May 2019, 3:03 AM
InvBoy [ :: FEDE :: ]
InvBoy [ :: FEDE :: ] - avatar
+ 8
Thanks InvBoy . I thought that only the fourth element would get freed and that if the entire block of 4 integers got freed, I would have got a segmentation fault/error. So no error but no output confuses me.
22nd May 2019, 3:10 AM
Sonic
Sonic - avatar
+ 8
So I guess it is the same number of bytes that were originally malloced that get freed, right? So free(p) will try to free 4 * sizeof(int) bytes but fail, giving rise to undefined behavior? I was expecting a segmentation fault/error rather than a seemingly clean 'No output'.
22nd May 2019, 3:34 AM
Sonic
Sonic - avatar
+ 6
Your program would work if it had one more level of indirection (an array of int * instead of int). Otherwise, you've thought about it correctly. Your 'p' pointer points to an allocated block of memory for an array of 4 integers. 'q' then points to 'p' which holds the address of the first element in the array. Then you increase the 'p' pointer offset by 3, so 'p' now points to the the 4th element in the array. Your call to free(p) attempts to free the 4th element (which is an integer) and you'll end up with memory corruption, since no memory was allocated for the integer itself. :-) It's akin to writing something like this: int num[4] = { 0, 1, 2, 3 }; int *p = num + 3; free(p); So the problem isn't that free(p) frees the entire array (it doesn't) as it is offset by 3. The problem is that you're attempting to free an integer rather than a pointer which holds memory for an integer. Here's a quick demo: https://code.sololearn.com/c194pe7TV4hq/#c
22nd May 2019, 3:15 PM
Komodo64
Komodo64 - avatar
+ 6
Sonic malloc() allocates memory dynamically and the returned address is stored in pointer p, that is storing to pointer q. But at the time of second loop for printing stored element pointer q goes to at that line of declaration of int *q=p ; but p is already free so there is no output generated.
23rd May 2019, 8:30 AM
Madhav
Madhav - avatar
+ 4
When you do p+=3; free(p); you have an undefined behavior and I guess in this case, your program just terminates
22nd May 2019, 3:12 AM
Paul
+ 4
I see now hahaha silly me, didn't take into account him going past the reserved memory area haha
22nd May 2019, 3:18 AM
jay
jay - avatar
+ 4
Same. But clearly here be dragons (for those unfamiliar with the saying: https://en.m.wikipedia.org/wiki/Here_be_dragons )
22nd May 2019, 3:36 AM
jay
jay - avatar
+ 3
InvBoy is correct. P and Q point to the same location. https://code.sololearn.com/cog35COSGDhq/?ref=app
22nd May 2019, 3:10 AM
jay
jay - avatar
+ 3
When you have an undefined behavior, the compiler can do pretty much whatever it wants.
22nd May 2019, 3:17 AM
Paul
+ 3
So to resize realloc should be called?
22nd May 2019, 3:23 AM
jay
jay - avatar
+ 3
Ran the program on codingground and you can see that you get a core dump. Meaning the free on p + 3 crashes your program
23rd May 2019, 12:01 PM
Paul
+ 2
Hello sonic, it's the free you're free in your storage before you print it
22nd May 2019, 4:41 PM
Rick Shiffman
Rick Shiffman - avatar
+ 2
The free of p+3 is wrong because you are trying to free memory which has not been initialized. It leads to undefined behavior. You will get a runtime error.
22nd May 2019, 7:41 PM
zwu01
zwu01 - avatar
+ 2
p+=3; I’m not sure, but free(p); needs a pointer free(*p);
24th May 2019, 3:54 AM
Ruben Enrique
Ruben Enrique - avatar
+ 1
Here are my thoughts. First this appears to be a runtime error, as there are no error messages. It must be the operating system booting the program. Second, I wanted to know if using free(q) would work since p was used in allocating the memory. This seems to work, as does free(p-3), which is the original value of p. It's interesting the output when you try to print the array after the free. The first and third values are very large integers that start with the same digit. The second and fourth are always zero. I dont have any explanation for this. https://code.sololearn.com/cd34TnEfgR7F/?ref=app
22nd May 2019, 8:01 PM
TomT
0
You must delete (free(p);) And the problem will be fixed
4th Aug 2019, 12:34 AM
Fathi Abdelmalek
Fathi Abdelmalek - avatar