开发者

Call to free() throwing segmentation fault

开发者 https://www.devze.com 2023-03-18 09:59 出处:网络
I have the below code which is throwing a segmentation fault. Please suggest what can be done to this.

I have the below code which is throwing a segmentation fault. Please suggest what can be done to this.

开发者_StackOverflow社区#include <stdio.h>

int main() {
    char *p ,*q ;

    p =(char *)malloc(20) ;
    *p = 30 ;
    p++ ;
    p=q ;

    free(q) ;

    return 0;
}

Thanks


You are freeing something not returned by malloc (q is never initialized). Also I can see you are doing

p++

In doing this, you are losing p and you can't free it anymore. Just in case you meant q=p, that's not valid either. You can only free what malloc returned.

EDIT

In light of comment it seems the OP does intend q = p. You can do this:

char *p;
char *save_p;

p = malloc(10); /* stop casting malloc */
save_p = p; /* save the original value of p BEFORE altering it */

/* use p to your heart's content */

free(save_p); /* it's legal to free this */

I see you are asking something about char versus integer. It's the same:

You can only free the exact values returned by malloc.


You never initialize that q pointer you're freeing, that's why your program segfaults: it tries to free a random uninitialized pointer.


You do not initialize q to the area you malloc-ed, and then you trash p (doing p = q), too. You need to save your pointer to malloc-ed area in order to be able to free it. So something like

p = q = malloc(...);
// play with p, or q, but not with both
free( /* the one you have not played with */ );


you are freeing q, that point to an undefined address. What you probably need is to write

q=p;

before incrementing p ( p++; )

so freing q will make sense with all your code ( preserving the original pointer for freeing it )


The reason I told you before but now let me tell you more clearly.Remember these arpita, the free() does not work with normal pointers , it only works with the pointers that are involved with malloc(),calloc() and realloc().The free() was developed in a way that it works only with dynamic memory allocation concepts.If you try to use it with a normal pointer it results segmentation fault or core dumped in linux based OS it is because the function is defined in that way because you are trying to free a memory pointed by q but q is uninitialized you don't know the address it is pointing to and you don't even know the size of memory to be deallocate.So their lies the error.I hope you understood. And one more thing people might be saying

int main()

{

int a=45;

int *q=&a;

free(q);

}

Some people may think and ask the above question saying that the q is initialized in the above code it is pointing to a and now why free() is not woking.Because i already told you the free() works only with dynamic memory allocation function. And in the above you are pointing to int a, but notice that int a, is a statically allocated variable. Hence still the error exists.

However when you move to some other language you will be having delete operator to delete it even if it not involved in dynamic allocation.People bring me a notification that the above code works . and i say yes with some compilers but that compilers are not for the real applications.I suggest you to work on gcc compiler,

0

精彩评论

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

关注公众号