heap vs stack memory

G

Gurikar

HI,


class A()
{
private:
int i;
char c;
public:
A();
};


void fun(A* a)
{

a = new A(); //allocated on heap(2nd time allocation)
}

int main()
{

A a; // allocated on stack

fun(&a);

}

Can you please tell wheather memory gets allocated 2 times for object
A(one on stack other on heap). Is this a valid code. Does both have
same adress(pointing to memory, even though both are on different
memory).. Atleast tell whats wrong in this code??

Regards
 
V

Victor Bazarov

Gurikar said:
class A()
{
private:
int i;
char c;
public:
A();
};


void fun(A* a)
{

a = new A(); //allocated on heap(2nd time allocation)
}

int main()
{

A a; // allocated on stack

fun(&a);

}

Can you please tell wheather memory gets allocated 2 times for object
A(one on stack other on heap). Is this a valid code. Does both have
same adress(pointing to memory, even though both are on different
memory).. Atleast tell whats wrong in this code??

The only thing that is "wrong" is that the memory allocated in 'fun'
is never freed.

If you change the pointer passed by value into a function to point to
something else inside that function, nothing happens to the object to
which that pointer pointed outside.

In the 'main' function you have 'a'. It has its address, &a. You take
that address an pass to 'fun'. Inside 'fun' there is a pointer to an A,
you called it 'a' to add more confusion to the issue. Fine. The 'a'
inside the 'fun' is initialised with the address of the 'a' in the 'main'
function. Immediately you override the value that came to 'fun' with
another value you obtain by dynamically allocating another A object.

The pointer 'a' inside 'fun' now has some new value, which is lost upon
the function exiting. The object outside 'fun' (the 'a' inside 'main')
never feels a thing.

V
 
M

Martin Gieseking

class A()
{
private:
int i;
char c;
public:
A();
};


void fun(A* a)
{

a = new A(); //allocated on heap(2nd time allocation)
}

int main()
{

A a; // allocated on stack

fun(&a);

}

Can you please tell wheather memory gets allocated 2 times for object
A(one on stack other on heap). Is this a valid code. Does both have
same adress(pointing to memory, even though both are on different
memory).. Atleast tell whats wrong in this code??


This is nearly compilable code (fix the syntax error in the class
declaration and implement the declared constructor). Nonetheless you get
a memory leak when leaving "fun" because the allocated memory is never
freed inside the function.
You have 2 different variables called "a" (let's refer to them as
"main::a" and "fun::a"). When executing the program you get the following
assignments:

fun::a = &main::a; // parameter passing
fun::a = new A(); // inside "fun"; previous assignment is overwritten

After returning from the function call fun::a has been removed but the
memory block where fun::a pointed to is still allocated.
main::a has not been changed and still contains the initial object.

Martin
 
R

Rolf Magnus

Gurikar said:
HI,


class A()
{
private:
int i;
char c;
public:
A();
};


void fun(A* a)
{

a = new A(); //allocated on heap(2nd time allocation)
}

int main()
{

A a; // allocated on stack

fun(&a);

}

Can you please tell wheather memory gets allocated 2 times for object
A(one on stack other on heap).
No.

Is this a valid code.
Yes.

Does both have same adress(pointing to memory, even though both are on
different memory)..
No.

Atleast tell whats wrong in this code??

Just remember that passing a parameter to a function means that its value
gets copied into the function parameter (unless you pass a reference).
In main, you take the address of a. Then you call fun and pass that address
to it. Fun has a parameter 'a' that this address gets copied into. So fun
now works on a local copy of the pointer value. Any changes to it will only
affect that copy within the function.
The next thing is that you assign to that parameter the return value from
'new A()', which is a pointer to a fresh object of class A. When returning
from the function, the parameter a is destroyed (but not the object it's
pointing to). Since 'a' within foo was just a local copy, nothing has
changed outside the function.
But now you have an object allocated with new, and you lost the only pointer
to it that you had when the function returned. You can never delete the
object anymore.
 
G

Gurikar

Thanks for all your answers..

Rolf said:
Just remember that passing a parameter to a function means that its value
gets copied into the function parameter (unless you pass a reference).
In main, you take the address of a. Then you call fun and pass that address
to it. Fun has a parameter 'a' that this address gets copied into. So fun
now works on a local copy of the pointer value. Any changes to it will only
affect that copy within the function.
The next thing is that you assign to that parameter the return value from
'new A()', which is a pointer to a fresh object of class A. When returning
from the function, the parameter a is destroyed (but not the object it's
pointing to). Since 'a' within foo was just a local copy, nothing has
changed outside the function.
But now you have an object allocated with new, and you lost the only pointer
to it that you had when the function returned. You can never delete the
object anymore.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top