The difference between static binding and dynamic array binding

I just read the entire search result on the same topic that I am asking right now in stackoverflow, and in fact this does not meet my curiosity. But here is what.

Question

1.) From what I know, static binding means that it is set at compile time, and it is at run time, whereas for dynamic binding it is set at run time.

2.) So, the book I read represents a dynamic array, it mentions that the size of the dynamic array can be set at runtime. This is done in this way.

Code

int size; cin >> size; int * pz = new int [size]; // dynamic binding, size set at run time delete [] pz; // free memory when finished 


3.) In this code, the book mentions the dynamic size of the array, which can be set at runtime. So out of curiosity I try this.

Code

 int size; cin >> size; int array[size]; //After the array declaraction i assign value to it to check whether it works or not. 


4.) The code above also works, so I'm just wondering what is so special about a dynamic array, since a regular static array could do the same job.

5.) Is it because the dynamic array can free its memory at runtime, while the static cannot do something special?


Thank you for taking the time to read my question, pay attention to any mistake made by me.

+4
source share
3 answers

Your dynamic-sized static array (called a variable-length array, short VLA) only works thanks to the language extension in your compiler. This is a C99 thing that is not contained in the C ++ standard, that is, it will not be portable.

Another obvious difference is that you can pass a pointer to a dynamic array somewhere else, save it somewhere, return it from a function, or, in other words, have it relive the area in which it was created in . You cannot do this with static arrays as they are destroyed at the end of their area.

+6
source
 int size; int array[size]; 

will generate compile-time errors by saying that size is not a compile-time constant or an expected constant expression .

You declare arrays like these

int array[5]

or

 const int size = 100; int array[size]; 

when you know the size of the array long before that.

Otherwise, you use the new approach and delete [] . I would recommend avoiding this construct altogether in favor of std :: vector

+2
source

early binding: - The language in which most bindings are performed during translation is said to have early binding at an early stage of program processing.
Late binding: - a language with late binding binds the largest binding to l runtime.

early binding: - . It is less flexible. Late binding: - . It has more programming flexibility.

early binding: - This data is only accepted in a data type declaration
Late binding: - . In this variable, you can accept any type of data.

early binding: - Defined in the correct job type.
Late binding: - . In this wrong type of right-side assignment, no errors were detected or not detected.

early binding: - Type checking should be done at compile time.
Late binding: - Type checking should be performed at run time.

early binding: - This is a custom compiler or interpreter.
Late binding: - A language that has a late binding to a variable is usually implemented using a pure interpreter, not a compiler.

+1
source

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


All Articles