简体   繁体   中英

How is memory allocated in int array

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

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.

在C ++中, new int[4]{1, 2, 3, 4}实际分配的内存量是实现定义的,但数组的大小将是sizeof(int)*4

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

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM