469,927 Members | 1,852 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

heap vs stack memory

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

Jul 23 '05 #1
4 4187
Gurikar wrote:
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
Jul 23 '05 #2
"Gurikar" <ms*******@gmail.com> wrote in news:1116506360.370454.25030
@f14g2000cwb.googlegroups.com:
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
Jul 23 '05 #3
Gurikar wrote:
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.

Jul 23 '05 #4

Thanks for all your answers..

Rolf Magnus wrote:
Gurikar wrote:
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.


Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Kevin Grigorenko | last post: by
17 posts views Thread by Jonas Rundberg | last post: by
22 posts views Thread by bitshadow | last post: by
9 posts views Thread by shine | last post: by
24 posts views Thread by arcticool | last post: by
16 posts views Thread by sarathy | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
9 posts views Thread by Roman Mashak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.