I was wondering about the following piece of code, its very simple but yet:
char* foo()
{
int i;
char buff[100];
snprintf(buff,100,"This is now written in the stack allocated memory!");
return buff;
}
Now, buff was allocated in the function's stack, meaning every variable is being freed, and what we have is a memory leak. But what exactly happens in the already written location?
The lines are still written in the memory, isn't it so?
Can i read that certain segment of memory? i do have the pointer pointing to the beginning of the segment, and if so will it be something like:
char* bar = foo(); char foobar = bar[0];
Can i write to that certain location? similar example:
char* bar = foo(); bar[1] = 'i';
- And in general, why is it considered to be a memory leak? can't we realloc this location again?
will appreciate clarifications regarding this issue!