Deepak Pandey

Static Data Structure

In this article, we are going to learn about the static data structure. Before getting started with the static data structure let us get a short overview of what is a data structure and which data structure is of type static.

Data Structure: A computer can contain an abundant amount of data, so a Data Structures. helps in organizing those data and storing them in a particular manner, such that we can efficiently perform numerous operations on them. Simply, Data structures are used to reduce the complexity (mostly the time complexity) of the code.

Basically a data structure is of two types :

  1. Static Data Structures
  2. Dynamic Data Structures

Let us also get a short overview of them.

Static Data Structure : A static data structure is an organization or collection of data in memory which have a fixed size, that is, it can store a limited amount of elements or data in it. Array is an example of static data structure.

Dynamic Data Structure : A dynamic data structure is an organization or collection of data in memory which do not have a fixed size, that is, its size can be modified during the operations performed on it and can store a variable amount of elements or data in it. Few examples of dynamic data structure are: ArrayList, LinkedList.

In this article, we are going to discuss the static data structures in detail. So let us now begin with the main agenda of our article, static data structure.

What is a Static Data Structure?

static data structure is an organization or collection of data in memory which have a fixed size, that is, it can store a limited amount of elements or data in it. The maximum amount of data to be stored in the static data structure must be known before the declaration of the data structure and its size is fixed in memory that is we cannot reallocate extra memory for it later, although The content of the data structure can be modified.

STATIC DATA STUCTURE 1

Explanation :

In the above image, we can see how the size of the static data structure is fixed. Once the data structure is allocated in the memory with a fixed size, we can not change the memory space allocated to it. Although the content of the data structure can be modified.

Let us look at some data structures which are static in nature and have fixed sizes allocated in memory.

Example for Static Data Structure

One of the most prominent examples of a static data structure is an Array. Let us discuss it in detail.

Array

An array is a container object or a collection that stores a fixed amount of data of a similar type at contiguous memory locations. By “similar type”, we mean that the elements in an array must be of the same data type in Java, that is they are homogeneous in nature. The length of an array is determined at the time of its declaration, and that length is allocated in the memory, which is fixed and can not be resized later. We cannot reallocate extra memory for the array once it is initialized with a fixed size. Hence, we can say an array is a static data structure.

Arrays in java are not similar to the arrays in c/c++. In java, the size of an array is fixed, whereas, in c++ it is not fixed. In java, if we perform any operation with an index that is not inbound with the array, it will throw an error stating indexed out of bounds, whereas, in C/C++ it will not throw such an error message. In C++, it is possible to create an array of fully constructed objects in a single operation, whereas, Java requires multiple operations to create an array of objects. Like in C++ to create an array of objects the syntax is class_name array_name [size] ; and the array of object is created, but in java we at first need to declare the array of object with syntax Class_Name[ ] objectArrayReference;, then we need to instantiate the array of objects with syntax Class_Name obj[ ]= new Class_Name[Array_Length];, then we need to initialize the array of object using either constructors or a separate member method, and the array of object is created.

STATIC DATA STUCTURE 2

Explanation :

In the above image, we can see how the data is allocated in the memory at contiguous memory locations.

Let us look at the syntax of an array data structure.

Syntax :

int[] ARRAY_NAME= new int[SIZE_OF_ARRAY]

Explanation :

In the above syntax, we are initializing the array with the new keyword, and using the new keyword the array data structure will be initialized in the heap memory. We can see how we are giving the fixed size of the array to be stored in the memory in advance. Hence, we can say an array is a static data structure with a fixed size in memory.

Features of Static Data Structures

Static data structures have many features. Let us look at the prime features of static data structures:

  • We do not need to store the structural information explicitly with the element. It is frequently held in the different physical and logical headers.
  • Usually the elements of any static data structure are stored in a continuous manner in the memory, as it is held in a single section of memory.
  • Depending on the data structure definition all the descriptive information, except the physical location of the allocated element in memory is determined.
  • All the descriptive information, other than the physical location of the allocated element in memory, is determined by its data structure definition.
  • Relationships between the elements of any static data structure remain unchanged throughout the lifetime of the structure.

Static Data Structure vs Dynamic Data Structure

