Today I was running one of my first C programs, and in the middle of it, I used a two-dimensional array. This time, after thinking about the syntax that is often learned without really understanding the execution logic, in this case, memory allocation at runtime. I come across an idea that seems somewhat confusing and that I share with the intention of checking if what I think is wrong and in this way there may be some correction that "lights" me on this matter.
I understand from what I have learned that a pointer is a variable that contains a memory address and allows access to it. This address can contain constant values or other variables. At this point, a pointer can point to another pointer because this pointer is also a variable, whose variability is determined by the memory location it points to.
Now, from here on I feel less sure of what I say and that is what I have doubts about.
When a pointer points to an array, it really does so to the first value, from which it is assumed that the successive elements of the array, being stored in contiguous positions, are modifiable when accessing with,
puntero[n]
taking into account that itn
is the magnitude of the displacement from the variable that the pointer actually points to.
About what I am saying I wonder:
- Is this correct?
- Do all arrays end with \0 or is this something unique to character arrays?
Continuing with the doubts, when assigning dynamic memory to a pointer.
- What really happens?
As far as I know, a dynamic memory pointer is a way to create an array and, with the logic of the previous paragraph, the pointer still points to a single memory location, which in this case is the first element of the array, so I conclude that what is really going on with the realloc()
. It is that the compiler is told
"This free zone memory can be used"
But in reality the nature of the pointer is not changing, since it continues to point to the first element and we move to the adjacent memory in the same way as was said before, with the difference that without allocating the memory this would not be possible because it is not available to use.
- Again, is this correct?
All in all, I get to what made me think of all this. It could be said that when declaring a two-dimensional array, if we see it as a matrix, the first array, of the same nature as the one described above, which determines the "rows" of the matrix, contains in turn pointers that point to the first element of other arrays. With which I think that the memory assigned to a two-dimensional array is only contiguous in two cases:
- The first, when we refer to the array that determines the "rows" of our matrix.
- Second, when we refer to each array pointed to by the pointers contained in the first array.
Therefore, the entire two-dimensional array would not be contiguous on the heap, but there would be a bunch of contiguous fragments randomly distributed on the heap, related to each other thanks to the pointers. I find this detail very fascinating and curious, to finish I must ask:
- What I say again, is correct?
It would be interesting to know that they can correct me or add something to what I say.