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
Define register with bit fields? We could define register simply with bit fields: struct DISK_REGISTER { unsigned ready:1; unsigned error_occured:1; unsigned disk_spinni

We need to Decompile ex4 to mq4 I have three expert advisors for mt4, which I need to decompile to its original mq4 code. Skills required are C Programming, C++ Programming,

What is the difference among a copy constructor and an overloaded assignment operator? Ans) A copy constructor constructs a latest object by using the content of the argument

How are postfix and prefix versions of operator++ () differentiated? A: The postfix version of operator++ () contain a dummy parameter of type int. The prefix version does not c

I need CSGO Wallhack and Human Aimbot Project Description: Need a CSGO hack which can ESP Wallhack and Aimbot realistically. Must be VAC Proof, and ESL proof, as wel

my program does not run more than 9 iterations when ever i want to iterate more than 100 iterations. what is the reason and how to tackle?

#padovan string   program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : package test.padovanstring; public class PadovanString {

A: The answer to this lies actually, how they used. Along ++i(PreIncrement) the variable is incremented and new value is returned. Thus it needs one instruction to increment the va

how to compare one file with another file. comparison should be like first line of first file compare every line of second file until it gets a blank line starting from the top and