C++ Tutorial 10: Pointers Explained (Linked Lists vs Arrays Example)

preview_player
Показать описание
Today we'll look at basic pointers. We'll go through the concept of a pointer and the syntax but we're really only scratching the surface here. We'll look at pointer arithmetic and passing by reference and that sort of stuff in a later tutorial. My main goal is to show that pointers are more than a way for experienced programmers to show off to newbies, they are an integral and very powerful part of C++. They are not vague, magical or difficult to understand, they're just variables which hold the address of another variable.
Рекомендации по теме
Комментарии
Автор

Cheers for watching and commenting! We will make a linked list in a future tute, we need to cover dynamic memory first as linked lists depend on it.

I'm very happy you've mentioned the challenges, I wasn't sure that it was a good idea. I'll try to put a challenge or two at the end of future tutes.

Thanks for watching and commenting again, have a good one!

WhatsACreel
Автор

Thanks again. Your explanation of how linked lists work brought home the value of pointers to me: it didn't make much sense before.

alfredcalleja
Автор

You could certainly use pointers to handle an inventory for a game!

WhatsACreel
Автор

You didn't show how to put the new number into the linked list. I get the example of how it is useful, but I still don't know how to actually do that in C++.

Also, please bring back the challenges. They were extremely helpful for my understanding of what is going on in your explanations and I haven't seen a challenge for the last couple videos.

I still love your tutorials though. :-)

DewDragon
Автор

One of the more common uses for pointers that is used probably more than any other use is like the following... lets say you have an struct that has a large number of values stored in it. And you need to pass that to a function. The normal way of doing this would be horrendously slow as you would be sending a copy of the entire struct to the function.

struct DATA {
int a;
long long int b;
char c;
int d;
}

DATA myfunc(DATA dataCopy)
{
// do something with Data
return dataCopy;
}

So with that, you have to copy the data and send that to the function. Then after it is done, it needs to send a copy of that entire struct back to where you called it from. That means you first send 17 bytes to the function (in the example above), then return 17 bytes of data. That's a lot of bytes. If you have to call this thousands of time or more, that grows exponentially and will slow things down.

The solution is to only send the function 4 bytes of data in the first place and you do not need to return anything from it at all! That's a huge savings to go from 34 bytes total (17x2) to 4! How do you reduce it down to 4 bytes (for 32bit, 8 bytes for 64)? You send the function the address of where that struct data is stored in memory so the function can then read it and write to it directly without any need to copy the data. The way you do that is by using a POINTER. So you might rewrite the myfunc() above like

void myfunc(DATA *dataPtr)
{
// do something with data
return;
}

When you call the function you would call it with:

myfunc(&mydata);

this would send the address of your data to the function instead of copying the entire thing. Addresses are only 4 bytes in size for 32 bit programs, 8 bytes in size for 64bit programs. 4 bytes is faster and the function can then access the struct members directly, also faster and of course, no need to return a copy of what changes you made.

This works fine like this in C. An additional feature you can use in C++ is you could also redo the function like:

void myfunc(DATA &dataPtr)
{
// do stuff here
return;
}

With this, you would only need to do:

myfunc(mydata);

...no & needed as the function knows to get the address of anything you send to it.

This is by far THE MOST COMMON use of pointers as it eliminates the need to waste time and memory copying data around to functions. You'll see quite a few libraries use this and that is why you often need to pass the address of a variable to some functions with &.

NeilRoy
Автор

omg awesome Video. everything explained very well and detailed, thank you allot !!!

helioimperium
Автор

It's a pretty hard concept to comprehend I think, but it's goddamn awesome! So, you could use, in example, a pointer (handler) to handle inventory slots in a game? Or is this too far fetched?

johnnyvandenelzen
Автор

I have a question. Are you still doing videos on this. If you are, maybe you could make a small game that wouldn't take long. Just for a test. You might of already, sorry if you have. Btw I really like the videos, i understand most of it, and i'm thirteen. I've always wanted to be a game programmer, and these videos give me a head start for the future!(:

MeBeMcGee
Автор

Shouldn't You say "Virtual Memory" instead of "RAM" ?

MateiAndrei
Автор

John Lennon teaching C++, what else could you ask for?

alejandrodoe