This forum may not be the best to ask the question, but the answers on the other forums (that i know) were generally with respect to C for desktop pc (where memory management is different from that in embedded) and this forum has knowledgeable embedded people. hence...
I was worried about the following situation:
void function1() { ... //other variables char* ptr = myary; ... ptr = ReadNandFlash("myfile"); ... } char* ReadNandFlash(char* Filename) { FILE* file; char temp_ary[256]; file = fopen(Filename, "r"); if(file == NULL) { return NULL; } while(!feof(file)) { fread(temp_ary, sizeof(char), 256, file); } fclose(file); return ary; }
will the 'temp_ary' be destroyed as only the pointer value is returned back to the function1()?
Your code doesn't show where that myary buffer is.
And it has two assigns to the pointer but never seems to make use of the memory (myary) that the pointer points to.
Separate "destroy" from "release".
When memory gets released, the contents in the memory may not be destroyed. It might, but that is outside the specifications of the language.
When an auto variable goes out of scope, you just have no right to assume anything about the contents of that memory anymore.
When a memory block gets released with free(), you might still be able to read the same data from the pointer - but doing so is wrong because the documentation for free() doesn't promise anything about the pointer or that block of memory after the call.
Hi Andrew,
thank you for this clarification - I never ran across such a compiler.
Martin
myary is local to function1(). Sorry for that.
View all questions in Keil forum