By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

Memory Allocation in an array

P: 100
When I make a static array, like so:

int myArray[100];

Will the array 400 bytes big, right away? or will it only assign the memory when it's actually needed?

eg; if I was then to call int myArray[64] = 5; the array would then be 4 bytes.

I have the same question for dynamic arrays, if I done:
int *myArray = new int[100];

Would it then be 400 bytes big, or only assign the memory as needed?
Nov 18 '08 #1
Share this Question
Share on Google+
7 Replies

P: 63
In both cases, the memory is allocated immediately. It doesn't wait for you to access it.
Nov 18 '08 #2

P: 100
Ok, thanks.

I just thought that it may try do this to try and save memory.
Nov 18 '08 #3

P: 63
In C and C++, the program generally does what you tell it to do.

If you would like the program to save memory (and sacrifice speed, since you have to check whether to allocate more memory or not), then you have to implement that yourself.
Nov 18 '08 #4

Expert 100+
P: 1,275
When u do a static alloction, the memory is allocated when object gets loaded into the memory or when the function stack gets loaded.
When u do a dynamic allocation the memory is allocated during run time and in bot cases the memory is allocated fully to the size requested for.

Nov 19 '08 #5

Expert Mod 5K+
P: 8,916
int myArray[100];

Will the array 400 bytes big, right away?
This is a minor point and slightly off topic for the question but the size of the array will be 100 * sizeof(int). It sounds like your system has 4 byte ints which would make the result of that calculation 400 bytes but ints are not the same size on all platforms. Plenty of platforms have 2 byte ints and some even have 1 byte ints.

An int is supposed to be the size that the platform find easiest to process (i.e. most efficient) 1 byte on an 8 bit platform, 2 bytes on a 16 bit platform 4 bytes on a 32 bit platform. In practice int is most often 2 or 4 bytes (some 8 bit platforms still use 2 byte ints).

In theory the modern 64 bit platforms should have 8 byte ints (and longs) but for some reason most of the compiler manufactures decided to keep ints and longs 4 byte and add the 8 byte long long. IMO this was most likely to prevent breaking all that code out there that assumes long is 4 bytes.
Nov 19 '08 #6

Expert 10K+
P: 11,448
if you do 'int myArray[100]' 100*sizeof(int) bytes are reserved for your array which
could very well be 400 bytes.

For dynamically allocated memory 'int* myArray= malloc(100*sizeof(int))'
things can be a bit more complicated. Most systems allocate all those bytes
for you immediately (or they fail) but on some (older) systems the memory
(all 400 bytes) is only allocated when you try to dereference the pointer myArray.

Notably the old AIX systems worked that way and it could cause havoc because
you could never tell in advance whether or not the allocation succeeded.

I don't know of any systems that allocate part of the array on demand.

kind regards,

Nov 19 '08 #7

Expert Mod 5K+
P: 9,197
int *myArray = new int[100];
The fact that you make this statement tells me you are using C++.

Do not use arrays in C++. Use the vector container. It is implemented as an array and it allocates memory as needed.
Nov 19 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.