Sign in to follow this  

free store.

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am reading a book on c++ I understand the uses of pointers and the free store work's, but what I would like to know is when you should allocate memory on the heap, or just puting it on the stack.Can anyone give me some examples? Would really appericate it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
stack allocations are generally for your local variables inside functions...
heap allocations are generally for large allocations that will remain for some period of time.
You have limited stack space, so allocating large arrays or the like at local function scope is a bad idea.

Share this post


Link to post
Share on other sites
If you haven't gotten to the chapter on c++ inheritance you can also use it for polymorphism:

class Vehicle
{
virtual void drive();
};

class Car : public Vehicle
{
void drive();
};

class Motorcycle : public Vehicle
{
void drive();
};

int main()
{
Vehicle* v = new Car;

// will call car's drive function
v->drive();
delete v;


v = new Motorcycle;

// now will call motorcycle's drive function
v->drive();
delete v;
}




This code isn't complete (need to implement the drive functions), but it hopefully demonstrates one possible use.

[Edited by - Will F on May 16, 2005 4:05:06 PM]

Share this post


Link to post
Share on other sites
As a general rule, stack-allocate things if you don't know why you want to heap-allocate them.

Reasons for heap allocation include:

- the object "lifetime" must exceed the scope in which it is created, and you're not able to handle that by returning the object.

- you require polymorphic behaviour (if you assigned a derived class to a stack-allocated base object (not pointer), it will get sliced in the same way as if you passed it in as a parameter without using a pointer or reference). The problem is that a derived class' object representation might be larger than that of its base, and when the compiled code is generated, there has to be knowledge of how much stack space to use - the compiler assumes space for just the base class, so anything else gets converted.

- You require some number of objects of the same type, but you won't know until runtime just how many. (HOWEVER, please consider std::vector etc. too.)

In modern C++, there honestly aren't that many reasons to heap-allocate things, because the C++ Standard Library provides useful classes that will wrap up the heap-allocation (and deallocation) work for you, and provide an object that lets you do a stack allocation and behave as if nothing is going on with the heap. For example, std::string produces an object on the heap which includes a raw char*; the char* points at a heap allocation (which is where the string data is located; this is needed because the size may vary). The object's methods handle all of the manipulation of the char* for you - including allocation, deallocation and resizing of the memory chunk on the heap - so you can basically forget about it. Please take advantage of this - it lets you treat C++ like a higher-level language, at a minimal overhead for what it does. (I don't like C++ overall, but I must admit that it does make some very good friendliness/efficiency tradeoffs in places...)

Share this post


Link to post
Share on other sites
The heap is not the free store. Eradicate this misconception from your mind immediatly. The heap is utilized using operators 'new' and 'delete'. The free store is utilized through the functions 'malloc' and 'free'. They are not compatible and there is no guarantee that they can even coexist on the same page in ram.

Share this post


Link to post
Share on other sites

This topic is 4594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this