It's sort of an apples-to-oranges comparison because ArrayWithTemplate is allocated on the stack, and ArrayWithConstructor is [dynamically] allocated on the heap.
If I may, a better comparison would be the following:
1 2 3 4 5 6 7 8
|
template<int Size>
class ArrayWithTemplate {
int array[Size];
};
class ArrayWithHardcodedValue {
int array[10];
};
| |
ArrayWithHardcodedValue will look exactly the same as ArrayWithTemplate<10>, with no extra overhead, if that answers your question.
the ArrayWithTemplate<1> will not consume more memory than ArrayWithHardcodedValue with array[1] instead of [10].
It will most likely put a hardcoded value in the exe to say how much stack space to allow.
When I construct array1 variable, will the size of the array '1' use some memory during runtime? |
You will inevitably have to put some value in a registry somewhere to make things function. Does that count as memory for you? It certainly doesn't need to load additional memory; putting a hardcoded value in a registry is about as basic of an operation as you can get.
ex, ArrayWithTemplate<100>:
1 2 3 4 5 6 7 8
|
subq $432, %rsp
.seh_stackalloc 432
leaq 128(%rsp), %rbp
.seh_setframe %rbp, 128
.seh_endprologue
call __main
movl $0, %eax
addq $432, %rsp
| |
The 432 number correlates with the size of 100.
ArrayWithTemplate<1000> produces:
1 2 3 4 5 6 7 8 9 10
|
movl $4032, %eax
call ___chkstk_ms
subq %rax, %rsp
.seh_stackalloc 4032
leaq 128(%rsp), %rbp
.seh_setframe %rbp, 128
.seh_endprologue
call __main
movl $0, %eax
addq $4032, %rsp
| |
It has an extra check when it calls ___chkstk_ms, which I assume is some Microsoft safety check, but that's unrelated and trivial.