How is memory allocated in int array
Asked Answered
B

5

8

How much space does a int array take up? Or how much space (in bytes) does a int array consumes that looks something like this:

 int[] SampleArray=new int[]{1,2,3,4};

Is memory allocation language specific ??

Thank you all

Bit answered 27/9, 2013 at 11:14 Comment(3)
You forget Cand phtyon tags.Changeable
This is very much language specific. Choose a language first.Bonn
In java, there is header part which cnould be fixed-size and a alignment addition which is variable so it must be bigger than elementNumber*4+8 for 64 bit system/os/jvm.Wardieu
A
7

Since you add a lot of language tags, I want to write for C#. In C#, this depends on operating system.

For 32-bit, each int is 4 byte and 4 byte also for reference to the object, that makes 4 * 4 + 4 = 20 byte

For 64-bit, each int is 4 byte and 8 byte also for reference to the object, that makes 4 * 4 + 8 = 24 byte

From C# 5.0 in a Nutshell in page 22;

Each reference to an object requires an extra four or eight bytes, depending on whether the .NET runtime is running on a 32- or 64-bit platform.

Adventure answered 27/9, 2013 at 11:23 Comment(3)
so that makes it 80 Byte as I am using 32 bit operating system...also could you explain byte for reference that is where I am stuckedBit
Also 12 bytes overhead for the array object itself (for 32 bit code)Suburbanize
@MatthewWatson What is that 12 bytes for?Changeable
G
2

In C++, how much memory new int[4]{1, 2, 3, 4} actually allocates is implementation-defined but the size of the array will be sizeof(int)*4.

Guinn answered 27/9, 2013 at 11:16 Comment(2)
This question had the c++ tag when I wrote this answer.Guinn
The syntax is not C++, so I removed the tag.Ximenez
R
1

Ques is : Is memory allocation language specific ?? Yes memory allocation is language specific..it vary according the language.. for exp: sizeof(int)*4

in java int size is 4byte so memory consumption is: 4*4=16bytes

Ruler answered 27/9, 2013 at 11:18 Comment(0)
K
1

It depends on both the language, but moreover to the operating system.

You need 4 integers. Normally an integer is 2 or 4 bytes (mostly 4 on most systems), but to be sure check sizeof(int).

(Also keep in mind the values can be differently represented depending on the operating system), like MSB first or LSB first (or a mix in case 4 bytes are used).

Katinakatine answered 27/9, 2013 at 11:18 Comment(0)
J
1

In Java int[] array is an Object which in memory represented by the header (8 bytes for x86) and int length field (4 bytes) followed by array of ints (arrayLength * 4).

   approxSize = 8 + 4 + 4 * arraylength 

see more here http://www.javamex.com/tutorials/memory/object_memory_usage.shtml

Justinn answered 27/9, 2013 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.