K
K
Kirill2021-11-05 16:21:59
C++ / C#
Kirill, 2021-11-05 16:21:59

What are pointers in C++?

Hello. I recently read an article about pointers on the site. They explained to me what pointers are and what they are used for.

Now I will quote the text from the site:

When executing any program, all the data necessary for its operation must be loaded into the computer's RAM. To access variables in memory, special addresses are used, which are written in hexadecimal form, for example, 0x100 or 0x200.

If there are too many variables in memory that the hardware itself cannot accommodate, the system will overload or freeze.

If we declare variables statically, as we did in previous lessons, they remain in memory until the program exits, and then are destroyed.

This approach may be acceptable in simple examples and simple programs that do not require a lot of resources. If our project is a huge software package with high functionality, declaring variables in this way, of course, would be rather unwise.

Can you imagine if the infamous Battlefield 3 used this method of working with data? In this case, the most avid gamers would have to reboot their heavily loaded systems with the reset button after a few seconds of the game.

The fact is that when playing the same Battlefield, the gamer sees various objects on the monitor screen at each new moment of time, for example, now I am shooting at the enemy, and in a split second he already falls dead, creating a lot of special effects around him, such as dust, shadows, etc.

Naturally, all this takes up some space in the computer's RAM. If you do not destroy unused objects, very soon they will fill the entire amount of PC resources.

For these reasons, most languages, including C/C++, have the concept of a pointer. A pointer is a variable that stores the address of a RAM cell, such as 0x100.

We can access, for example, an array of data through a pointer that will contain the address of the beginning of the range of memory cells that store this array.

After this array is no longer needed for the rest of the program, we simply free the memory at the address of this pointer, and it becomes available again for other variables.


My friend said that this is all garbage and they are talking nonsense. I'm a little confused. Can you express your version about pointers, please!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mercury13, 2021-11-05
Tritonov @tritonov01

If there are too many variables in memory that the hardware itself cannot accommodate, the system will overload or freeze.

Either it will be the wildest swap, or a failure with a lack of memory. And how this failure is arranged - through a "fall", freezing, some kind of message ... - it can be different.
Can you imagine if the infamous Battlefield 3 used this method of working with data? In this case, the most avid gamers would have to reboot their heavily loaded systems with the reset button after a few seconds of the game.

No. The memory would have been allotted with a margin already at launch, and even a small game would require many, many gigabytes of memory, and Battlefield would simply not run even on top PCs. And part of the quality settings would not make sense - initially, already when loading the EXE, memory was allocated with a margin and you can’t shrink in any way. But every cloud has a silver lining - half of the glitches and crashes would not exist. So small games that can run on 99% of modern and not very PCs and therefore do not have quality settings use this mechanism quite well.
Allow me to promote - I am writing the Unicode program”, an encyclopedia of symbols - a table with 140 thousand positions (the number of symbols currently existing) occupies most of the EXE's. A table for 1.1 million (Unicode capacity) eight-byte pointers is also allocated statically, but it does not take up space in the EXE and is filled at the start. Thus, we can easily go over all the characters, and we can find its entry by the character code.
If you do not destroy unused objects, very soon they will fill the entire amount of PC resources.

Pointers are not only about memory destruction, but also about allocation.
But the main thing is that you understood one thing: a pointer is an address in memory plus a type compiled into the program (there are no types at the iron level).
And secondly, using the pointer, we can access an object whose name we do not know (as I have in the symbol table) or it is unnamed (if it is allocated dynamically, during the program, and not compiled into it).

A
Adamos, 2021-11-05
@Adamos

Written game that has nothing to do with reality.
Moreover, the style of writing speaks of the low literacy of the author.
Read normal textbooks, not gag.

R
res2001, 2021-11-05
@res2001

From all that has been written, in fact, it says only:

A pointer is a variable that stores the address of a RAM cell.

the rest is some trash.
I'll add from myself about pointers:
Memory in a computer is addressed byte by byte.
Bits within a byte cannot be addressed via a pointer.
An address is just an unsigned integer - the byte number in memory.
In 32-bit processors, the maximum available is 2^32 bytes of memory, in 64-bit ones - 2^64.
Accordingly, to store an address in 32-bit systems, 32 bits are needed, i.e. 4 bytes, in 64-bit - 64 bits, i.e. 8 bytes. Thus, depending on the bitness of the processor, the size of the pointer changes.
When an object (string, number, etc.) is stored in memory, it has no type - it's just a collection of bytes of some length. The program does the typing. For example, in the processor there are sets of arithmetic instructions for integer 1 byte variables, 2 byte variables, 4 and 8 byte variables (separately for signed and unsigned variables). Those. these are really different assembler instructions (for example, for addition). By specifying a pointer type in this way, you are telling the compiler that the instructions for that data type should be applied to the memory addressed by that pointer.
For typed pointers, address arithmetic can be used because the compiler knows the size of the type. For untyped pointers (void*), address arithmetic is not possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question