2

I am not sure I understand how stack works...

  1. Is there any performance difference between static and dynamic arrays, excluding the time needed for memory allocation?

    char* data = (char*)malloc(100);
    char data[100];
    
  2. From what I understand stack is only a few MB. Why is the stack limited? Why have stack and heap and not a single place to store memory?

  3. Should all arrays be allocated dynamically? For example, having one array with size 1KB on stack, is it a good idea?

Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
Luka
  • 1,641
  • 1
  • 18
  • 28
  • [C++ Which is faster: Stack allocation or Heap allocation](http://stackoverflow.com/q/161053/11683) – GSerg Feb 25 '14 at 20:00
  • http://stackoverflow.com/questions/408670/stack-static-and-heap-in-c?rq=1 – Ashalynd Feb 25 '14 at 20:00
  • [What and where are the stack and heap?](http://stackoverflow.com/q/79923/11683) – GSerg Feb 25 '14 at 20:01
  • 1
    I am just learning, also, I found no information regarding question 3. What is considered a good practice? – Luka Feb 25 '14 at 20:08
  • @Luka Duplicate or not I don't doubt that someone would be willing to write up an excellent answer sooner or later (judging from the 2 upvotes.) –  Feb 25 '14 at 20:14
  • "Should all arrays be allocated dynamically?" No, all arrays should not be allocated dynamically. – juanchopanza Feb 25 '14 at 20:17

1 Answers1

2

Is there any performance difference between static and dynamic arrays, excluding the time needed for memory allocation?

The performance difference is in initializing and any changing of the array capacity. Arrays can be randomly accessed are about the fastest container as far as element access goes.

From what I understand stack is only a few MB. Why is the stack limited? Why have stack and heap and not a single place to store memory?

Stack, dynamic and program memory is limited. Computers don't have infinite memory size.

The amount of memory devoted to stack or heap can be altered by the build environment and maybe by the OS. If a program needs more memory, it can ask the OS for more (but the OS may use paging and virtual memory).

There are more than one type of memory regions because variables and data have different lifetimes. And they may have different sizes as well.

Should all arrays be allocated dynamically? For example, having one array with size 1KB on stack, is it a good idea?

No, allocating arrays that don't change in size and are small should not be allocated dynamically. Smaller arrays can be allocated as local variables; while larger ones may want to be located in filescope (automatic variables). Huge arrays may have to be allocated in dynamic memory or in OS memory depending on the limitations set by your compiler or OS.

Also, there is no point in dynamically allocating arrays of constant values (such as menu texts or lookup tables). These could be placed into a read-only memory area or in the same space as the executable.

Is there a reason you need to limit the flexibility of array allocations? (Remember, there are very many different applications out there, from embedded to desktop.)

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144