In C the space you reserve for the dasdos and how you effectively use this data are two separate things.
Since sizeof(char) is always 1, you in fact, when calling realloc left only 3 bytes reserved for your sample text - as you expected.
Only the "printf" function does not know this - when searching for a string for printing, as the control code "%s" it will print the entire target string until it finds a character \x00
. If there is none before the end of the area allocated to astrign pdem happens one of two things: your program terminates with a segmentatin fault, or, pritnf consumes data from an area of an area where it may be being used for other things within your program. By awareness, the bytes you released with realloc remained within a segment used in your program and this data was not used by anything else - then your printf
worked.
But the big lesson to have in memte is: in C there is no automatic mechanism that will "stop" the access to memory and restrict it to areas you have allocated (either with malloc, or reserving the dimensions in variable declaration) - You are responsible for knowing that all functions called by your program are accessing memory areas allowed.
If you want to automatic verfification of vector sizes, and so on, you will only find this in something else level languages - Java, Go, Pythn, Ruby, etc... In C, and C++ is you and the CPU.
Note: by definition
sizeof (char)
is1
.– pmg