472,331 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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 4259
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
9
by: gold | last post by:
Hello all, I want know abt wht kind of datastructures using both C & C++ internally. Some were said heap, others said tree anyone can explain...
22
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the...
9
by: shine | last post by:
what is the difference between a heap and a stack?
2
by: Ma Xiaoming | last post by:
Dear ladies and gentlemen, I don't understand what the difference between the Heap and the Stack is. Could you please explain the difference...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value...
16
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable...
9
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.