开发者

Memory Clobbering Error

开发者 https://www.devze.com 2023-03-17 16:33 出处:网络
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 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 run this code:

memory clobbered before allocated block

Can someone explain the reason why free(ptr) will throw me this err开发者_如何学Pythonor?

How else can I free the pointer?

Thanks.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#define LEN 5


int main(int argc, char *argv[]){

    char *ptr = NULL;

    ptr = (char *) malloc(LEN+1);// +1 for string
    strcpy(ptr, "hello");

    int i = 0;
    for(i = 0; i<LEN; i++)
    {
        printf("ptr[%d] = %c\n", i, ptr[i]);
        ptr++;
    }
    free(ptr);


    return 0;
}


You are incrementing ptr, therefore changing the address that it points to. You can't do that.

In your case, have a separate pointer, let's say char * p = ptr and do your operations with p leaving ptr intact so you can free(ptr) later.

EDIT Taking a second look at your code, I found that you are doing ptr++ when you shouldn't. You are accessing the characters in the array like ptr[i], if you mess with the ptr pointer, you are changing the base address and accessing the characters with ptr[i] can lead (and will lead) to unexpected results.

If you simply remove that line (ptr++) your code will magically work. If you want to explore the pointer concept and try another solution, your code could look something like this:

int main(int argc, char *argv[]){

    char *ptr = NULL;
    char * p; 

    ptr = (char *) malloc(LEN+1);// +1 for string (please check for NULL)
    p = ptr;

    strcpy(ptr, "hello");

    int i = 0;
    while (*p) // note how I changed it to a while loop, C strings are NULL terminated, so this will break once we get to the end of the string. What we gain is that this will work for ANY string size.
    {
        printf("ptr[%d] = %c\n", i++, *p); // here i dereference the pointer, accessing its individual char
        p++;
    }
    free(ptr);


    return 0;
}


Because ptr no longer points to the base of the memory you allocated.


Also, after you increment ptr, the expression ptr[i] does not return what you might expect; and that is why the output starts with "hlo".


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<LEN; i++)
    {
        printf("ptr[%d] = %c\n", i, temp[i]);
        temp++;                 // Why you are incrementing this? Just to print, there is no need of this.
    }
    free(ptr);


    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号