Let us now look at some differences between a static data structure and a dynamic data structure.

Static Data StructureDynamic Data Strcutre
The Static Data Structure has a fixed memory size, and its size cannot be randomly updated during the run time.The Dynamic Data Structure does not have any fixed memory size, and its size can be randomly updated during the run time.
Memory is allocated to the data structure during compile time. Fixed-size.Memory is allocated to the data structure dynamically that is, during run time, as the program executes.
Static Data Structure provides easier access to elements through their indexesDynamic Data Structure does not provide easier access to the elements. as the memory allocation is not in contiguous manners. However data structures like ArrayList is dynamic in nature and provide easy contiguous access through their indexes
In Static Data Structure along with the data, we do not need to store the memory address information of its following or previous elements. As the data is allocated in the memory in a contiguous manner, so we can directly access its following or previous elements with indexes.In Dynamic Data Structures like LinkedList we do need to store the memory address information of its following or previous elements. As the data is not allocated in the memory in a contiguous manner, so we cannot directly access its next or previous elements without the address information.
The memory allocation is fixed and so there will be no problem with adding and removing data items. Until and unless the given size doesn’t exceed the free contiguous space left in the memoryAs the memory allocation in the dynamic data structure is not fixed, it might throw an error of overflow if the limit is exceeded and we are trying to add any more elements to it. It will also throw an error of underflow if the dynamic data structure is empty, and we are trying to remove an element from it.
Can be very inefficient as the memory for the data structure has been set aside regardless of whether it is needed or not whilst the program is executing.The dynamic data structure uses the memory in the most efficient way, as it only uses the memory as much as needed.
Static Data Structure is not as flexible as Dynamic Data Structure.Dynamic Data Structure is not as flexible as Static Data Structure.
It is easier to program in the case of Static Data Structure as there is no need to check on data structure size at any point.In the case of Dynamic Data Structure, it is difficult to code as we need to keep the track of its size and its data locations all the time.
|Example : Arrays | Example : Linked List |

Advantages and Disadvantages of Static Data Structures

The following are the advantages of Static Data Structures:

  • A key advantage of static data structures is that as the memory allocation is fixed, we do not need to control or oversight the potential overflow or underflow issues when adding new items or removing existing ones. Until and unless the given size dosen’t exceeds the free contiguous space left in the memory.
  • It is very easy to program as there is no need to check on data structure size at any point.
  • Compiler and it allocates space during compilation.
  • Static Data Structure provides easier access to elements through their indexes.
  • It is also Easy to check for the overflow situation in the case of Static Data Structure.

The following are the disadvantages of Static Data Structures:

  • We have to evaluate the maximum amount of space that is needed to store the element in memory.
  • We cannot reallocate extra space in memory, once the Static Data Structure is initialized with a fixed size in the memory.
  • A lot of space might get wasted. If they remain unused in the Static Data Structure (if the size is too high).
  • Static Data Structure is not as flexible as Dynamic Data Structure.

Conclusion

In this article, we learned about Static Data Structure. Let us recap the points we discussed throughout the article:

  • A Data Structure is a collection of data, which helps in organizing those data and storing it in a particular manner, such that we can efficiently perform numerous operations on them.
  • Basically a data structure is of two types: Static Data Structures and Dynamic Data Structures.
  • Static Data Structure is an organization or collection of data in memory which have a fixed size, that is, it can store a limited amount of elements or data in it.
  • Dynamic Data Structures is an organization or collection of data in memory which do not have a fixed size, that is, its size can be modified during the operations performed on it and can store a variable amount of elements or data in it.
  • Arrays are a prominent example of a static data structure.
  • An array is a container object or a collection that stores a fixed amount of data of a similar type in it.
  • We discussed some features of static data structures.
  • We also have seen some differences between a static data structure and a dynamic data structure.
  • A key advantage of static data structures is that as the memory allocation is fixed, we do not need to control or oversight the potential overflow or underflow issues when adding new items or removing existing ones.
  • The disadvantage of static data structures is that a lot of space might get wasted. If they remain unused in the Static Data Structure.

Code with Precision! Enroll in Scaler Academy’s Online DSA Course and Master Efficient Algorithms.

Author