I wanted to test my knowledge, and wrote a small program, which looks like this:
#include <iostream>
#include <cstdlib>
int main()
{
int* mem1 = (int*) malloc(2 * sizeof(int));
int* mem2 = (int*) malloc(5 * sizeof(int));
std::cout << "mem1: " << sizeof(mem1) << std::endl;
std::cout << "mem2: " << sizeof(mem2) << std::endl;
return 0;
}
I get the output:
mem1: 8
mem2: 8
When I change the values, that malloc(2 * sizeof(int))
to malloc(3 * sizeof(int))
, the output also doesn't change.
This is the Tutorial I used, so I am not entirely sure, if this is memory safe, with the conversion to int*
when calling malloc
.
I have also found this question, but I don't find it to be very helpful in my case.
Clang++ is the Compiler I used, but I don't think it makes any difference.
My best guess is, that it allocates the same memory, because it doesn't know where the memory ends.