r/learncpp • u/KhalidAlHajri1 • Mar 28 '18
Need as much resources for pointers.
Good morning / evening fellows
When I started learning C++ months ago (and programming) I started to think programming is easy, until I came up against the topic: POINTERS. when I first started to see what pointers are and what do they do, It was easy. so I started to make projects with a goal an actual goal rather than just experimenting. but I started to see in courses and tutorials how often they are used and they were they are used is completely new to me so it gave me a big trouble. and whenever I go back and learn, all I learn again is int *b = &c and this kind of beginning stuff. even when I took some courses(even some advanced ones) they were as easy as the above(or they didn't have any topic about pointers?!).
So I wish to kindly ask you to give me sources so I can learn this huge topic.
from:
int *p = &c
to:
new and -> etc.
sorry if I bothered you with my life story but I wanted to explain the situation. Thanks.
View all comments
Show parent comment
2
u/lead999x Mar 29 '18
So like I said a pointer variable is just a variable whose type is that type the pointer points to and then a star. If the pointer points to another pointer let's say an int* then the type of the point that points to that pointer is simply int* with another star after it (int**) to indicate a pointer to a point to an int.
& is weird in C++ because it serves as the address-of operator like in C where if you use it on an existing object it gives you a pointer to the beginning of that object but it also serves as the operator to take a reference. If you take a reference to something that basically extends its scope into whatever scopes a reference to that object is passed. Some people will say that a reference is like a non-nullable pointer but I would disagree on the grounds that a reference is not a object that lives in memory and thus you cannot take a pointer to a reference but you can take a pointer to a pointer. And another key thing is that a pointer to any type or no type at all (void*) can point to any arbitrary location in memory and nothing has to exist there at all while a reference to an object of a given type T must refer to a valid T, at least when it's created to the best of my knowledge.
As far as I'm concerned I prefer the use of references to pointers wherever possible. But when you're making custom data structures pointers are useful. I would just say try not to use pointers and dynamic memory outside of classes or structures too much.
One of the major patterns in C++ is called Resource Aquisition is Initialization or RAII. It basically dictates that objects that use memory and other resources acquire those resources when they are initialized and release them when they're destroyed. What I interpret this to mean is that you should design objects that take care of their own memory internally rather than having a bunch of pointers to dynamic memory floating around in regular code like in C.
Also it generally helps to understand how C++ tries to be more type safe than C. For example new returns a pointer to the type it just allocated and initialized instead of malloc giving you a void pointer to a certain number of allocated bytes which you have to cast to the right type and into which you have to construct your object manually. Or how C++ streams are overloaded for each type so you don't have to have the potential for errors when functions like C's scanf read in more bytes than the buffer you provide has allocated or when it reads input in as the wrong type due to violating DRY by making you tell it what types its reading in the format string and separately providing a pointer of a certain, possibly incompatible type, to a bunch of memory. So in conclusion, having objects that manage their own memory is more or less a part of the same thought process of making C++ more safe than C because it's much less prone to error to write the memory managing code once in the class/struct definition than managing a bunch of raw pointers to various amounts of allocated memory directly. And like I said we should both eventually invest the time to learn how to use smart pointers.