Bus error with allocated memory on a heap

后端 未结 3 425
情话喂你
情话喂你 2021-01-28 04:45

I have Bus Error in such code:

char* mem_original;
int int_var = 987411;
mem_original = new char [250];
memcpy(&mem_original[250-sizeof(int)         


        
3条回答
  •  北海茫月
    2021-01-28 05:26

    I get the following warning when I compile your code:

    main.cpp:19:26: warning: cast from 'const unsigned char *' to 'const int *' increases required alignment from 1 to 4 [-Wcast-align]
        int original_var = *((const int*)location);
                             ^~~~~~~~~~~~~~~~~~~~
    

    This seems to be the cause of the bus error, because improperly aligned access can cause a bus error.

提交回复
热议问题