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 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.
If it was q = p; then the story would be different. ´ – inestical Jul 7 at 10:52 OP can do whatever he wants with p, still he's freeing q which is uninitialized... – Gregory Pakosz Jul 7 at 10:52 Thanks all for your response #include #include int main() { char *p ,*q ; p =(char *)malloc(10) ; strcpy( p , "AB") ; *p = '\0' ; p++ ; q = p ; //*q = 32 ; free(q) ; return 0; #include #include int main() { int *p ,*q ; p =(int *)malloc(10) ; *p = 30 ; p++ ; q = p ; *q = 32 ; free(q) ; return 0; } please suggest,in case of integer if I assign p++ to q and free its ok but not correct in case of charcter Thanks – arpita Jul 7 at 11:50.
You never initialize that q pointer you're freeing, that's why your program segfaults: it tries to free a random uninitialized pointer.
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 ).
Thanks all for your response , well The belwo code throws bus error -: – arpita Jul 7 at 11:43.
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 */ ).
3 downvoters should be forced to leave a comment, especially when they downvote without reason (the answer is correct) – ShinTakezou Jul 7 at 11:43 +1 to counter (stupid) downvote – pmg Jul 7 at 12:22.
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.
Don't forget to accept an answer arpita. – niko Jul 7 at 18:11 Thanks... I got the concept.. arpita – arpita Jul 8 at 9:50 Arpita I want you to do something when you understand the concept or got the answer what you needed then accept an answer which made you more sense which gave you more clear concepts because people gets the reputation when they get points and they try to answer your question with more enthusiasm so please accept an answer when you got the point. I suggest you to go through the stackoverflow rules and regulations before you post a question.
Thank you and I am glad that you understood the concept. And remember these we are here to help you. – niko Jul 8 at 11:02 Once again if you have any doubts on 'c', just post them here.
I can help you. I have been working on c from my little age. I want to know whether I can clear someone's doubt in a clear way and make them understand in the way they want,So I am testing the concepts I have with me here on stackoveflow.
Thank you – niko Jul 8 at 11:09.
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. 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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.