D
D
DVoropaev2017-08-29 21:26:07
C++ / C#
DVoropaev, 2017-08-29 21:26:07

How much memory is allocated for an array in C++?

If I write like this:

unsigned int a[] = {1, 2, 4, 8, 16, 32, 64, 128, 512, 256};

then how much memory will be allocated for the array? Does it depend on the compiler?
I'm assuming that exactly as many items are selected as there are items in the list (i.e. sizeof(type) * number of items in the list).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2017-08-29
@DVoropaev

I assume that exactly as many items will be selected as there are items in the list

Exactly right with one exception. Memory may be allocated a little more due to alignment. Accessing memory at aligned addresses is much faster, which makes it more efficient, for example, to allocate an additional byte at the beginning (so that the array address becomes a multiple of 2/4/8/16 (depending on which alignment is used in the architecture).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question