Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coding problem to solve: Delete a dynamic array of structures Objective: Write a function ` delete _ present _ list ` to free all memory

Coding problem to solve: Delete a dynamic array of structures
Objective: Write a function `delete_present_list` to free all memory allocated for an array of structures containing dynamically allocated strings and return a null pointer in c
Function Signature to use for the program:
struct present* delete_present_list(struct present* present_list, int num);
Structure Definition:
struct present {
char *present_name;
float price;
}
Edge Cases, must account for all:
Null Pointer for present_list: When passed to the function, the present_list is NULL. The function should gracefully handle this case by returning NULL immediately.
Zero Presents (num =0): The num parameter is 0, indicating no presence in the list. The function should handle this case without attempting to access or free any memory.
Null Pointers within present_list: Some present_name pointers within the present_list array are NULL. The function should check each present_name before attempting to free it.
Memory Allocation Failures (Hypothetical): consider what will happen if present_name allocations had failed during the list's creation. This might require handling NULL pointers within the array.
Criteria for code, must include all:
Correct function signature and parameter types.
Correctly handles the case where present_list is NULL
Properly iterates through the array to access each structure
Correctly frees each dynamically allocated string in the array
Correctly frees the dynamically allocated array
Returns `NULL` after freeing all allocated memory.
Clear, well-commented, and properly formatted code
Handles edge cases, such as an empty array or `NULL``present_name` pointers within the array.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions