I have below code where I have commented when I get segmentation fault and when not.
Originally I got segmentation fault and then I could figure out that probably I cannot initialize my char pointer locations like "abcd". But I am not able to understand - WHY?
I thought testString = "abcd"; will put a at first memory address, b at second and so on ...
Segmentation fault occurs when trying to free memory, based on how I initialize memory location.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char* testString = malloc(sizeof(char) * 5);
printf("Size of char is: %d\n", sizeof(char));
printf("Size of int is: %d\n", sizeof(int));
for(int i = 0; i < 5; i++)
{
printf("Pointer addresses are: %p\n", testString + i);
}
char* tempPtr = testString + 2;
printf("My temp pointer address = %p\n", tempPtr);
// This gives me segmentation fault ....
testString = "abcd";
// This will not give me segmentation fault ....
//int count = 65;
//for(int i = 0; i < 5; i++)
//{
// testString[i] = count + i;
//}
printf("Printing character...\n");
for(int i = 0; i < 5; i++)
{
printf("Characters are: %c\n", testString[i]);
}
printf("Freeing memory...\n");
free(testString);
//printf("Access after freeing >>%c<<\n", tempPtr[0]);
//free(testString);
}
Based on @M.M. and @Jonathan's comment I understood that with testString = "abcd"; my testString will point to a memory location where string "abcd" was created and since I didn't malloc'ed it I cannot free it. Also, since my original pointer to heap memory (which I got using malloc) is gone, so it is waste of memory or memory lead.
So, does it means that when I use printf statement like printf("Printing character...\n");, this is also a memory leak? Then how do I avoid it? Looping and inserting into char* is certainly a bad idea.
testString = "abcd"means that the pointertestStringwill now point to the memory location containing"abcd". Then you try tofreethat location, causing segmentation fault. - M.MtestString = "abcd";throws away the pointer to the allocated memory (a memory leak). You needstrcpy()instead, perhaps. And since the string literal (pointer value) was not returned bymalloc()(orcalloc()orrealloc()), you can't free it. - Jonathan LefflertestStringcontains the only pointer to the allocated memory. When you assigntestString = "abcd", you assign a new, different pointer value totestString. You can no longer free the pointer that was returned bymalloc()because you no longer have a record of what it was. The string"abcd"exists in your program's memory space somewhere; it isn't a memory leak. It's the pointer overwrite that causes the leak. Theprintf("…")operation does not leak memory. - Jonathan Leffler"abcd"will exist somewhere in memory. It is quite likely to be in the text segment since string literals are read-only, but the standard doesn't mandate anything about segments. It won't be in the heap area managed bymalloc(); it probably won't be on the stack, either. It will most likely either be in the data segment or in the text segment — these days, in the text segment, though historically, it would have been in the data segment. - Jonathan Leffler