Click to See Complete Forum and Search --> : problem with free ( ) call


liadalex
05-20-2003, 04:11 AM
I have a segmentation fault after the free ( ) call.
Can someone help me to understand why?in following the code:

Spawn913
05-20-2003, 04:53 AM
First, you should always check the return value of malloc() calls. i.e.
str_ptr=malloc(...)
if (str_ptr==NULL) { /..../ }

anyway, I don't think the problem lies with the checking, it's just good practice.

With regards to your code, you malloc'd match as:

match=(char *)malloc(sizeof(char));

then you do a lot of manipulation on match, such as:
match=strstr(s1,": ");
match=match+11;

so, when you get to the line: free(match);, you'll be free'ing some other memory location (i.e. not the original location assigned by malloc), that's why you're getting a seg fault.

IMHO, you don't really need to allocate match in your code since you're just using it as a pointer and not as a storage variable. I guess removing the malloc and free lines for match would solve your problem.

liadalex
05-20-2003, 05:43 AM
Thanks Spawn913,
I have found that the problem is with "match pointer", in fact if you see the code attached you'll note the comment char for the free(match)...but i would understand two things:
1. iwhy the operation on the match pointer produces a seg fault when i try to free it;
2. why when i use pointer for storage i couldn't use malloc and free. In the other part of program if i don't allocate the pointer i have seg fault in this point...
liadalex

Spawn913
05-20-2003, 05:57 AM
hi again...

i'm not sure if you still have a question.. if you do, can you please re-phrase?

anyway, if you're asking why you have a seg fault when you don't allocate the pointer, I guess the line:
match=strstr(s1,": ");
may cause problems.

Make sure that match here actually points to something (i.e. the ": " was found in string s1). Since strstr() will return a NULL if the substring is not found.

if the line above returns NULL, match=match+11 would give you a seg fault.

this is just one possible source of a seg fault. Please post example codes of what you did if you still have questions.

bwkaz
05-20-2003, 06:50 PM
Originally posted by Spawn913
if the line above returns NULL, match=match+11 would give you a seg fault.

this is just one possible source of a seg fault. Please post example codes of what you did if you still have questions. Actually, that wouldn't. ;)

NULL is just 0. match=match+11 just assigns 11 to match. No segfault can be caused by that.

However, if you indirect through match (if you say match[0] or *match, or you call a routine that expects a string, with an illegal pointer), it will segfault then (even if match isn't strictly NULL anymore; even if it's 11).

You have to allocate memory the first time you put something into it (whether you use malloc, or you use an array, the same thing happens, more or less). If you have two pointers pointing at the same thing, though, yoiu shouldn't malloc anything for the second one.

For example, if you had a variable named "string" that was a pointer to character, that pointed at the start of a string that you created with malloc (and then put data into with strncpy or something), and you had another variable named ptr that you just run through each character in the string, you should not allocate anything for ptr, and you should also not free(ptr) (since you never allocated it). You just do ptr = string;, followed by your logic for each character, followed by ptr++; (to move it to the next character). When you're done, you can either leave it (make sure to free string, though), or say ptr = NULL; -- either way will work.

Spawn913
05-20-2003, 09:26 PM
you're right... my error about that match=match+11 line. :D


the line that would give you a segfault if match was NULL would be:
strncpy(s2,match,4);