can you please give me an article address which tells when to use stack and when to use heap, how to use them properly and the performance issues related to usage.
Thanks

Recommended Answers

All 4 Replies

I believe you can google an article out there, there are many.

Very brief info about them.
When you create an object in the stack, the object will be deleted at end of the scope. When you create an object in the heap, it is in memory and will be only deleted when you call the delete function.

i would prefer narue's detailed explanations..

>can you please give me an article address which tells when to use stack and when to use heap
Oh, if only it were as simple as "use the stack here and use the heap here". :icon_rolleyes:. The problem is that often it's not easy to decide. Generally, a good guideline is to use the stack when you can and the heap when you must, because doling out heap memory is a relatively expensive task.

Examples of when you have no choice are large objects that won't fit on the stack, dynamic data structures whose size you can't predict, and when the scope of your object doesn't mesh with how the stack works. As with everything in programming, knowing when to use something comes down to experience trying it both ways.

>how to use them properly
As long as you remember to free your heap memory, there aren't really any special rules for using either stack or heap memory "properly".

>and the performance issues related to usage
The only performance issue is that requesting memory from the heap manager is vastly slower than the stack. Beyond that you pretty much only have copying issues to consider, but that has more to do with passing data around in with pointers vs. passing by value, which isn't really a stack vs. heap issue.

thanks narue, you not only prove us wrong but also prove why i love you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.