Memory Clobbering Error

前端 未结 4 639
梦如初夏
梦如初夏 2020-12-19 12:26

I have a small piece of code. I compiled it with -lmcheck as I am trying to debug a code where I have the same similar error.

I get this error when I ru

4条回答
  •  旧巷少年郎
    2020-12-19 12:41

    Find the answer in comments. When you allocate some memory, typically, the memory management framework keep tracks of it by adding some more info (you can say Header and Footer) to the allocated memory area. When you free this memory, the same info is matched so as to detect any unwanted/invalid memory access.

    int main(int argc, char *argv[]){
    
        char *ptr = NULL;
        char* temp = NULL;           // Have a temp pointer.
    
        ptr = (char *) malloc(LEN+1);// +1 for string
        strcpy(ptr, "hello");
    
        temp = ptr;                 // manipulate temp pointer instead of ptr itself
    
        int i = 0;
        for(i = 0; i

提交回复
热议问题