How do pointers in C++ work? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

How do pointers in C++ work?

After days of searching the web, I still haven't completely understood how pointers in C++ work

31st Mar 2019, 11:51 AM
ViperByte
ViperByte - avatar
5 ответов
+ 4
I made this code because this question or similar appears many times, in fact this is not a simple topic as it may appear at first glance. Hope it helps. https://code.sololearn.com/cYF432ujeucS/?ref=app
1st Apr 2019, 12:06 PM
AZTECCO
AZTECCO - avatar
+ 3
Thank you everyone for your answers! I know I need more practice but I believe I now have a better understanding of how pointers work.
1st Apr 2019, 9:21 PM
ViperByte
ViperByte - avatar
+ 2
Pointers are a way to pass by reference. Imagine copying the contents of a book and then making changes to the copy, vs changing what's written in the book. You'd argue that the latter's more efficient no? Then that's what a pointer does, it passes the address of data and helps manipulate the original value vs copying said value. If you're new to programming and starting out with C++, it may take getting used to, especially since passing copies of data is easier and at first you won't notice a big difference. In time though, it'll become natural.
31st Mar 2019, 5:00 PM
Justin
Justin - avatar
+ 2
Another obvious use is the implementation of arrays: int *p = new int[50]; This allocates 50 integers on the heap (outside the stack) and saves their location in p. You can also do linked lists, like this: struct person { char *name; int age; person *next; }; This way you can work with a person and then follow a next pointer to work with the next one (when next is NULL you have reached the end of the list). In the end, pointers are just integers, and since you can already work with integers you shouldn't be scared of them.
31st Mar 2019, 9:06 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
A CPU can only work with a few numbers (integers, I'll be excluding floats for the purpose of this answer) at a time. How many depends on the architecture: it's 8 32-bit numbers on x86 and 16 64-bit numbers on x86_64. These numbers are held in something called registers. All machine code instructions (those that the CPU reads in and executes) reference these registers. If you want to add 2 numbers, you can only add 2 registers (or a register and a literal) and the output will be held in a register. So then how does CPU deal with more than 16 variables? How does it even access the instructions it must execute? It does this using pointers. For example, one additional register not counted in the 8/16 is the instruction pointer. This holds the address of the next instruction in memory. When it is time to execute it, the CPU fetches the next instruction from RAM and increments the instruction pointer. One other register is the stack pointer, where most variables are stored.
31st Mar 2019, 9:01 PM
Vlad Serbu
Vlad Serbu - avatar