UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

10.7.  Implementation of data tables in C

After doing the complete modeling of an application, finally, for the non persistent data, a translation of the data into a specific code in the proper programming language is required, so that this code can be able to define the needed data structures to support these data. In addition, the functions in charge of the data manipulation must be implemented.

Usually, the same information can be stored in different ways, using the same programming language. For example, for the C programming language, the following methods can be used to store the non persistent information for a modeled table (for example for the photos or authors):

  1. With a static table of N elements. The problem of this solution is that the number of table elements is normally undefined, and with static memory, we are setting a maximum limit of N elements. If there would be more than these elements, then the application will not work correctly. Furthermore, if N is too big and the number of present elements is low, then more memory than needed would be used. On the contrary, an advantage of this solution is that the searching operations would be quicker than for example a linked list.

  2. With a continuous memory area that can change its size to fit the real number of elements that are in a moment. In this case, there will be a pointer that will point to the beginning of this continuous memory area. All the elements of the table are in continuous positions and there is not more memory than needed allocated, because the amount of memory is adjusted to the actual number of elements that there are. For it, the first allocation of memory will be done with a malloc, and the following add or remove operations will be done with a realloc in order to adjust the size of the allocated memory area to increase or decrease the amount of memory respectively.

  3. With a linked list, in which each table element is an element of the linked list. As the new elements are being added, they are included within the linked list; and as the existing elements are being removed, the memory areas of these elements are freed from memory from the linked lists, and the proper link updating is performed.

  4. With a hash table, having different linked lists. Each element of the modeled data table, will occupy one position in the hash table, according to the result of performing a hash function over one of the element fields. In this way, each one of the positions of the hash table will have its own linked list. This storage method implies that M linked lists are available (size of the hash table) instead of having only one linked list. Therefore, the searching times in the linked list are reduced at the prize of occupying more memory.

So far, this section has commented on possible implementations in C of different data tables. In addition, it is also interesting to think about how to implement relationships among tables, as the photo and author tables. Among the possible methods for implementing such a relationship, we can distinguishes the following, that can be also combined with the previous 4 methods that we have just presented:

  1. Relate a photo element with its author element through a table field that is an integer, which will be the same for the two elements.

  2. Relate a photo element with its author element through a pointer, in such a way that a field of the photo element will have a pointer to the beginning of the memory area where the corresponding author information is present. In this way, several photos will point to the same memory area that corresponds to their respective author. In this case, the AUTHOR_ID field will not exist within the author table, but its functionality would be replaced by such pointer. It is important to note that it is not always necessary that a specific data modeling matches exactly all its fields into a specific programming language, as it is the case.

As an example, next a figure shows a case in which photos as well as authors are stored in linked lists and how the relationship between both elements is done using pointers.

10.7.1.  Self-assessment questions

For each one of the sentences mark true or false

  1. In C programming language usually the non persistent information can be stored in different ways.

    • True

    • False

  2. The static tables allow to store an undefined number of elements.

    • True

    • False

  3. When information is stored in linked lists the relationships among list elements can not be stablished.

    • True

    • false