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

Week 3

Uploaded by

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

Week 3

Uploaded by

theeeclipse17
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Dynamic Memory

Allocation

● Scope

● Dynamic memory Allocation

● Malloc

● Calloc

● Realloc

● Alternative to Dynamic
Memory
Scope of a Variable

Three places
Scope of a Variable

Scope of n and m
Scope of a Variable

Scope of g
Lifetime of a local variable

Local variables are destroyed when they go out-of-scope


Reference to Local

Pointer referring to expired local variable


Reference to Local

Pointer referring to expired local variable


Reference to Local

Pointer referring to expired local variable (Dangling Pointer)


Reference to Local

Pointer referring to expired local variable (Dangling Pointer)


Dynamic Memory Allocation

The concept

Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope Out-of-Scope
Dynamic Memory Allocation

The concept

Out-of-Scope Out-of-Scope
The malloc function

void * malloc (size_t size);

Memory required in byte


e.g. sizeof (int)

Returns pointer to the beginning of the block

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit. This type is
used to represent the size of an object.
• https://en.wikipedia.org/wiki/C_data_types#stddef.h
• https://stackoverflow.com/questions/2550774/what-is-size-t-in-c
The malloc function
Usage of malloc function

• Dynamic allocation of
• Array
• Struct
• Factory Methods
Destruction of Dynamic Memory

When is it destroyed?

Out-of-Scope

Not destroyed automatically


(Memory leak)
The free function

When the outside (dynamic) memory is no longer needed

void free(void *ptr)


The free function

When the outside (dynamic) memory is no longer needed

void free(void *ptr)

free( )
The calloc function

Same as malloc
void *calloc(size_t nitems, size_t size)

How many items?Size of each item

The following two lines produces similar allocation


The calloc function

void *calloc(size_t nitems, size_t size)

• Initializes every bit to zero


• Slower than malloc
The realloc function

For resizing existing dynamic memory


void *realloc(void *ptr, size_t size)

Existing pointer New size

Either memory is extended,


Or Previous items are copied to new larger location
The realloc function

void *realloc(void *ptr, size_t size)

Existing pointer New size


The realloc function

void *realloc(void *ptr, size_t size)

Existing pointer New size


The realloc function

void *realloc(void *ptr, size_t size)

Existing pointer New size


The realloc function

void *realloc(void *ptr, size_t size)

Existing pointer New size


The realloc function

void *realloc(void *ptr, size_t size)

Existing pointer New size


Alternative to Dynamic memory

- Global variables (Scope is increased)

- Static variables (Life-time is increased)


Global Variable Initialization

Automatically initialized
Global Variable Initialization

Automatically initialized
Variable Shadowing

In case of same name, local variable takes preference


The static Keyword

Prevents local variable from being destroyed until the program terminates
The static Keyword

Prevents local variable from being destroyed until the program terminates
Memory Layout of a C Program
Task 1

Create a resizable array of integers with the following options

1. Add a new number to the array


2. Display all numbers in the array
3. Delete an existing integer (by index)

The storage should be flexible so that no memory is wasted.


Task 2
Task 2
Design a record book that will hold the name, id and total marks of a
student. The following options should be available.

1. Add a new record


2. Display all records
3. Edit an existing record
4. Delete an existing record
5. Search for a record by name or id

The storage should be flexible so that no memory is wasted.


Task 3
Task 3
Integrate Persistent storage (File) in Task 2, i.e. all the contents of the
array should be written to file at the end of the program, and it
should load all the contents from the file at the beginning of the
program.

You might also like