Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The practice of programming (Some confusing statements)

The other day I was reading The practice of programming by Kernighan and Rob Pike.

In the second chapter, under the Searching section, I read some of these lines which created confusion in my mind.

"Nothing beats an array for storing static tabular data. Compile-time initialization makes it cheap and easy to construct such arrays. (In Java, the initialization occurs at run-time, but this is an unimportant implementation detail unless the arrays are large.)"

My question is that how in any language its possible to have compile time initialization of array or variable if the user is going to supply it only at run time and also the memory allocation for variables takes place at run time. Without knowing the memory address how an array can be initialized?

like image 246
Jayram Avatar asked Oct 29 '13 10:10

Jayram


2 Answers

It does not say that the data is supplied at runtime. It just says "static" data. If it is known at compile-time and then the compiler could compile it right into the code. API keys, tables with "magic numbers", or error message texts fit into that pattern.

like image 116
Thilo Avatar answered Oct 19 '22 23:10

Thilo


I think you misunderstood what the author was saying. Note:

This is a static array, in Java:

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

Now, Java doesn't allow you to declare real dynamic arrays like Delphi and another languages, for dynamic ones, we must choose another data structure called ArrayList like this example:

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

If the user wants use the static array with runtime defined length, the most flexible way he can do is the following:

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

This is static array in Delphi:

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

And this is 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 that how in any language its possible to have compile time initialization of array or variable if the user is going to supply it only at run time and also the memory allocation for variables takes place at run time. Without knowing the memory address how an array can be initialized?

That said, we can easily see that, THIS is a "compile time" initialization (doesn't matter the fact of implementation details)

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

Once the array is initialized it CANNOT BE resized, so the OS can allocate the memory wherever it wants. And since arrays are sequential in the memory, by using indexes, Java knows what address you wants to get.

Considering the array above, this is the 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 easy to understand, actually String is also a pointer to something.

Alias is how Java hides the pointer arithmetic, that is, allowing us to access indexes instead of memory addresses.

Here is some documentation about arrays:

Read Static Arrays and see Array List

like image 37
EProgrammerNotFound Avatar answered Oct 19 '22 23:10

EProgrammerNotFound