[go: up one dir, main page]

0% found this document useful (0 votes)
11 views11 pages

Dynamic Data Stucture

The document discusses dynamic data structures, focusing on dynamic memory allocation, including dynamic arrays and structs. It explains the use of 'new' for memory allocation and 'delete' for deallocation, emphasizing the importance of managing memory to prevent leaks and errors. Key practices include checking for allocation failure, freeing memory correctly, and avoiding dangling pointers.

Uploaded by

Althea Rhien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Dynamic Data Stucture

The document discusses dynamic data structures, focusing on dynamic memory allocation, including dynamic arrays and structs. It explains the use of 'new' for memory allocation and 'delete' for deallocation, emphasizing the importance of managing memory to prevent leaks and errors. Key practices include checking for allocation failure, freeing memory correctly, and avoiding dangling pointers.

Uploaded by

Althea Rhien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Dynamic Data Structures

Dynamic Data Structures


Dynamic Data Structures
Dynamic Data Structures
Dynamic Data Structures
Dynamic Data Structures
Dynamic Data Structures
Dynamic Data Structures
Presentation Contents
• Dynamic Allocation
• Dynamic Arrays
• Dynamic Structs
Dynamic Memory Allocation

• Fundamental concept in programming that


allows you to allocate and deallocate memory
during runtime.
• Create data structures of variable sizes,
providing flexibility in managing memory
resources efficiently.
• Performed using new and delete operators.
new

Used for dynamically


allocating memory for an
object or array of
objects.
Returns a pointer to the A memory for an integer was allocated
allocated memory block. using new int. the allocated memory
was freed using delete dynamicInt.
delete

Used for deallocating


memory previously
allocated with new.
It frees the memory and
destroys any objects in A memory for an integer was allocated
the allocated memory using new int. the allocated memory
block. was freed using delete dynamicInt.
Dynamic Allocation
• It is essential to allocate memory when
needed, and deallocate when it is no
longer required.
• Failure of deallocation leads to memory
leaks.
• To deallocate memory, use the delete
operator, passing the pointer to the
dynamically allocated memory block as
an argument.
Handling Memory Errors and Leaks
• Check for Allocation Failure: After allocating memory
using new, always check if the returned pointer is nullptr.
• Free Memory Correctly: Make sure to free dynamically
allocated memory using the delete operator when it is no
longer needed.
• Avoid Dangling Pointers: After freeing memory, avoid
using the corresponding pointer.
• Use delete[]for Arrays: When dynamically allocating an
array with new[], make sure to deallocate it using delete[].
Dynamic Memory Allocation
For Arrays
• An integer array with the size
specified by the user was
dynamically allocated.
• The user will enter the size, and the
memory is allocated using the new
int[size].
• The elements of the dynamic array
are assigned values and printed.
• The allocated memory is freed
using delete[]dynamicArray.
For Structs
• We dynamically allocate memory
for a Person struct using new
Person.
• The fields of the struct are
assigned values, and the values
are printed.
• Finally, the allocated memory is
freed using delete person.
Thank you!

You might also like