Suppose I have a simple code something like that allocates a block of memory of a size of 4 byte and prints the address:
int32_t *ptr = malloc(4);
*ptr = 90; //for debugging
printf("%p\n", ptr);
//did not freed
Now when I execute this, (I executed it 6 times) I was confused to see an address get allocated twice:
$ ./t
0xb6cca008
$ ./t
0xb6cca008
$ ./t
0xb6d4a008
$ ./t
0xb6d8a008
$ ./t
0xb6d8a008
$ ./t
0xb6d0a008
$
And if try printing the value of the said address above, expecting to get 90, I instead get an annoying segmentation fault.
Code for accessing value of the address:
int32_t *ptr = (int32_t *) 0xb6cca008;
printf("%d\n", *ptr);
The remarkable thing here is the appearance of the address 0xb6cca008 and 0xb6d8a008 in a streak. I thought malloc don't use the same address?
And the problem might be in my side because I'm using termux, a linux terminal emulator inside android. I have never used a real PC with linux installed so I don't know if segmentation fault would appear or would print 90.
Since 4 byte is allocated on the heap and not freed after program terminates, shouldn't it exist and not give a segmentation fault? I also doubt that this is also an "Access violation".