I have an struct variable which is passed like as follows:
//function definition
void function1(const Node* aVAR1)
{
Node* value=NULL;
.....
}
int main()
{
Node* aVAR=NULL;
aVAR=x.value;
function1(aVAR);
}
Here, when I run this in gdb, and step into function1()
, I see for variable aVAR
one temporary memory address is created.
GDB:
21 aVAR=x.value;
(gdb) p aVAR
$5 = (Node *) 0x654321
(gdb) n
Breakpoint 1, function1(aVAR1=0x7ffffffffebcdf ) at debug/../abc.c:12
12 {
(gdb) p aVAR1
$6 = (const Node *) 0x7ffffffffebcdf
For example,
- Initially, the address of aVAR is
0x654321
- Later for a short while until the first instruction in
function1()
is not executed,aVAR1
is kept in some temporary address like0x7ffffffffebcdf
. - After executing
Node* value=NULL;
which is first instruction in thefunction1()
, theaVar1
's address is0x654321
again. - but this temporary (
0x7ffffffffebcdf
) address is not cleaned up: even after the function exits,0x7ffffffffebcdf
is not cleared
I want 0x7ffffffffebcdf
to be cleared after function exits but that 0x7ffffffffebcdf
address does not have a pointer through which I can access this memory. Is there any option while linking in GCC through which I can prevent this?
If I add a malloc for aVAR and clear it later using memset and free, the problem gets resolved BUT logically when I see , I lose the reference to the memory block allocated by malloc() , and I won't be able to free() the allocated memory (causing memory leak ).