What exactly do pointers store? (C ++)

I know that pointers store the address of the value they are pointing to, but if you pointed the pointer value directly to the screen, you will get a hexadecimal number. If a number is exactly what the pointer holds, then, saying

pA = pB; //both are pointers 

you copy the address. Then there would be no big overhead for using pointers when working with very small elements like int and bool s?

+7
c ++ pointers overhead
source share
7 answers

A pointer is essentially just a number. It stores the address in RAM, where the data is located. The pointer itself is quite small (probably the same size as int on 32-bit architectures, long is 64 bits).

You are right, although int * will not save space when working with int s. But this is not the main thing (pun intended). There are pointers so you can reference things, not just use the things themselves.

+13
source share

Addresses of memory.

These are places in memory where there are other things.

Pointers are usually the size of a processor word, so they can usually be moved around in a single instruction loop. In short, they are fast.

+3
source share

As others have said, a pointer stores a memory address, which is "just a number", but this is an abstraction. Depending on the architecture of the processor, there may be more than one number, such as base and offsets, that must be added to dereference a pointer. In this case, the service data is slightly higher than if the address is a single number.

Yes, there is overhead when accessing an int or bool through a pointer vs. directly where the processor can put the variable in a register. Pointers are usually used where the value of indirection outweighs any overhead, i.e. Moving an array.

I meant temporary overhead. Not sure if OP is more concerned about space or time.

+3
source share

A number refers to its address in memory. The size of the pointer is usually the proper size of the computer architecture, so there is no additional overhead compared to any other primitive type.

+1
source share

Some architectures have the additional overhead of character pointers, since the architecture only supports addressing (32- or 64-bit values). Therefore, a pointer to a symbol is stored as the address of the word and the offset of the symbol inside this word. De-linking to a pointer involves fetching a word, and then shifting and masking its value to extract a character.

+1
source share

The address in memory. Points somewhere !:-)

0
source share

Let me start with the basics. First of all, you need to know which variable and how they are used.

Variables are basically memory cells (usually containing some values), and we use some identifier (i.e. variable names) to refer to this memory cell and use the value present in this place.

To understand this better, suppose that we want information from memory cells to be present in some place relative to the current variable. Can we use an identifier to retrieve information from neighboring cells? Not. Since the identifier (variable name) will only give the value contained in this particular cell.

But, if somehow we can get the memory address in which this variable is present, we can easily move to nearby locations and use their information (at run time).

Here the pointers are included. They are used to store the location of this variable so that we can use additional address information if necessary.

Syntax:. To save the address of a variable, we can simply use the & (address) operator.

 foo = &bar 

Here foo stores the address of the variable bar.

Now, if we want to know the value present at this address?

To do this, we can simply use the * (dereference) operator.

 value = *foo 

Now, when we need to save the address of the variable, we need the memory in the same way as we need in the case of the variable. This means that pointers are also stored in memory in the same way as other variables, therefore, as in the case of variables, we can also store the pointer address in another pointer.

0
source share

All Articles