Memory allocation for pointers

5

I've been reading and studying pointers and I came across the following quote in a book:

"Although it is possible to use them as vectors, pointers do not have their own memory. Pointing can only be used after a pointer is pointing to some object already in existence."

But I had some doubts:
What does it mean to say that pointers have no memory of their own? If they do not have their own memory, where are they then allocated?

    
asked by anonymous 17.08.2015 / 12:37

4 answers

5

The pointer is a variable like any other, but it stores a memory address that references another memory location where the content is.

Examples:

in C:

int * a = malloc(10 * sizeof *a);

in C ++:

int * a = new int[10];

The variable a is a pointer that stores the memory address for the 1st position of the vector. The rest of the positions are accessed through basic arithmetic, see this answer: link

Pointers have 4 bytes in a 32-bit architecture and 8 bytes in a 64-bit architecture. Note that they will always be the same size, no matter what type of variable they point to. So a pointer to char is the same size as a pointer to double.

Considering a 32-bit architecture, in the above example, the variable a is a 4-byte pointer pointing to a vector that uses 10 * 4 = 40 bytes of RAM (a < int in a 32-bit architecture also uses 4 bytes of memory to be represented).

Update

Even when you just declare the pointer (without pointing it to any memory address), as below:

int * a;

The same 4 bytes of RAM are used. However, in this case, the vector is pointing to a random memory address.

The same thing happens if you do this:

int * a = NULL;

however, in this case, the vector is pointing to address 0.

Anyway, the 4 bytes of memory are spent anyway.

    
17.08.2015 / 13:04
5

The book in question is misleading.

Pointers are objects that need storage space such as "normal" objects.

    
17.08.2015 / 13:18
2

Actually what the author tried to say is that in a pointer it is not possible to store a value of an integer type, character, real ... because in fact a pointer has a space in memory, but to store the address ( of memory) of an object allocated, this object can receive common types.

    
17.08.2015 / 13:09
-1

In fact it does, but it is not quite like the other objects.

Imagine the memory as a whole, many and many bytes there. The pointer is nothing more than just one of those bytes with a value there, and that value is nothing more than a memory address. Exactly as if he were pointing to another place.

The pointers themselves are not objects, objects are where they point to. It points to the first position in the memory area. To access the following is very easy, we have the address of the first position ... If you know the size of the object (eg 20) and the vector points to the first position of memory occupied by the object (eg a positive integer between 0 ~ 2 {raised to something}) by adding one more in that space, you get the next ex:

auto main() -> int
{
    int[10] arr = {0,1,2,3,4,5,6,7,8,9};
    int * arr_prt = arr; // arr em si ja é um ponteiro, por isso é possivel 
                         // essa atribuição
    arr[5] = 7;   //<-isso 
                  //é equivalente
    *(arr_prt+5) = 7 //<-isso
}
    
23.05.2016 / 23:54