Table of Contents
Solve at lest the first thirteen problems of the given collection. If after solving them you sense you have unclear concepts about memory management, go to office hours with the professor. If you sense that the concepts are clear, solve the remaining problems.
For this activity, the following table will be used:
Aspect | C | Java |
---|---|---|
Call to obtain memory | ||
Memory allocation needs size | ||
More than one function available for allocation/creation | ||
Is allocated memory initialized? | ||
Call to destroy/liberate memory | ||
Call to change the size of a previously allocated block | ||
Is it possible to access beyond the limits of an allocated block? | ||
What happens when a pointer to an allocated memory block is lost (is no longer stored in any variable)? |
In groups of four people and during five minutes, complete the previous table. The answers must be obtained through consensus.
Each group answer are shared with the rest of the class to fill a common table.
A C program needs to manipulate a set of words. We are asked to implement this functionality and the following description of the requirements is given:
The application must manipulate several set of words separately.
The order and form in which these words are stored in the data structure is irrelevant for the rest of the application.
The operations that must be supported are:
Obtain an empty set.
Given a word, obtain a set only with that word.
Given a set and a word, obtain a new set containing the given word.
Given a set and a word, return a set without the given word. If the word does not exist, the set is not modified.
Destroy a given set of words.
All the operations receiving a word to be stored need to
create a duplicate with the system call strdup
. This
function receives as parameter a zero-terminated string of type
char *
and returns a string (of type char *
)
which is a duplicate of the given parameter in a memory location that
has been allocated with malloc
.
The proposed solution must be compact, that is, have a memory usage as reduced as possible.
In groups of four people, comment during five minutes the data structured proposed to manipulate these sets of words. Write its definition in C. Comment the proposed solution with the rest of the class. Derive a final proposal which will be the base for all the groups.
The team proposes the implementation of the function to return an empty set and the function to return a set with a single word. Check the proposed solution with the teacher.
The team proposes an implementation for the function that inserts a word in a set. Pay special attention to the data type that this function must return.
Write the definitions of the headers of the two remaining functions (the one that removes an element from a set and the one that destroys a set).
Let us suppose that we have written all the functions, now the rest of the application wants to use them, but as it was stated in the specification, the internal details of the data structure are irrelevant. How would you organize the code and the definitions to achieve this?
Implement one of the two remaining functions.