Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
c++ - Warning 15: returning address of automatic 'identifier'
Warning 15: returning address of automatic 'identifier' Warning. This results in an invalid pointer beyond the end of the stack. When the function returns, the caller receives an illegal address that can cause a general protection fault. How is this caused and how is it fixed, I've never ran into this before, but it pops up now and again in my programs, ruining the functions I write. Here is a function in which it appears(There are a few non-local variables): LPSTR GetField(LPSTR id, int record) { int alphaix, betaix = 0, offset, count; char return_string[2000]; ZeroMemory(return_string, 2000); for (alphaix = 0;alphaix < num_headers;alphaix++) { if (strnicmp((header_id + (alphaix * 11)), id, strlen(id)) == 0) break; betaix += (header_length[alphaix]); } if (alphaix == num_headers) return "ERR1"; offset = (record_bytes * record) + header_bytes + betaix + 1; SetFilePointer(scanfile, offset, NULL, FILE_BEGIN); wfgets(return_string, header_length[alphaix] + 1, scanfile); // Causes warning 15 return return_string; } Sep 30 2002
Try returning strdup(return_string), that should take care of the problem. "DeadlyWarning" <DeadlyWarning_member pathlink.com> wrote in message news:ana6aa$1pue$1 digitaldaemon.com...Warning 15: returning address of automatic 'identifier' Warning. This results in an invalid pointer beyond the end of the stack. Sep 30 2002
Walter schrieb...Try returning strdup(return_string), that should take care of the problem. Sep 30 2002
"Heinz Saathoff" <hsaat bre.ipnet.de> wrote in message news:MPG.180365b21bde22f49896b1 news.digitalmars.com...Walter schrieb...Try returning strdup(return_string), that should take care of the Oct 01 2002
The problem with your code can be resumed to: char *buggy(void) { char str[1000]; return str; } The "str" array is created on stack when entering the function, and destroyed when exiting the function. The caller of the function receives a pointer to memory that no longer exists! In fact, it still exists, but it may contain anything by the time you try to read it. And, if you write there, it could be even worse (crash, probably). You could fix this in several ways: - return strdup(return_string), as Walter suggested, but make sure you call free() on the returned pointer after you don't need it anymore - otherwise your program will leak memory! - use a static array, which gets you out of trouble when you need large arrays. This has the disadvantage that your function becomes not reentrant (advantage would be that a buffer overflow cannot start executing random code - security might or might not be an issue for you) - only if you're programming in C++: use a std::string (DMC doesn't support namespaces yet, AFAIK, so try just with string), in which case the C++ standard library will handle memory management. This is by far the best solution IMHO. Regards, Laurentiu DeadlyWarning wrote:Warning 15: returning address of automatic 'identifier' Warning. This results in an invalid pointer beyond the end of the stack. When the function returns, the caller receives an illegal address that can cause a general protection fault. How is this caused and how is it fixed, I've never ran into this before, but it pops up now and again in my programs, ruining the functions I write. Here is a function in which it appears(There are a few non-local variables): LPSTR GetField(LPSTR id, int record) { int alphaix, betaix = 0, offset, count; char return_string[2000]; ZeroMemory(return_string, 2000); for (alphaix = 0;alphaix < num_headers;alphaix++) { if (strnicmp((header_id + (alphaix * 11)), id, strlen(id)) == 0) break; betaix += (header_length[alphaix]); } if (alphaix == num_headers) return "ERR1"; offset = (record_bytes * record) + header_bytes + betaix + 1; SetFilePointer(scanfile, offset, NULL, FILE_BEGIN); wfgets(return_string, header_length[alphaix] + 1, scanfile); // Causes warning 15 return return_string; } Sep 30 2002
I'm just expanding upon Walter's answer so that you will understand what the warning means and why you should revise code that induces it. In article <ana6aa$1pue$1 digitaldaemon.com>, DeadlyWarning (DeadlyWarning_member pathlink.com) says...Warning 15: returning address of automatic 'identifier' Sep 30 2002
|