Page 1 of 1

[C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 12:49 pm
by vitinho444
Hey guys, I've been reading in my "no-internet-connection" free time a C++ book to expand my knowledge with technical details, those that I never used, and I came across Pointers and References.

I got what they meant, and how to use them, but I just don't see why I would use that in my projects. The book even says a Pointer is a ALIAS of the object it points to, but why do I need a ALIAS if I will create the variable with the name I wanna use throughout the project?

Like why do :

Code: Select all

Player player;
Player *p = player;
Instead of

Code: Select all

Player p;
If anyone can give me some advice on this, because it's messing with my head :P
Thanks

Re: [C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 5:35 pm
by a_bertrand
Pointers are NOT related directly to objects but just how memory is accessed:
A pointer is nothing else as an address which POINTS to possibly some data. Possibly as a pointer could point to a function, or nothing as well.

Pointers in C and C++ are preceded with a * character:

int a=5;
int *pa=&a;

that will make our *pa pointer points to the address of the variable a. Yes it's a bit odd but that would work ;)

Pointers data are usually allocated either via malloc (memory allocation) (old C syntax) or via the new keyword. BE CAREFUL, whatever you allocate you will need to free! Otherwise you will have what is called a memory leak => basically you lose memory as your soft is not releasing what is not useful anymore (using free or using the delete keyword).

Keep in mind that using correctly pointers is essential in C / C++ but can be really handy with other languages as well like C# or even Java (the last one you don't have pointers, while in C# you can). Why? Because once you really understood how memory is handled on your CPU you will code in better ways, and you will also understand what's really the difference between passing a variable by reference (pointer) or by value (copy over the data).

Re: [C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 6:32 pm
by Jackolantern
Pretty much the explanation Alain gave, but I just wanted to say that in C++, using pointers is not an issue of "wanting to" or "not wanting to". You MUST use pointers (unless you code everything procedurally, I guess, but then just use C if that is the case). It is part of C++ that gives it its low-level powers. You will make tons of mistakes in C++ if you try to ignore the pointer operators.

You are using them in C# and Java, too, but the runtime is doing it for you instead of you having to do them explicitly.

Re: [C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 7:03 pm
by vitinho444
Wow very detailed explanation, I haven't really learn much yet, I just read it out, not practiced anything yet, but I get it now just by this:
a_bertrand wrote:the difference between passing a variable by reference (pointer) or by value (copy over the data).


So to access some object already created in C++ (or the other languages with Pointers), we should use Pointers to help out the memory "stuff" ( memory stuff :lol:) ?

I guess when I followed a series of tutorials on C++ the guy used pointers on the functions to pass on objects like: void Function( Player *player), and that makes sense now :)

By looking at all this, I feel like I don't know much about the programming languages I use :/ because what I use it's like a tiny bubble, and I keep on doing that (mainly because I like it), It makes me fear the future of getting a job and people asking me stuff I don't really feel comfortable doing (because I may not know it 100%).

Re: [C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 8:11 pm
by Jackolantern
Maybe this is comforting or maybe its disheartening, but you will never be comfortable with everything programming has to offer. Even if you hit all the popular stuff, there are entire worlds that stay mostly out of sight. Last semester we had to work with the low-level details of how you make multi-core system processors communicate, and it is insane. There are likely 1000+ page books written on just that topic.

Re: [C++] Pointers, Where and why should we use them?

Posted: Sat Jul 12, 2014 8:20 pm
by vitinho444
Jackolantern wrote:Maybe this is comforting or maybe its disheartening, but you will never be comfortable with everything programming has to offer. Even if you hit all the popular stuff, there are entire worlds that stay mostly out of sight. Last semester we had to work with the low-level details of how you make multi-core system processors communicate, and it is insane. There are likely 1000+ page books written on just that topic.
Well yes, but people offering jobs won't accept that... They WANT you to know, and that's it.. (at least here in Portugal). I hope college helps me get into the "mainstream" side of programming, so I can perform my future job comfortably.
I just can't imagine those guys developing new technologies, like.. YOU are the inventor of it, you don't have "tutorials" or guides or stackoverflow for inventors :lol: . It must take some deep knowledge and confidence i guess.

Thank you Alain and Jacko for everything :)

Re: [C++] Pointers, Where and why should we use them?

Posted: Sun Jul 13, 2014 4:24 pm
by a_bertrand
Here I disagree, you CANNOT simply know all. If you specialize in web development you will not know how low level drivers are written and you don't care either. Companies hiring you will be more happy by somebody which is showing willingness to learn and skills to adapt than somebody which says he knows all (as it's simply impossible). The last guy we hired had no knowledge of C# / .NET nor any of the protocols or tools we used. Yet we (my boss and I) choose him because he demonstrated that he was keen to learn and flexible enough to do so.

That's also why I don't agree with sentences like "learn all the languages you can". Sorry that doesn't work, as anyhow you will take YEARS to master any language really. Sure in a week or less you can write a soft in this or that language, but between being able to write a first soft and really taking profit of the language / framework there is a huge gap.

I will continue to say as I always said: stick on one subject / language / framework, don't switch every time, and try to go deeper and deeper with it. At the end it will be more profitable than simply knowing just a bit of each.

Yet a good knowledge of C / C++ is profitable in nearly all the cases, even if I would never use it anymore for WEB / GUI or DB development ;)

Re: [C++] Pointers, Where and why should we use them?

Posted: Sun Jul 13, 2014 4:33 pm
by vitinho444
Wow very nice then :D

I'm currently working only with C# (unity scripting) and PHP/MySQL/HTML/JS (web dev). I fear not only that I may not know what they want me to know, but also that I may not show that much of keen to learn new stuff. Don't get me wrong, I'm willing to learn everything I need to complete my task and go beyond that, but I'm just afraid not to show it all on an interview due to the fact that I know nothing/little about what they want me to know.
I'm a little bit less afraid now because you said that you and your boss hired that guy, and I hope many are like you and your boss and give a chance to others that are willing to go as far as it gets to complete the task.

Re: [C++] Pointers, Where and why should we use them?

Posted: Mon Jul 14, 2014 1:56 am
by Jackolantern
vitinho444 wrote:I just can't imagine those guys developing new technologies, like.. YOU are the inventor of it, you don't have "tutorials" or guides or stackoverflow for inventors :lol: . It must take some deep knowledge and confidence i guess.
There is always tutorials/documentation for software, because software requires software to make. Even if you are making a new native programming language compiler, there are tutorials out there on how to make compilers and there are huge assembly manuals for each processor architecture. You are never completely in the dark.

Hardware is a bit of a different story, since in some cases you really are fabricating it from scratch, but even that is rare today.

Re: [C++] Pointers, Where and why should we use them?

Posted: Mon Jul 14, 2014 8:48 am
by a_bertrand
Actually no Jacko, we do develop every days new pieces of hardware here. Sure they usually start with an FPG or some chip from the market, yet what is built here is usually totally new / custom, and may be even unique as we may some times need only one piece of it. However yes, developing hardware is usually more rare than developing software ;)

vitinho444: Don't worry, I would guess that most companies don't expect you to know all what they need as if they are really doing something special (and not just web shops) they will have their own way of doing it. Sure they can (but not given) work with common tools / languages / frameworks, and knowing some of it can be an advantage, but this is rarely a show stopper.