-4

Possible Duplicate:
Differences between dynamic memory and “ordinary” memory

I was reading the C++ tutorial and I don't understand why I need to declare dynamic memory, this is what the tutorial says:

Until now, in all our programs, we have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the execution of the program.

And then it says that we have to use new and delete operators to use dynamic memory. However, I seem to be using dynamic memory when declare a pointer, e.g. char* p, for which I have not specified the length of the array of characters. In fact, I thought that when you use a pointer you are always using dynamic memory. Isn't it true? I just don't see the difference between declaring a variable using new operator and not. I don't really understand what dynamic memory is. Can anyone explain me this?

Community
  • 1
  • 1
cracq
  • 81
  • 1
  • 9
  • http://stackoverflow.com/questions/3930013/stack-overflow-static-memory-vs-dynamic-memory –  Apr 19 '12 at 12:30
  • http://stackoverflow.com/questions/2300191/dynamic-memory-allocation-question –  Apr 19 '12 at 12:30
  • http://stackoverflow.com/questions/1699057/why-are-two-different-concepts-both-called-heap –  Apr 19 '12 at 12:31
  • http://stackoverflow.com/questions/660855/what-is-the-origin-of-the-term-heap-for-the-free-store –  Apr 19 '12 at 12:31

6 Answers6

5

I thought that when you use a pointer you are always using dynamic memory. Isn't it true?

No it's not true, for example

int i;
int *p = &i; // uses a pointer to static memory, no dynamic memory.

However, I seem to be using dynamic memory when declare a pointer, e.g. char* p, for which I have not specified the length of the array of characters

char[100] string;
char* p = &(string[0]);  // Same as above, no dynamic memory.

You need dynamic memory when you can't tell how big the data structure needs to be.

Say you've to read some ints from a file and store them in memory. You have no idea how many ints you need. You could pick a figure of 100, but then your program breaks if there are 101. You could pick 100,000 hoping that's enough, but it's waste of resources if there's only 10 in the file, and again, it breaks if there's 100,001 ints in the file.

In this scenario your program could iterate through the file, count the number of ints, then dynamically create an array of the correct size. Then you pass over the file a second time reading the ints into your new array.

Static v's Dynamic Memory
Static memory is static because once the program is compiled it can't be changed, it is static. Variables you declare in functions, and members declared on classes / structs are static. The compiler calculates exactly how many of each its going to need as each method gets called.
Dynamic memory is a "pool" of memory that can be made available to your program on demand, at run time. The compiler only knows it needs to allocate some (probably unknown) amount of that memory, and to release that memory back to the dynamic memory pool.

Hope this helps.

P.S. Yes, there are more efficient ways to get an unknown number of items into memory, but this is the simplest to explain

Binary Worrier
  • 47,526
  • 17
  • 131
  • 178
3

When you have:

char* p;

p is variable of type pointer to char and p is stored on the stack and you haven't allocated any dynamic memory.

But when you do:

p = new char[100];

you have allocated a part of dynamic memory (heap) of the size 100*sizeof(char).

You are responsible to free allocated memory on the heap:

delete[] p;

You don't need to clean variables from the stack - they will be removed automatically after variable goes out of scope. In this example, p will be removed from the stack when it goes out of its scope.

Bojan Komazec
  • 8,186
  • 2
  • 33
  • 48
2

In fact, I thought that when you use a pointer you are always using dynamic memory. Isn't it true?

No. Here's a pointer to stack-allocated ("automatic") memory:

{
    int i;
    int *p = &i;
}
Fred Foo
  • 328,932
  • 68
  • 689
  • 800
2

Dynamic memory is memory which the programmer has to explicity request, as an oppose to have automatically allocated on the stack.

There are many advantages to dynamic memory such being persistent between stack frames (function calls) and can be of varying size.

On the stack an array much be of a certain size:

int ar[5];

However if you 10 element then you can't do it, the solution is to dynamically allocate the memory;

size_t sz;
std::cin >> sz;
int *i_p=new int[sz];

That said everything dynamically allocated must be freed (in C++ using delete)

delete i_p;

However it is generally better where possible to use wrappers to dynamic arrays such as the std::vector

size_t sz;
std::cin >> sz;
std::vector<int> vect(sz);

This will automatically manage the memory and provide a useful interface to the array.

111111
  • 14,528
  • 6
  • 38
  • 58
2

Let's say you want to read an unknown number of integers from a user. You could, for example, declare int numbers[100], ask the user how many numbers there are (let's say this is store in variable n) and if he enters a number larger than 100, you would have no choice but to report an error. Alternatively, you could write int *numbers = new int[n] and allocate just enough space for all the numbers.

zvrba
  • 23,242
  • 3
  • 52
  • 65
2

Dynamic memory in c++ is a memory allocated in a heap of operation system by using new operator. You need the dynamic memory when you need to allocate the objects which are too large and cannot be allocated in the stack, or when you have a multithreaded environment and need to share the memory allocated in one of the threads between the different threads. Pointer doesn't mean that you use the dynamic memory pointers also can contain the a stack address related with the object in the stack.

AlexTheo
  • 3,747
  • 1
  • 19
  • 32