Mar 24, 2022

Array data structure

There are two primary ways to make an array in the memory. the first one is the most familiar one which is using the stack. Actually, when you are declaring an array like this you may have no idea where you are storing data. But if you are declaring a local variable it is stored on the stack.

int main(int argc, char const *argv[])
{
    int numbers[5];

    return 0;
}

The second method to make an array is allocating the memory from the heap as follows. Actually, the stack space is limited. It is optimized to use with functions. The primary purpose of the stack is to use with function calls. So if we are going to need a large memory space the ideal area is the heap. We can allocate some memory space on the heap using the new keyword in C++.


int main(int argc, char const *argv[])
{
    int *numbers = new int[10];

    return 0;
}

Accessing elements of an array

sf

Arrays and Pointers

hgf

Functions and Arrays

sdf

 

Pros and Cons of Arrays

aa

Extending the size of an array

We saw that the size of an array is fixed. We cant extend that natively using the built-in programming language methods. So we have to use an alternative method to do it.

For example, assume that we have created an array with the size of 4 and we want to extend it o the size 10.

extend-array-size

What we are going to do is create a new array with the size 10 and copy all elements from the previous one. So the new array will look like the extended array of the previous one. The following code does the job for us. Let's read it and see what it does.

int main(int argc, char const *argv[])
{
   int *numbers = new int[4] = {2, 3, 5, 8};
   int *temp  = new int[10];

   for(int i; i < 4; i++) temp[i] = numbers[i];
    
   delete[] numbers;

   numbers = temp;

   return 0;
}

The first line of the main function is just how we are creating our original array. Notice that its size is 4 and it holds some integers like 2, 3, 5and 8.

The next few instructions are the actual procedure of extending the array.

First, we make a new array with size 10. It is n empty array at the moment. Even the pointer's name is temp don't understand it in the wrong way that the new array we are declaring is a temporary array. It is the one we are going to use in the future. But the pointer we are making is a temporary one.

Next, we use a for loop and copy all items from the old array to the new array until the limit of the original array.

Append an item to the array

sdf

append-item-to-array

sd

int main(int argc, char const *argv[])
{
    int *numbers = new int[4] = {2, 3, 5, 8, 16};

    int *temp  = new int[5];

    for(int i; i < 4; i++) temp[i] = numbers[i];

    delete[] numbers;

    numbers = temp;
    numbers[5] = 24;

    return 0;
}

fs

df

sdf

Insert new item at an index

Let's say we want to insert the integer value 35 at index 4. So how we can do it? can we use the statement numbers[4] = 35? No, if we d it like that the current value will be overwritten. At the moment the integer with value 7 is on the array. What we want to do is insert a new value at index 4 and extend the array. So all items after that index should be shifted to the right.

insert-array-element

sdf

sdf

int main(int argc, char const *argv[])
{
    int *numbers = new int[4] = {2, 3, 5, 8};

    int *temp  = new int[10];

    for(int i = 0; i < 4; i++) temp[i] = numbers[I];
    temp[4] = 35;
    for(int i = 5; i < 9; i++) temp[i + 1] = numbers[i];
    
    delete[] numbers;

    numbers = temp;

    return 0;
}

fddgffg

dffg

Multidimensional Arrays 

Jun 21
Protostar Stack1 Tutorial

In previous tutorial I completely explained how to exploit protostar stack 0 vulnerable program. In....

Mar 12
C programming file descriptors

File access is an essential feature of any programming language. In C we can use two methods to....

Aug 12
What is a cookie?

If you are working with web applications you may have heard about cookies. Also when we browsing....

Replying to 's comment Cancel reply
ABOUT AUTHOR
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.

CATEGORIES
SOCIAL
RANDOM ARTICLES