Message from C, C++ discussions

December 2019

— Thanku



Can anyone explain what is the benefit of fixed-size array on stack versus dynamic size memory block (array) in heap?

Except possible compiler optimizations *

There are no any performance differences: in both cases the cpu just accesses memory; memory consuming isn't heavily different

— Memory on stack is already on stack, no dynamic alloc

— I looked some lectures, and one said that changing small vector to array in hot spot made program run 40-70% faster, but this are rare cases

Message permanent page

— Not actually

In the case of allocating memory on stack we just need to reduce current stack pointer register, with heap we need to use system calls

I wouldn't call it benefit

Is there any other benefit?

Message permanent page

— Any benefits could be measured, i will not assume any, having not so much experience

— And I'm talking only about these two cases, not vector:
int *heap = malloc(N bytes);
int stack[N elements];

Message permanent page

— Allocating memory on a heap costs more than on a stack

— Not always

— Talk about custom allocators

— Almost always

— That isn't the benefit, that I wanted to know :D

I already noticed the stack pointer