UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

Dynamic memory management in C

Exercicises

1.  Memory management functions in C

Work Plan

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

How long did this activity take you? mins.

2.  Memory management in C and Java

Resources

  • 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)?

Work Plan

In groups of four people and during five minutes, complete the previous table. The answers must be obtained through consensus.

Assessment

Each group answer are shared with the rest of the class to fill a common table.

How long did this activity take you? mins.

3.  Word Set Management

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:

    1. Obtain an empty set.

    2. Given a word, obtain a set only with that word.

    3. Given a set and a word, obtain a new set containing the given word.

    4. Given a set and a word, return a set without the given word. If the word does not exist, the set is not modified.

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

Work Plan

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

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

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

  4. 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).

  5. 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?

  6. Implement one of the two remaining functions.

How long did this activity take you? mins.