Programming practice (some confusing statements)

The other day I read the Kernigan and Rob Pike Programming Practices.

In the second chapter, in the Search section, I read some of these lines that created confusion in my mind.

"Nothing compares an array to hold static tabular data. Initializing compilation time makes it cheap and easy to create such arrays. (In Java, initialization occurs at runtime, but this is an unimportant implementation detail if arrays are not large.)"

My question is that in any language it could be compiled to initialize the time of an array or variable if the user would supply it only at run time, and memory allocation for variables occurs at run time. Without knowing the memory address, how can an array be initialized?

+6
source share
3 answers

He does not say that data is provided at runtime. It just says โ€œstaticโ€ data. If this is known at compile time, then the compiler can compile it directly into code. API keys, magic number tables, or error message texts fit into this template.

+1
source

I think you misunderstood what the author said. Note:

This is a static array in Java:

String[] suit = { "item 1", "item 2", "item 3", "item 4" }; 

Now Java does not allow you to declare real dynamic arrays, such as Delphi and other languages, for dynamic, we must choose another data structure called ArrayList , like this example:

 List<String> list = new ArrayList<String>(); 

If the user wants to use a static array with a specific execution length, in the most flexible way he can do the following:

 int maxsize = Integer.ParseInt(JOP.ShowInputDialog("give me a number")...); int[] myArray = new int[maxsize](); 

This is a static array in Delphi:

 const MyStaticArray : array [0..3] of Integer = (0, 1, 2, 3); 

And this is a dynamic array

 var MyDinamicArray : array of Integer; MaxSize: Integer; begin MaxSize := StrToInt(InputBox(..,'Give me a number', ..)); SetLength(MyDinamicArray, MaxSize); //Defines the array size, in runtime; end; 

My question is how in any language it can be compiled during array or variable initialization, if the user is going to supply it only at runtime, and memory allocation for variables occurs at runtime. Without knowing the memory address, how can an array be initialized?

However, we can easily see that THIS is the initialization of the "compilation time" (it does not matter, the fact of implementation details)

 String[] suit = { "item 1", "item 2", "item 3", "item 4" }; 

After the array is initialized, the size CANNOT BE ; therefore, the OS can allocate memory where it wants. And since arrays are sequential in memory using indexes, Java knows what address you want to get.

Given the array above, this is a memory sketch:

// Program memory

 address 00A1 value | 00BA | alias suit 

// OS memory

 address 00BA 00BB 00BC 00BD value | "item 1" | "item 2" | "item 3" | "item 4" | alias suit[0] suit[1] suit[2] suit[3] 

The string is here to make it understandable; in fact, String is also a pointer to something.

Alias โ€‹โ€‹is how Java hides pointer arithmetic, that is, it allows us to access indexes instead of memory addresses.

Here are some docs about arrays:

Read Static Arrays and see List of Arrays

+1
source

From JLS

10.2. Array variables

An array type variable contains a reference to an object. Declaring an array type variable does not create an array object or allocate space for array elements. It creates only the variable itself, which may contain a reference to the array.

However, the initializer part of the declarator (ยง8.3, ยง9.3, ยง14.4.1) can create an array, the reference to which will then become the initial value of the variable.

0
source

Source: https://habr.com/ru/post/956931/


All Articles