Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Dec 1, 1999

Memory Stack vs. Free Store

Question:
When using C++, is it better to use stack or free store memory for data objects of medium to large size? I have multiplatform concerns on the answer. The app will run on Solaris, Linux, and Windows NT.

Answer:
As a rule, whenever you can choose between free store (or "heap," in C terminology) and stack memory, go with stack memory. It's much faster—sometime allocating an object on free store allocation is hundreds of times (!) slower than allocating it on the stack. Furthermore, using stack memory, you avoid all the hassles of memory leaks, dangling pointers, null pointer dereferencing, and uninitialized pointers. Your code is much cleaner, simpler, and efficient.

You shouldn't be concerned about the size of the objects allocated on the stack. All the platforms you mentioned provide a default stack size of at least 1MB per process, and you can configure the compiler to extend this size even further. So unless you're programming for DOS, stack memory is preferable.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap