<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 19 Dynamic Memory Allocation_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 19: Dynamic Memory Allocation
 More of this Feature
• Introduction
• Allocating Single Objects
• Dynamically Allocation Arrays
• Dangling Pointers
 
 Related Resources
• Introduction to Pointers
• C Tutorial - Dynamic Memory Allocation
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial - See all lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson is a brief aside in our tour of classes and objects, but it covers a topic, dynamic memory allocation, that will be needed in future lessons. In C++, space in memory for variables may be either statically or dynamically allocated. Statically allocated objects are those that are not created with the memory allocator new, that is, they are just the ordinary objects we have been using through out this tutorial.

int x;
float money;
Employee janitor;

These objects are of fixed, known size and the compiler arranges the required space as it turns source code into a binary or executable program. Statically allocated objects that are of local scope are put into a memory space known as the stack. Statically allocated objects of global scope live in the global address space. The key point is that for these objects their size is fixed at compile time.

Now, suppose we don't know the size of an object until program execution. Examples of this are a buffer to hold a block of text of variable size, or an array with an undetermined number of elements We could try to size the buffer or array to be large enough to hold the worst case, that is, to be big enough to hold anything we should encounter. But, there are two problems with this strategy. First, it consumes memory unnecessarily. This is less of an issue than in the past when memory was more limited, but still impacts overall system performance. Second, no matter how much memory is statically set aside for our object, we can never be sure it will be large enough. This is really a ticking time bomb. Eventually, someone will try to load one too many elements, or type on too many words and then our program will either crash or run erratically. The solution to this problem is dynamic memory allocation, which is not surprising given the title of this lesson.

Next page > Allocating Single Objects > Page 1, 2, 3, 4


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.