Sometimes we already know the amount of information we are going to store
when we implemented our system. We can have, for example,
10 whole numbers or 500 records with information about our friends. Other times, however, the amount of information to be stored can not be predicted a priori, that is, before our system is used in the practice. If we have not predicted a sufficient number of variables or a sufficient size for our data structures (such as vectors or matrices), we will not have room to store all the information. Imagine that our client or the user of our system can not register a new product or run a sale because we did not anticipate a size enough for our data structures. He would not be happy, would he? On the other hand, imagine that, in order not to take this risk, we always declare our data structures with very large sizes. In this case, they would require a lot of memory, possibly more memory than available. Complicated situation, no? Luckily there are data structures dynamics, which are those that we build on demand, that is, we occupy the memory as it is needed to store the data. Among the dynamic data structures, we can mention the linked lists, the rows, cells, trees and graphs. Some of these structures may also be implemented with conventional data structures, which we will call them static. 6. Dynamic allocation 125 In this chapter, we will learn how to reserve (allocate) memory dynamically (on demand) to build these data structures. Come on! Attention In static data structures, the allocated memory has a default size. Even if we do not store anything, the placeholder can not be used to store other information. And if we need more space, this will not be possible; to do so, we would need to reprogram and recompile the program. In dynamic data structures, memory is allocated as necessary to store the data, that is, it is allocated "at runtime". An important question is whether we should use data structures static or dynamic. When we know in advance how many variables or how many values ??we will store, we use static variables. We can also use them if we know that the maximum amount of data we will have to store it will be small (at most 100 values, for example). On the other hand, if we do not know the amount of data and if this quantity is large, we usually use dynamic variables. We propose below some situations for you to identify how many variables are needed and what their size is (for example: two variables integers, or a vector of integers with 20 positions). When it is not possible know the amount of data, answer "dynamic variables". 1. Store the sizes of the sides of a triangle. 2. Identify the largest of 10 real numbers. 3. To store the data structure tests of the students of the your class. 4. Calculate and print the average grade for each student. 5. Read and store integer values ??until the read value is -1. 6. Get the temperature (of a sensor) every hour and calculate the highest and the lowest temperature of the day. 7. Save the highest and lowest temperature of each day. 8. Store the name and an email of no more than 1,000 friends. 9. Store the name and an email of all friends. Can you identify? Have you been in doubt about many items? Let's learn a bit more to eliminate doubts and use the memory allocation. 126 Data Structure and Programming Techniques Knowing the theory to program The variables we have used so far (static) are stored in the global variables or in the process stack (our program, when is being executed), being identified and accessed by their names. Like this, we can have two integer variables, i and j, and a string named name with 20 characters, for example. Dynamic variables, on the other hand, are stored in the heap of the process. Because we do not know how much information we are going to we can give her names. How can we access them, then? In that In this case, the access is done through pointers, that is, their memory. Remember if Most variables store values ??(integers, characters, real numbers, ...). a pointer is also a variable, but instead of storing the value of a given, stores a memory address. In Chapter 14 of Algorithms and Computer Programming, our author, you will find details about the pointers. To gain memory space while the program is running (we call this memory allocation), we mainly use the malloc command, which is present in the stdlib.h library. The malloc command has only one parameter, which defines how many bytes will be allocated. Like this, malloc (4), in Code 6.1, allocates 4 bytes, and malloc (20) allocates 20 bytes in the area