Dynamic Memory Allocation and Variable Scope in Programming

 
 
Dynamic Memory
Allocation
 
 Scope
 Dynamic memory Allocation
 Malloc
 Calloc
 Realloc
 Alternative to Dynamic Memory
 
Md. Jakaria
Lecturer
Dept. of CSE, MIST
 
Three places
 
Scope of a Variable
 
 
Scope of n and m
 
Scope of a Variable
 
Scope of g
 
Scope of a Variable
 
Local variables are 
destroyed
 when they go 
out-of-scope
 
Lifetime of a local variable
 
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)
 
Reference to Local
 
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
 
Dynamic Memory Allocation
 
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
 
The 
malloc
 function
 
Dynamic allocation of
Array
Struct
Factory Methods
 
Usage of malloc function
 
When is it destroyed?
 
Out-of-Scope
 
Not destroyed automatically
(
Memory leak
)
 
Destruction of Dynamic Memory
 
void free(void *ptr)
 
When the outside (dynamic) memory is no longer needed
 
The 
free 
function
 
void free(void *ptr)
 
When the outside (dynamic) memory is no longer needed
 
free(      )
 
The 
free 
function
 
void *calloc(size_t nitems, size_t size)
 
Same as malloc
 
The following two lines produces similar allocation
 
How many items?
 
Size of each item
 
The 
calloc
 
function
 
void *calloc(size_t nitems, size_t size)
 
Initializes every bit to zero
Slower than malloc
 
The 
calloc
 
function
 
void *realloc(void *ptr, size_t size)
 
 
Either memory is extended,
Or
 
Previous items are copied to new larger location
 
For resizing existing dynamic memory
 
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
 
The 
realloc
 
function
 
void *realloc(void *ptr, size_t size)
 
Existing pointer
 
New size
 
The 
realloc
 
function
 
-
Global variables (Scope is increased)
-
Static variables (Life-time is increased)
 
Alternative to Dynamic memory
 
Automatically initialized
 
Global Variable Initialization
 
Automatically initialized
 
Credit: https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
 
Global Variable Initialization
 
In case of same name, local variable takes preference
 
Variable Shadowing
 
Prevents local variable from being destroyed until the program terminates
 
The 
static
 Keyword
 
Prevents local variable from being destroyed until the program terminates
 
The 
static
 Keyword
 
https://www.hackerearth.com/practice/notes/memory-layout-of-c-program/
 
Memory Layout of a C Program
 
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 1
 
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 2
 
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.
 
Task 3
Slide Note
Embed
Share

This content delves into the concepts of dynamic memory allocation, variable scope, and the lifetime of local variables in programming. It explores the scopes of variables in different contexts and addresses issues like dangling pointers and out-of-scope references. The images provided visually enhance the understanding of these crucial programming concepts.

  • Memory Allocation
  • Variable Scope
  • Programming Concepts
  • Dangling Pointers
  • Dynamic Memory

Uploaded on Jul 20, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Dynamic Memory Allocation Scope Dynamic memory Allocation Malloc Calloc Realloc Alternative to Dynamic Memory Md. Jakaria Lecturer Dept. of CSE, MIST

  2. Scope of a Variable Three places

  3. Scope of a Variable Scope of n and m

  4. Scope of a Variable Scope of g

  5. Lifetime of a local variable Local variables are destroyed when they go out-of-scope

  6. Reference to Local Pointer referring to expired local variable

  7. Reference to Local Pointer referring to expired local variable

  8. Reference to Local Pointer referring to expired local variable (Dangling Pointer)

  9. Reference to Local Pointer referring to expired local variable (Dangling Pointer)

  10. Dynamic Memory Allocation The concept Out-of-Scope

  11. Dynamic Memory Allocation The concept Out-of-Scope

  12. Dynamic Memory Allocation The concept Out-of-Scope Out-of-Scope

  13. Dynamic Memory Allocation The concept Out-of-Scope Out-of-Scope

  14. Dynamic Memory Allocation The concept Out-of-Scope Out-of-Scope

  15. Dynamic Memory Allocation The concept Out-of-Scope Out-of-Scope

  16. Dynamic Memory Allocation The concept Out-of-Scope Out-of-Scope

  17. 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

  18. The malloc function

  19. Usage of malloc function Dynamic allocation of Array Struct Factory Methods

  20. Destruction of Dynamic Memory When is it destroyed? Out-of-Scope Not destroyed automatically (Memory leak)

  21. The free function When the outside (dynamic) memory is no longer needed void free(void *ptr)

  22. The free function When the outside (dynamic) memory is no longer needed void free(void *ptr) free( )

  23. 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

  24. The calloc function void *calloc(size_t nitems, size_t size) Initializes every bit to zero Slower than malloc

  25. 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

  26. The realloc function void *realloc(void *ptr, size_t size) Existing pointer New size

  27. The realloc function void *realloc(void *ptr, size_t size) Existing pointer New size

  28. The realloc function void *realloc(void *ptr, size_t size) Existing pointer New size

  29. The realloc function void *realloc(void *ptr, size_t size) Existing pointer New size

  30. The realloc function void *realloc(void *ptr, size_t size) Existing pointer New size

  31. Alternative to Dynamic memory - Global variables (Scope is increased) - Static variables (Life-time is increased)

  32. Global Variable Initialization Automatically initialized

  33. Global Variable Initialization Automatically initialized Credit: https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm

  34. Variable Shadowing In case of same name, local variable takes preference

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

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

  37. Memory Layout of a C Program https://www.hackerearth.com/practice/notes/memory-layout-of-c-program/

  38. 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.

  39. 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.

  40. 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.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#