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.
warning: function returns address of local variable
Which function? Can you post the problem code? Does the function in question return a pointer? If so, you shouldn't return a pointer to a variable that's declared on the stack in that function. The reason is that when the function returns, these variables are removed from the stack and so that memory should no longer be used by your program.
Or better yet, pass a pointer to the variable to the function and declare it out of the function. Or, if it's a simple enough type, just make the function return that data type, and call it as value=func(...);
Arrays in C are passed and returned by address, not by value. So when you return returnValue, you are returning just a pointer to the 80 char array, not a copy of it.
That 80 char array goes out of scope when the function exits, so its memory may be reused by any later function that is called.
Because TorsionCheck has so much local storage, your program apparently works despite this serious bug. The compiler happened to allocate returnValue far enough down the stack and you then called only functions that use less stack, so the value did not get clobbered before you used it. Any of that could be affected by minor changes almost anywhere in your program. So this bug could suddenly make your program fail after some change that may not appear to relate in any way to the actual bug.
Once you understand why the code is wrong, then you should fix it. Depending on the rest of your program, there are several different approaches to fixing this bug. Some of them are:
1) Use malloc: Instead of
char returnValue[80];
use
char* returnValue = (char*)malloc(80);
If you do that, then freeing the allocated memory becomes the responsibility of the calling code.
If TorsionCheck is called only once per run of your program and/or the resulting char array is needed until nearly the end of the program, it is common and OK to just forget about freeing the memory, because when the program exits it makes no difference whether such allocations were internally freed.
But in other situations, it is important for the calling code to know that it "owns" the returned memory and must use the free() function to release it when it is no longer needed.
2) Declare the char[80] in the calling code and pass the address into TorsionCheck, rather than out of it.
3) Make the return value static: If TorsionCheck is called only once, or if the caller of TorsionCheck is always done using the result before it calls TorsionCheck again, you can change
char returnValue[80];
to
static char returnValue[80];
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by johnsfine
First, do you understand why the above is
If TorsionCheck is called only once per run of your program and/or the resulting char array is needed until nearly the end of the program, it is common and OK to just forget about freeing the memory, because when the program exits it makes no difference whether such allocations were internally freed.
WTF? that is called a MEMORY LEAK and is very bad programming practice and telling a newbie that its is ok is just an outright stupid thing to do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.