Consider the following code example:
#include <iostream>
struct Foo
{
int x = 2;
int &rx = x;
};
int main()
{
Foo *f1 = new Foo[4];
std::cout<< f1[0].rx <<std::endl; //ok
Foo *f2 = (Foo*) malloc (4 * sizeof(Foo));
std::cout<< f2[0].rx <<std::endl; //memory leak
free(f2);
delete [] f1;
}
When new is used to allocate memory, the member reference rx is accessed normally and the correct value is printed. However, when malloc is used, accessing the member reference produces a segmentation fault. Can you please explain why?
I suspect a collision of C vs C++ implementation, but I'm not sure.
I ran the code with an address sanitizer which produced the following output:
ASAN:SIGSEGV
=================================================================
==32515==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000048cb69 sp 0x7ffea3d6bf40 bp 0x7ffea3d6bf70 T0)
#0 0x48cb68 in main /home/.../Desktop/test.cpp:15
#1 0x7f9861fb1544 in __libc_start_main (/lib64/libc.so.6+0x22544)
#2 0x405578 (/home/.../Desktop/a.out+0x405578)
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/.../Desktop/test.cpp:15 main
==32515==ABORTING