Dynamic memory management, C/C++ Programming

C and C++ require explicit dynamic memory management, using new and delete or malloc() and free().

It is helpful to understand where variables exist (usually the stack or the heap, sometimes the data segment 1).

The stack is managed for you by the compiler, so it's usually the easiest memory to use. Local variables go on the stack, and passed function parameters go on the stack2. Since the stack frame can change signi?cantly between uses, do not return pointers to the stack! This is analogous to dereferencing freed memory.

If a variable needs to exist longer than a function call, then you should put allocate space for it on the heap (with new, for instance). If you allocate space for a variable, remember to free that space when you're done with it! If you allocate memory but don't free it, you'll end up with memory leaks, which usually becomes a problem when a program is supposed to run for a long time, repeatedly allocating and forgetting to free. It can be helpful to write allocating functions/methods at the same time as freeing functions/methods, so that you don't forget to deallocate memory from the heap.

Posted Date: 3/12/2013 4:01:06 AM | Location : United States

Related Discussions:- Dynamic memory management, Assignment Help, Ask Question on Dynamic memory management, Get Answer, Expert's Help, Dynamic memory management Discussions

Write discussion on Dynamic memory management
Your posts are moderated
Related Questions
minimum of seven number

Class Templates In addition to function  templates, C++  also supports the  method of class templates. By definition, a class template is a class definition that explains a fam

simple coding for pebble merchant..

Define the Float Data Type of c Language? The float is used to define floating point numbers. The Floating point numbers are stored in 32 bits with 6 digits of precision. Eg

in this problem you are given a board in which some of the elements are placed..each element represent ancolor.fill the other elements in the board such that none of the adjacent e

We need an E Commerce site likefifacointrader. you must have experience with that kind of work You have to show a sample of work like that site. Skills required are C Prog

Pupils' heights The health visitor at a school is going to measure the heights of all pupils. For each class she makes a statistics giving the number of pupils of each height and

should i put define constant and memory constant in my flowchart? the other one, how to draw flowchart for break and continue statement?