can anyone explain what is main difference between the static array and a dynamic array....also i am confused with dynamic array and dynamic allocated array????can anyone help me...
Difference between a static array and a dynamic array
Collapse
X
-
Tags: None
-
A static array has memory allocated at compile time.
Please note: This has nothing to do with the C/C++ static storage class specifier.
A dynamic array has memory allocated at run time.
A dynamic array and a dynamically allocated array are the same thing. -
-
Compile time is when your code is converted by the compiler into machine instructions. The memory for the static array will be allocated when these machine instructions execute. There is no way to change the amount of memory allocated when the program is running.
Run time is when the program is actually running. An array created at this time requires the number of bytes required to be submitted to a memory allocation function which will allocate the memory and return the address of the allocation:
You might read this: http://bytes.com/topic/c/insights/77...rrays-revealedCode:int arr[25]; //static array. Can't change the 25 later. int* ptr; int num = 25; ptr = (int*)malloc(num * sizeof(int)); //dynamic array //num can be changed later for a different sized array.Comment
-
An array created at compile time by specifying size in the source code has a fixed size and cannot be modified at run time. The process of allocating memory at compile time is known as static memory allocation and the arrays that receives static memory allocation are called static arrays.
This approach works fine as long as we know exactly what our data requirements are. Suppose we don't know what are our data requirements in that case we can use dynamic arrays.
A static array is declared at compile time; thus, its size must be a compile time constant. Most older languages, such as FORTRAN, COBOL and even Pascal, use this technique exclusively. These cannot have variable sizes because allocation of the array is done at compile-time. These are usually placed on the stack. The memory management is easy because everything is done at compile-time and no re-sizing is allowed.
A dynamic array may be declared at compile-time but it is not dimensioned (the number of elements determined) until run-time. Thus, the value that determines the size may come from a constant or variable.Comment
Comment