Arrays in different languages ​​- store links or raw objects?

I'm trying to wrap my head around the way that raw memory looks in different languages ​​when using an array.

Consider the following Java code:

String a = "hi"; String b = "there"; String c = "everyone"; String[] array = {a, b, c}; 

Obviously, the array contains references, not objects; those. in memory there is a continuous array of three links, each of which points to a different place in memory where the object is located. Therefore, the objects themselves are not necessarily in three adjacent buckets; rather, there are links.

Now consider the following:

 String[] array = {"hi", "there", "everyone"} 

I would suggest that in this situation, strings exist somewhere with all the other constants in memory, and then the array contains references to these constants in memory? So, again, in raw memory, the array is not like ['h', 'i', '\0', 't', 'h', 'e', 'r', 'e'... (etc)] . (using c-style for convenience). Rather, it is more like ['a83a3edf' ,'a38decd' ... (etc)] , where each element is a memory location (link).

My conclusion from this thought process is that in Java you can never think of arrays as buckets of adjacent objects in memory, but rather as continuous references. I can think of no way to guarantee that objects will always be stored in Java.

Now consider C:

 char *a = "hi"; char *b = "there"; char *c = "everyone"; char *array[] = {a, b, c}; 

The above code is functionally equivalent to Java above - that is, the array contains references (pointers) to another memory location. Like Java, the objects pointed to are not necessarily contiguous.

HOWEVER, in the following C code:

 struct my_struct array[5]; // allocates 5 * size(my_struct) in memory! NOT room for 5 // references/pointers, but room for 5 my_structs. 

The structures in array ARE are adjacent to raw memory.

Now for my specific questions:

  • Was I right in assuming that in Java arrays should ALWAYS contain links, since the programmer only ever has access to links in Java? What about raw data types? Will it work differently then? Will an int array in Java look the same as one in C in raw memory (except that it adds an Object class, a cool Java class)?

  • In Java, is there no way for a programmer to guarantee continuous allocation of memory by objects? This can happen by chance or with high probability, but the programmer can not GUARANTEE that this will be so?

  • In C, programmers MAY create arrays of arrays of objects (structures) contiguously in memory, as I showed above, right?

  • How do other languages ​​deal with this? I assume Python works like Java?

The motivation for this question is that I want to get a clear idea of ​​what is happening at the raw memory level with arrays in these languages. Mostly for questions related to the programmer. In a previous interview, I said that an array (not in any language, as a rule, as a whole) holds objects contiguously in memory, for example, buckets. Only after I said this did I realize that it’s not quite the way it works in a language such as Java. Therefore, I want to be 100% on it.

Thanks. Let me know if something needs clarification.

+6
source share
4 answers

you can never imagine arrays as buckets of adjacent objects in memory, but rather as adjacent links.

In theory, you're right; in practice, the JVM does not provide random access to memory. It sequentially allocates memory and copies objects during the GC in the detection order (or in reverse)

Was I right in assuming that in Java arrays should ALWAYS contain links, since the programmer only ever has access to links in Java?

Yes, unless you have an array of primitives, of course.

What about raw data types? Will it work differently?

Primitives and references are continuous in memory. They are basically the same.

Will an ints array in Java look the same as one in C in raw memory (besides the Object class, cruft Java will add)?

Yes.

Is there no way in Java that a programmer can guarantee continuous allocation of memory by objects?

Not unless you use heap memory. Although this is usually not such a big problem, as you might think, as most of the time, objects will be continuous in memory.

This can happen by chance or with high probability, but the programmer can not GUARANTEE that this will be so?

right. You usually have more problems when you look at the worst 0.1% delay or higher.

In C, programmers MAY create arrays of arrays of objects (structures) in memory, as I showed above, right?

Yes. You can do this in Java as well, but you must use heap memory. There are a number of libraries that support this, for example, Javolution, Chronicle, SBE.

+6
source

Low-level languages, such as C, allow you to deal with a memory layout, and whether you have a pointer to something else or a value right here. Make sure that you manage stack and heap allocation correctly and don't forget free() every malloc() pointer.

Higher-level languages ​​such as Java, Python, and JavaScript remove this low-level memory layout. All objects are on the heap, and you have a link to it. Although the link is like a pointer, it is opaque and not directly related to this memory location. Thus, all data structures contain references to objects.

0
source

to 1) Objects are located in java arrays, and objects and arrays are stored on the heap, since the heap may not be continuous, so arrays may also not be continuous.

4) In python, you can create an adjacent array if you use scipy

0
source

I can’t talk in detail about Java, although I understand that given the following code

 int arr[] = new int[N]; 

the local (stack) variable arr contains a reference to the array object on the heap, giving us a layout something like this:

  +---+ arr: | |---+ +---+ | ... | +---+ | cp: | |<--+ class pointer +---+ flg: | | flags +---+ lck: | | locks +---+ sz: | | size +---+ arr[0]: | | +---+ arr[1]: | | +---+ ... +---+ arr[N-1]: | | +---+ 

For an array of primitive types, values ​​are stored directly in arr[0] , arr[1] , etc. For an array of class types, each element of the array stores a reference to an instance of this class, therefore a different level of indirection. The links themselves are stored contiguously, but the instances they point to are not (or at least not guaranteed).

C and C ++ arrays are much more complicated. Given the following code:

  int arr[N]; 

You will get the following:

  +---+ arr[0]: | | +---+ arr[1]: | | +---+ ... +---+ arr[N-1]: | | +---+ 

There is no indirect or metadata associated with the array C. There is no storage space for the arr object to point to the first element of the array. If the array is auto (this means that it was declared in a block, not static ), then the memory for the elements of the array is allocated in the same way as for any local variable.

For any type T T arr[N] allocates N adjacent elements to store values ​​of type T If T is an abhorrent type of struct , then T a[N] stores N continuous instances of this indecent type of struct .

0
source

All Articles