Link to home
Start Free TrialLog in
Avatar of Mindbender
Mindbender

asked on

free() behaviour questions

OK, I have a unix shell. I am using gcc compiler for cygwin and on a unix box @ uni.

I am wondering why, if I have the shell's control loop only exiting when 'exit' is typed in to the command line, a bunch of free()s that shouldn't have even been reached cause segmentation faults before the shell script has exited. The free()s relate to the input string and this is the problem, I beleive. The free()s are before a return of the regular int on exiting the main function.

Whatchya reckon?? Guidance appreciated.
Avatar of Mindbender
Mindbender

ASKER

And an extra 50 points to the person that can tell me how to get the login name into a C program - like getting logname into a string so I can print it.
SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh, thanks! Why isn't that referenced by the getdomainname man page I wonder?
Hi Mindbender,
> get the login name into a C program
You could also use the environment for that:
getenv("USER")

>Why isn't that referenced by the getdomainname man page I wonder?
Because everything's related ... somehow.

Cheers!

Stefan
Why does the free() cause segmentation faults before they're processed??

Code example

char* function() {
    char* stringA = (char*)malloc(MAX_STRING);
    char* stringB = (char*)malloc(MAX_STRING);
    char* stringC = (char*)malloc(2*MAX_STRING);

    stringA = "something";
    stringB = "somethingelse";
    stringC = strcat(stringA,stringB);

    free(stringA);
    free(stringB);

    return(stringC);
}

Why doesn't that work?
one problem with this ( which may or may not be related to yours segfault)


strcat doesnt concatenate two strings and return the result ,

its appends the second argument to the first ....


man strcat NAME
       strcat, strncat - concatenate two strings

SYNOPSIS
       #include <string.h>

       char *strcat(char *dest, const char *src);

       char *strncat(char *dest, const char *src, size_t n);

DESCRIPTION
       The  strcat()  function  appends the src string to the dest string overwriting the `\0' character at the end of dest, and
       then adds a terminating `\0' character.  The strings may not overlap, and the dest string must have enough space for  the
       result.

       The strncat() function is similar, except that only the first n characters of src are appended to dest.

RETURN VALUE
       The strcat() and strncat() functions return a pointer to the resulting string dest.



OK, that's true, but that is just because I was too lazy to properly write it out ;) Sorry. I was intending to purely demonstrate the way I had used free() calls. If the free calls are put after the return, it all works fine.

The issue here is that I am inexperienced with C....can I put return() anywhere in a function and have it still work? That would explain it well enough for me. I am used to putting return as the last statement in a function. There is no set text for this unit, so I went without.
ASKER CERTIFIED SOLUTION
Avatar of Narendra Kumar S S
Narendra Kumar S S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
anything after return in a fucntion is not executed ..
return , just returns the control to the function caller

>....can I put return() anywhere in a function and have it still work?
When the execution encounters "return" statement, it will return from the function at that point.
The subsequent statements will not executed!
That's the reason your code worked when you moved "return" to before "free"!
Actually the "free" statements nver got executed and hence it didn't crash!!

-ssnkumar
I guess I should try it, eh? The reason I am not is because I am in a Winblows lab right now using Java (no unix access unless I feel like SSH'ing nutmeg), but I will. If I answer my own question, what do I do? I want to give points to avizit, but it isn't the correct answer for the main question. Been a LONG time since I have been here (about year in a serious sense).

OK then, but why did the free() statements cause segmentation faults?
free() can only free what has been allocated by malloc etc ,
free cannot free just any pointers ..

so when you have some other value in stringA which has not be allocated by malloc/calloc etc you get segfault
Wow, I can't keep up with you guys. OK, thankyou, that explains my problems. Let me try it out and I will award points.

Look at my first comment:
>This address is not assigned by you using malloc. So, you cannot also free this address.
>That is the reason:
>    free(stringA);
>gives a segmentation fault!

Hope this helps you.....

-ssnkumar
Hi Mindbender,

   If you had read my first comment, it would have become clear for you.
   I have clearly explained what is happening with your code. Why it segfaults and also how to overcome it.
   I don't know if you checked it.

-ssnkumar
Christ, what a mess. To clarify my responses:

I will probably accept a split answer, 125 to ssnkumar, 50 to avizit.

I have been flicking between apps, and not paying a large amount of attention to this page. I completely missed ssnkumar's explanation, which I will probably accept as an answer.
Thanks for reading my comments also.
I didn't bargain for the points!
I felt bad that, you completely missed out my comments.

Happy to know that it helped you:-)

-ssnkumar
Thanks guys, points awarded!