Segmentation Faults

Hello...I am developing a code that wil deal with the string manipulation functions on char arrays which have been declared as char *.

Now whenever I try to use these variables in functions like strcat().strcpy() ...etc,it gives me a segmentation fault error...

I got a way to work around this problem by using malloc() to allocate some memory beforehand.....it works even if a use a statement like

" x=(char*)malloc(0) "

Sometimes i get the correct output even without using malloc...what could be the reason for this erratic occurances of "segmentation fault" in my code...?

Segmentation faults occur when the program tries to access some memory that it isn't supposed to. Often happens when you have a loop that doesn't end when it is supposed to and goes on to try and access unavailable memory.

/*line 1 */
char *x;  <- this pointer aimed nowhere in particular;
/* line 2 */
char *y=NULL; <- this is better
/* line 3*/
x=malloc(10);  <- if malloc succeeds, x is now aimed somewhere reliable.
/* line * 9999 */
free(x);

When you call a function, the variables you declare at the top of a function (automatic variables) are allocated on the stack. They are plopped on there right over whatever was in memory there before, with no regard to what was there.

Line 1 - x can literally be "pointed" anywhere in memory, because of the fact that it was plopped into already used memory. If by some luck x is aimed at memory you own and can write, then it works -- BUT -- it's extremely likely to corrupt something else.
This is a bad thing - it's called a wild pointer.

Line 2 - y is just fine. It's set to an initial value of NULL, so it's not pointing randomly into memory.

Line 3 - x (assuming malloc does not fail, which it doesn't very often - you should learn to check how to find out if malloc failed) now pints somewhere that you can use.

Line 9999 - be sure the last thing you do is call free - in this case x - for any variable that was malloc-ed earlier.

Just a question, it it a good practice to assign NULL to any pointer that you free()? Or does it not really matter?

After you use free(), you should not dereference a pointer pointing to the area you just freed. The area that was freed is not returned to the OS, it remains available to be reused by the next malloc(). Some people will malloc and then free immediately in a function. Then they go ahead a use the area malloc'ed and figure that they don't need to free() at each return point. I feel this is poor technique.

So I believe that you should never dereference a pointer to an area that has been freed. But what purpose is served by setting the pointer to NULL? You should never dereference a NULL pointer either. I guess it might be argued that, like superfluous parentheses in complex expressions, it makes things a bit clearer to the reader. I rarely will set a pointer to NULL when I'm done with it, but I often initialize a pointer to NULL. I think it is mostly a matter of style.

But it is critical to realize that correct code will absolutely never dereference a NULL pointer.