Contenido del Curso
C++ Intermediate | Mobile-Friendly
C++ Intermediate | Mobile-Friendly
Back to Memory
The memory on your computer is not just chaotic units. Everything has its unique logic, and knowing it can help you optimize memory consumption. We can work with addresses and pointers to travel around the memory, and as surprising as it may sound, we have already done this!
Starting with simple math, if you add to the pointer 1 of the pointer type, for example, int
or double
(it also means move 4 or 8 bytes forward), you will go to the next storage unit, 1 unit forward. Sounds interesting, but where can we use it? We don’t know if something is in the next or previous memory unit, so is it useless? Actually, no. It can be used by working with arrays. We are already familiar with them and now let’s learn a new rule: each array’s element is stored in the following storage cell. For example, the 5-element integer array:
Сells of elements go one after another since an array is a contiguous memory area. With each step, we move forward by 1 integer unit (4 bytes).
To sum up, we should know only the address of the first element of the array to work with all its elements.
¡Gracias por tus comentarios!