0% found this document useful (0 votes)
17 views

Data Structures Notes

The document discusses different types of variables in C/C++ and how they are stored. Local/automatic variables are stored in the stack, global/static variables are stored in the data segment, and dynamic variables are stored in the heap via functions like malloc. Arrays can also be created dynamically in the heap. The garbage collector frees up memory that is no longer accessible.

Uploaded by

Sikander Saqlain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Data Structures Notes

The document discusses different types of variables in C/C++ and how they are stored. Local/automatic variables are stored in the stack, global/static variables are stored in the data segment, and dynamic variables are stored in the heap via functions like malloc. Arrays can also be created dynamically in the heap. The garbage collector frees up memory that is no longer accessible.

Uploaded by

Sikander Saqlain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Type and scope of variables:

-Variables are stored in one of three places depending on their type. The Stack
segment, Data segment or Heap.
-Local/Automatic variables are stored in the Stack segment
-Global/Static variables are stored in the Data segment (Even ones that are
created inside a function)
-Dynamic variables are stored in the Heap

In C# and Java, arrays are basically objects of a class and they are stored in the
Heap. Any variable where we use the new keyword gets stored in the Heap,
whether that’s a single variable or an array.

To create dynamic variables in C or C++ we have a different syntax. In C we use


the keyword malloc to allocate memory in the heap. We need to use a pointer
variable to do this.

First, we declare a pointer int* p. This variable will be stored in the Stack
Segment:
Then, we define the variable by writing
p = (int *) malloc(sizeof(datatype). What this will do
is allocate the specified amount of memory in the
Heap and the memory address will be stored in p.
So, the variable itself is in the Stack Segment and it
Holds the address of the memory which is allocated in
The Heap

Now, memory allocated to p is empty and


doesn’t hold any data. To initialize it
we write *p = 15

Declaration: int* p
Definition: p = (int *) malloc(sizeof(datatype))
Initialization = *p = 15

The malloc function returns a void pointer (*void) and takes an unsigned(+ve)
integer as its input. A void pointer is a pointer that can hold the address of any
variable type. That’s why we have to type case the return value by writing
(datatype *) else it will give an error.
Note: A pointer that has not been initialized is called a dangling pointer and
points to a random memory location. Using it before initialization will crash the
program

We can use this method to define an array in the Heap as well.


Int * list = (int *) malloc(sizeof(int) * 4)
This will create an array of size 4 and the Base address will be stored in the list
variable:

The difference between using this method and simply writing int data[4] is that
the above method creates a dynamic array in the Heap while int data[4] creates
a static array in the Stack segment.

To access the elements in the array


we can simply write list[ i ]
The max memory space for the Heap, Data Segment and Stack in a 32-bit
system is 2 GB. The advantage of using C rather than C# or Java is that we can
make both static and dynamic arrays in C and make our program more efficient.
Garbage Collector:

When the above code is run, the following memory is


allocated:
A dynamic array is created and the address is stored in
funlist
When the program exits fun and returns to main, the
memory that was allocated to tlist and funlist will be
freed
Now, the memory in the Heap that was allocated to
funlist has no way of being accessed. If we create another array using malloc, it
will allocate new memory, not the memory that was previously allocated to
funlist
This occurrence is called a memory leak.

The job of the garbage collector is to scan the memory and check to see if there
is any allocated memory in the Heap that can’t be accessed through a variable in
the Stack or Data Segment. If it finds any allocated memory that cant be
accessed it will free it.
In C# and Java this is done automatically but in C/C++ we have to do it manually
using the delete(only C++) keyword or the free function

It is necessary to specify the size of a static array before runtime however for
dynamic arrays we can do it at runtime too

2-D dynamic arrays:

This line creates an array of pointers


Then using a for loop we create a dynamic array at
each pointer address

To create a jagged array, we will need to take the colCount as an input in each
iteration of the loop

You might also like