Memory allocation for pointers

Asked

Viewed 456 times

5

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

"Although it is possible to use them as vectors, pointers do not have their own memory. You can only use addressing via a pointer after it is pointing to an existing object."

However I was left with some doubts:
What does it mean that pointers do not have their own memory? If they do not have their own memory then they are allocated?

4 answers

5


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

Examples:

in C:

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

in C++:

int * a = new int[10];

the variable to is a pointer that stores the memory address for the 1st vector position. The rest of the positions are accessed by means of basic arithmetic, see this answer: /a/80097/3084

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

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

Updating

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 happens if you do so:

int * a = NULL;

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

Anyway, the 4 bytes of memory are spent anyway.

  • The syntax shown is not C. I think you meant int *a = malloc(10 * sizeof *a);

  • Thank you @pmg. I have included the two examples.

  • @pmg, edited. I switched sizeof *a (according to its edition), to sizeof(int). In this case, take sizeof(int) and multiply by 10.

  • Hmmm ... *a has kind int, so much so sizeof *a as sizeof (int) are correct. I prefer to use the object itself to, in case of changing the type, only need to change the code in one place: double *a = malloc(10 * sizeof *a); vs double *a = malloc(10 * sizeof (double));

  • Ahh ok! You’re right! It really avoids two changes. I’ll change. Thanks again.

  • 1

    It just needs to be clear that this consumption of the 4 bytes (8 on 64 bit architectures) is relative. There is consumption but no direct allocation. If it is in the stack this is already allocated. If it is in an area of heap, it has already been allocated by the object containing it.

  • Thanks @bigown, I even started writing the memory area where usually the pointer is stored (stack), but I think it deserves to be better explored and I won’t have time now. As soon as I have time I’ll update.

Show 2 more comments

5

The book in question misleads.

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

  • I swear I didn’t understand what you said

  • @Luiz Vê a definition of object in the Standard (translation is mine) "data storage region whose content represents values". The pointers are objects. The book quote ("the pointers have no memory of their own") is misleading.

2

In fact 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 allocated object, this object yes can receive common types.

-1

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

Imagine the memory as a whole, many and many bytes there. The pointer is 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 the objects, the objects are where it points. It points to the first position of 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(ex:20) and the vector points to the first position of memory occupied by the object(ex:a positive integer between 0~2{elevated to something}) adding one more in this 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
}
  • The pointers themselves sane objects. The code shown is not C. In C, an array nay is a pointer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.