Reputation: 81
I am using the following function to read from a file.
void read_file(){
char buf[BUF_SIZE];
// file is a vraiable defined and assigned during initialization
int numread = pread(file->fd, buf, BUF_SIZE, file->curpos);
// other logic follows
file->buffer += buf;
}
The following function still in the same class evaluates the contents of the buffer read from file.
void evaluate(){
read_file();
//evaluate the file->buffer contents
}
From my understanding the stack variables are automatically 'removed' when a function exits but I cant seem to comprehend why the buf variable in the read_file() function is not being cleared on consecutive call to evauluate() .
For instance if I do;
int main(){
evaluate(); // first call works as expected
evaluate(); // second call buf variable still has contents from previous call
return 0;
}
I would appreciate a hint to the right direction in solving this. Thanks in advance.
Upvotes: 0
Views: 107
Reputation: 150
First of all, there is design issue in your class.
Stack variables valid only in functions they declared.
Because stack pointers probably valid, they must not be used for checking.
Use allocated buffers (new
, malloc()
,HeapAlloc()
or other function every time you read
void evaluate(){
char *buf = new char[BUF_SIZE];
read_file(buf);
//evaluate the file->buffer contents
delete [] buf;
}
And
void read_file(char *buf){
int numread = pread(file->fd, buf, BUF_SIZE, file->curpos);
// other logic follows
file->buffer += buf;
}
This will help you with you question, but there will be next issue in file->buffer += buf;
. There should think about other way to copy arrays.
Upvotes: 0
Reputation: 24788
I cant seem to comprehend why the
buf
variable in theread_file()
function is not being cleared on consecutive call toevaluate()
.
Forcing always to clear the memory whether at allocation or release may incur a performance overhead. You are however free to do so when initializating or freeing (i.e.: before the control flows leaves the enclosing block where buf
is declared) the memory.
Take into account that N successive calls to evaluate()
like in the following code:
int main(){
evaluate(); // first call
evaluate(); // second call
...
evaluate(); // N call
return 0;
}
would imply to clear that memory N times (think of the overhead).
Note that even though all the calls to evaluate()
above will be using the same memory space on the stack, reading the memory prior to initialization results in undefined behavior. This way the standard imposes no requeriments on clearing data that has automatic storage duration.
Upvotes: 1
Reputation: 330
It is undefined behavior. C++ has no notion of a "stack", that is a platform-specific detail. Generally what happens when you free "stack space" is that the stack pointer (i.e, esp
. on x86, the stack grows downwards) is simply incremented. However, that doesn't mean the stuff on the stack automatically goes away. You cannot rely on undefined behavior because the standard says nothing about what will happen, so anything is a "valid" result.
Upvotes: 1
Reputation: 37317
For whatever it's worth, it's unnecessary to cleanup freed memory. Once it's freed and re-acquired, the state of its content turns into "indeterminate". In other words, it may contain data from last run, but it's absolutely unnecessary. You can't rely on this.
If you're confused, it's recommended that you zero-initialize the buffer:
char buf[SIZE] = {};
You can take a look at this question.
Upvotes: 1