ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1. The main problem was you didn't allocate space for your string before you tried to copy it.
char *q; // This allocates 32-bits on the stack for a pointer: nothing more
char *q = (char *)malloc (strlen (mystring) + 1); // This is actually what you wanted
2. A second problem is that the standard C library *already* provides an "strdup()"
function. If you wanted to write your own, it would probably be wise to call it
something else.
3. Here's the modified code (I also verified that the standard "strdup()" function
works fine):
Didn't you forget to free() the malloc()ed memory at the end of the program? I think that in such a short lived program, the OS will reclaim the memory, but in a long-lived program, that could be a source of problems.
No, you don't need to free the memory before you exit - the OS will do it for you. If Linux or Windows, not "think" - but *will*.
Yes, you definitely need to free the memory when you're done with it if the program will continue running. This is equally true of our "homegrown strdup" (where you clearly see the "malloc") as well as the standard library's version of "strdup" (which does the "malloc" behind your back, and which can be a source of memory leaks if you're not careful).
I'd strongly suggest that any time any code ever does a "free", it is ALWAYS good practice to ALSO set the pointer to NULL.
If you were coding in C++, it would be wise to use "new" and "delete" instead of "malloc()" and "free()", and it would also be wise to consider using a class (ideally, the standard C++ "string" class).
And if performance was an issue, I would consider whether or not the overhead of the default "malloc/free" and/or "new/delete" was acceptable, or whether I wanted to devise some strategy for re-using memory that I had already allocated.
All of which brings us very far afield from the original question.
when you create a function with malloc do you need to free the memory in the function or in the main. Also i thought that after a function exits all of the information that was contained in it is destoryed unless its static or passed back to main.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
when you create a function with malloc do you need to free the memory in the function or in the main.
When you allocate memory, you need to free it when its storage is no more needed, or before it can be used no more because reference (i.e. pointer) to it is lost. Not doing it is called a memory leak.
There is no rule about where (in the function, in the caller, in the main, wherever) the free must be done.
One important point is that the free must be done only once, thus the advice to set the pointer to NULL after freeing it.
Quote:
Also i thought that after a function exits all of the information that was contained in it is destoryed unless its static or passed back to main.
You are definitely confusing the stack and the heap here.
What is automatically freed when the program leave a function are its local variables, stored on the stack.
Malloc do not use the stack to get memory space, but the heap and that one is independant of function calls.
You are definitely confusing the stack and the heap here.
What is automatically freed when the program leave a function are its local variables, stored on the stack.
Malloc do not use the stack to get memory space, but the heap and that one is independant of function calls.
ohhh really wow me learned something new today i knew malloc created a pointer to
a memory location but i wasent sure if it obeyed same rules as varibles and scope.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.