When do I need dynamic memory?

Possible duplicate:
Definition of Malloc or regular array?

We learn that in C and dynamic variables there is dynamic memory:

#include <stdio.h> int a = 17; int main(void) { int b = 18; //automatic stack memory int * c; c = malloc( sizeof( int ) ); //dynamic heap memory *c = 19; printf("a = %d at address %x\n", a, &a); printf("b = %d at address %x\n", b, &b); printf("c = %d at address %x\n", *c, c); free(c); system("PAUSE"); return 0; } 

How to find out what type of memory to use? When am I one or the other?

+8
c malloc memory free
source share
6 answers

Use dynamic mode in the following situations:

  • When you need a lot of memory. The typical stack size is 1 MB, so better than 50-100 KB should be dynamically allocated or you run the risk of colliding. Some platforms may have this limit even lower.

  • When the memory should work after the function returns. Stack memory is destroyed when the function ends, dynamic memory is freed when you want.

  • When you create a structure (for example, an array or a graph) of a size that is unknown (i.e. it can become large), it changes dynamically or it is too difficult to calculate beforehand. Dynamic allocation allows your code to naturally request memory in parts at any time and only when you need it. It is not possible to re-request more and more stack space in a for loop.

Prefer stack location otherwise. It is faster and cannot leak.

+17
source share

You use dynamic memory when the size of your distribution is not known in advance only at runtime.

For example, you ask the user to enter names (let say up to 10 names) and save them in an array of strings. Since you do not know how many names the user will provide (only at runtime), you will have to allocate an array only after you know how much to allocate in order to use dynamic allocation.

Of course, you can use a fixed-size array of 10, but for large sums this will be wasteful

+4
source share

Use dynamic memory allocation if you do not know exactly how much memory your program will need when compiling.

int a[n] , for example, will limit your array size to n. In addition, it allocates nx 4 bytes of memory regardless of whether you use it or not. This is allocated on the stack, and the variable n must be known at compile time.

int *a = (int *)malloc(n * sizeof (int)) , on the other hand, assigned at runtime on the heap, and n should only be known at runtime, not necessarily at compile time.

It also ensures that you allocate as much memory as you really need. However, since you allocated it at run time, the cleanup should be done by you with free .

+2
source share

You should use dynamic memory if:

  • If you want your object to persist outside the area in which it was created.
  • Typically, the size of the stack is limited, and therefore, if your object takes up a lot of memory, then you may encounter stack space, in such cases dynamic allocation of memory is usually done.

Note that the c99 standard introduces Variable Length Arrays (VLA) in C, so you do not need to use dynamic memory allocation just because you do not know the size of the array before hand (if only the above #2 is so)

It is best to avoid dynamic memory allocations as much as possible, because this means explicit memory management instead of an automatic mechanism provided by the language. Explicit memory management means that you tend to make more mistakes, which can lead to disastrous consequences.
Having said that dynamic memory allocations cannot always be avoided and they must be used when use is mandatory (two cases mentioned above).

+2
source share

If you can program without dynamic allocation, do not use it!




























+2
source share

Als made an interesting idea that you should allocate memory from the heap if your object should remain outside the area in which it was created. In the above code, you do not need to allocate memory from the heap at all. You can rewrite it as follows:

 #include <stdio.h> int a = 17; int main(void) { int b = 18; //automatic stack memory int c[1]; // allocating stack memory. sizeof(int) * 1 c[0] = 19; printf("a = %d at address %x\n", a, &a); printf("b = %d at address %x\n", b, &b); printf("c = %d at address %x\n", c[0], c); system("PAUSE"); return 0; } 

In fact, as part of the C99 ( Variable Length Array ) standard, you can use the [] operator to allocate dynamic space for an array on the stack in the same way as usual for creating an array. You do not even need to know the size of the array at compile time. The compiler will simply adjust the esp register (for x86 ) based on the requested space to accommodate, and you're good to go.

+1
source share

All Articles