-3

I'm currently using C/C++. But what is the main reason of using malloc/new instead of just declare some var on the stack. Like: int a;

Or another example:

  1. int *a; // then use this to track an array

  2. int a = (int)malloc(sizeof(int));

Daren Lin
  • 15
  • 2
  • 2
    Maybe you want a value to persist even after a function returns, or you want to allocate a huge block of memory too large for the stack. – cdhowie Oct 28 '14 at 16:08
  • 9
    C and C++ are different languages. – Neil Kirk Oct 28 '14 at 16:09
  • There is no "main reason". There are pro and cons and they change in whatever situation you are in. Understand what each does, then decide if you rather use malloc/new or the stack in that situation. – Slyps Oct 28 '14 at 16:10
  • Two main reasons. 1. To allocate memory that lives beyond the scope of the allocating function. 2. To allocate large amounts of memory, that would otherwise run the risk of stack overflow if allocated automatically. – David Heffernan Oct 28 '14 at 16:13
  • I can't believe this isn't a dupe, but I can't find one... edit: maybe this: http://stackoverflow.com/questions/14013620/using-malloc-over-array – Baldrickk Oct 28 '14 at 16:26

3 Answers3

1

Automatic variables have a lifetime that ends when the program leaves the block of code that declares them. Sometimes you want them to live longer than that; dynamic allocation gives you full control over their lifetime. Sometimes they are too big for the stack; dynamic memory is (typically) less restricted.

With that flexibility comes responsibility: you need to delete them when you've finished with them, but not before. This is difficult to get right if you try to hold onto a raw pointer and do it yourself; so (in C++) learn about RAII, and use ready-made management types like smart pointers and containers to do the work for you.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

TL;DR: stack and heap are two different memory areas, they serve different purposes, they have their own access pattern (with everything that implies) and policies (yes, stack memory is way more controlled than heap memory on hw-enforced systems).

Stack memory is a precious, limited resource that needs to be allocated contiguously (you can't chunk it as you would for a heap allocation).

As a consequence of this, its default dimension on many x86 platforms is usually around 1 MB (although this can be increased). Definitely small with regard to how much memory you might allocate with a heap allocation. Your question isn't an exact duplicate of this question but I believe you should take a look at it if you're interested in other reasons for the stack being a limited resource.

Other reasons include visibility scopes (stack stuff is collected/destroyed at the end of the scope while heap allocated memory can be used in other parts of your application until you free it).

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
0

The C standard function malloc() with its friends (free(), calloc() and realloc()) allows to dynamically allocate arbitrary large (allowed by OS though) chunks of memory in runtime. This has informal name of dynamic storage duration (formal one from N1570 is allocated storage duration) with following characteristics:

  • lifetime is cotrolled by malloc() and free() calls. This is unlikely to automatic storage duration variables, where their lifetime is restricted by scope (e.g. block, more precisely by { and }, with exception to VLAs) and static storage duration, which have lifetime of whole execution of program
  • scope is determined by availability of pointer to allocated data

In other words you use malloc() when you need maximum flexibility of data allocation in runtime. Automatic variables are practically limited by stack size and static variables require compile-time reservation for exact (i.e. static) amount of memory.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137