Buckys C++ Programming Tutorials - 41 - Pointers and Math

  Рет қаралды 365,296

thenewboston

thenewboston

Күн бұрын

Пікірлер: 216
@juustgowithit
@juustgowithit 8 жыл бұрын
usual teacher: *explains shittily, moves on like it's fine* bucky: *explains perfectly the first time, still tries to phrase it in as many ways as possible to be clear*
@raytang1867
@raytang1867 7 жыл бұрын
So true.
@evi.wammes
@evi.wammes 6 жыл бұрын
pretty cool huh
@informativecontent4778
@informativecontent4778 6 жыл бұрын
coz he's the best teacher in the world
@abboudashkar3804
@abboudashkar3804 4 жыл бұрын
I have shitty teacher like that
@stezzthehaxor9927
@stezzthehaxor9927 8 жыл бұрын
5 years later and you are still making nerd lifes easier
@rawanabo-egila9083
@rawanabo-egila9083 5 жыл бұрын
7 years*
@LANstorm.
@LANstorm. 5 жыл бұрын
@@rawanabo-egila9083 7 years and 4 months
@figure8379
@figure8379 5 жыл бұрын
@@LANstorm. 8 Years and 5 Months*.
@LANstorm.
@LANstorm. 5 жыл бұрын
@@figure8379 8 years and 8 months
@P170101
@P170101 5 жыл бұрын
@@LANstorm. 8 years 8months and a week
@GabrielSantos-kq1sg
@GabrielSantos-kq1sg 8 жыл бұрын
The BEST tutorial about this that I've ever seen, I'm Brazilian but I understood your tutorial very clearly, all the other tutorials that I watched don't explain like yours, I was about to been crazy with this pointers and you made me get this, thanks a lot man!
@timesnow6205
@timesnow6205 3 жыл бұрын
and to make it even better, try to use what you just learned so you can understand even better
@mk-nw4si
@mk-nw4si 5 жыл бұрын
for those who didn't get it properly yet, the memory address changes by 4 instead of 1 because it increases in terms of bytes of an integer which holds 4 bytes. to make it more clear.. assume that you have an array of type double (size of double datatype is 8 bytes) assume the first index got the address of " 003DF6D0 " then the index afterwards will hold the place of "003DF6D8 " in the memory , why? because the array is double type. so if you have a pointer that points to index [0], then increased where that pointer points by 1, it will move where that pointer by 8 bytes. which will be the second index. just like the previous example. hope that helps.
@小龙的梦
@小龙的梦 6 жыл бұрын
the array is already a pointer you don't need separate pointer variable bucky is the address of first int element in the array and bucky +1 is adress value at the second element dereferencing the *(bucky + i ) will show the elements stored just like bucky[0] or bucky[i] you truly are a great at teaching loved your videos
@molnez
@molnez 7 жыл бұрын
Super nice tutorials. Thank you very much, Just one small thing @1:10; Saying "bp0 is at " print(bp0) is misleading since it implies that you are printing the address of bp0. The address of bp0 is &bp0. The text should really say "bp0 points to address: " print(bp0), or "The address of the first element in \"bucky\" array is" print(bp0). I have a little bit of experience coding in C, I'm mostly watching for the higher level C++ stuff. Really like the format of super short and concise videos by the way:)
@johnwayne2700
@johnwayne2700 8 жыл бұрын
I am actually getting what pointers are for. Thanks Bucky. At first, it seemed to me like black magic. Now I cannot imagine my programs without pointers.
@JoseGonzalez-rt5fk
@JoseGonzalez-rt5fk 7 жыл бұрын
Thanks, you taught me something I probably would not have known. I really appreciate your work!
@Glad-z3t
@Glad-z3t 11 жыл бұрын
Bucky, you can't keep eating mcdonalds! You are too smart for that!
@IUseQsInsteqdOfQs
@IUseQsInsteqdOfQs 12 жыл бұрын
I love how you teach Computer Science theory instead of just codemonkey stuff. Great job!
@eyalpery8470
@eyalpery8470 8 жыл бұрын
Saying "next element" is not accurate because what if we are not handling an array? A different explanation: bp0 += 2 means increment of "2" in memory => 2*sizeof(pointer type) => 2*sizeof(int) => 2*4 = 8 This is why memory 00 became 08. Test your understanding: if bp0 was double and not int, then +2 will be?..... 2*8 = 16 => memory of 00 will become 16 (and not 8)
@TzaRider
@TzaRider 8 жыл бұрын
thanks that helped a lot
@BradenBest
@BradenBest 8 жыл бұрын
I'm glad I'm not the only one who understands and appreciates what the compiler automatically does for the programmer. Although, just a note, 16 is 10 in hexadecimal, so 0xff00 would become 0xff10, not 0xff16
@shakuntalajain9788
@shakuntalajain9788 7 жыл бұрын
Eyal Pery Thats exactly what he said. He said bp0 += 2 will point to memory location which is 2 ahead of the current location. So if its char, it will point from 00 to 02 If its int, it will point from 00 to 08 If its double, it will point from 00 to 16, but since its in hexadecimal, 16 will be converted to hexadecimal i.e 10
@huseyin.goktas
@huseyin.goktas 6 жыл бұрын
That is a nice explanation.
@SmithsRichard
@SmithsRichard 12 жыл бұрын
the other teachers on youtube sucks because they try to explain stuff which we really DONT need to know at start AND THEN WHAT HAPPENS is that we start thinking that the language is difficult and quit it. Bucky built our confidence by saying that you dont need to learn this now and we are like "OK BUCKY :)" and this is how slowly slowly we try to understand and master the language. Bucky is an awesome teacher.
@RattanakChea
@RattanakChea 13 жыл бұрын
My instructor spent one hour explaining what you did in 6 min. and i understand you better.
@jamesr2784
@jamesr2784 9 жыл бұрын
"i'm lovin' it!" - my thoughts on this tutorial
@xXBR4D3NXx
@xXBR4D3NXx 12 жыл бұрын
If there's one thing my professor has been successful at (teaching coding is not one of them), it is that programming is way more than knowing syntax. You've got to practice real world problems and not just sit back and be satisfied with just these videos (even though you'll learn c++ syntax much more efficiently and passively).
@abhilash438
@abhilash438 13 жыл бұрын
u r awsum...don't have words 2 thank u...u r last most wonderfl creature created by god... really thnaks...i hav downloaded all ur boston tutorials of c, c++,java,xml....all od dem r superb..
@Microtardz
@Microtardz 11 жыл бұрын
To defy the scope of variables without having to declare globals. He went over this previously. If you give a function a variable from say, main, that function won't be able to change that variable. If you give it a pointer from main, that function will be able to change the value in the memory address of the pointer you've given it, thusly changing the value of the variable. Also, pointers are useful for creating dynamic arrays. Though I tend to use vectors for that, which are build on pointers
@SagiMor23
@SagiMor23 5 жыл бұрын
should've done this with for loop but again...BRILLIANT
@cnsnmms3708
@cnsnmms3708 8 жыл бұрын
Wow that is really a great tutorial. I couldn't stop and I watch all the series until this. I learnt lots of useful tricks! Is there a donation channel or something to support this?
@Qazqi
@Qazqi 11 жыл бұрын
Adding 1 or 2 to a pointer moves it ahead 1 or 2 * sizeof(*ptr) bytes. An int * has no idea whether what it's pointing to belongs to an array. The best you can hope for if it isn't pointing to an element of an array is adding 1 to it, in which case the variable it points to is treated like an array of one element and you end up with a valid one-past-the-end pointer. If it's not pointing within an array, or to a variable, or one past the end of either, it's undefined behaviour.
@kagedrengen
@kagedrengen 13 жыл бұрын
I love learning this stuff, thanks Bucky.
@geraldjoshua9554
@geraldjoshua9554 8 жыл бұрын
I think it would be more appropriate to say that the pointer bp0 has a value of 0x28ff00, since the pointer bp0 has its own memory address (Simply just use this statement to know the memory address of the pointer bp0: cout
@myyoutubeorel
@myyoutubeorel 8 жыл бұрын
it actually changes to 08 because each address is 4 bytes in c++, therefore if you say +2, the compiler will take it to 2 ""steps"" and place it in address 08.
@bohterham
@bohterham 13 жыл бұрын
Thank you bucky you're the best teacher in the world, but i think you read that every day :D
@TsvetomirLazarov
@TsvetomirLazarov 11 жыл бұрын
In HEX code you have 0-1-2-3-4-5-6-7-8-9-A-B-C-D-E-F and then again 0-1-2... In your case you have 8. When you add 4 to 8, you get 8-9-A-B-[C]. Then, when you add 4 to C, you get C-D-E-F-[0]. After that, you add 4 to 0 and you get, as expected [4]. Adding 4 to 4 will be [8]. And for the bp5, you will get [C] again.
@Octayne93
@Octayne93 13 жыл бұрын
LOL so...here I am at tutorial 41, learning about pointers. My college professor is still teaching my class about cin
@herp_derpingson
@herp_derpingson 12 жыл бұрын
Now you are thinking with Pointers!
@Qazqi
@Qazqi 12 жыл бұрын
It also only applies when your pointer points within an array (IIRC, normal variables are counted as an array of length one for this purpose) or one past the end (though you can't dereference it there). Doing int *p = &x; and p += 2; is undefined behaviour. Doing int arr[10]; int *p = arr; p = p - 1 + 4; is undefined behaviour because even though it would end up three ahead, still in the array, it went out when moved back one. It's kind of stupid imo, but that's how it works.
@Endgame_01
@Endgame_01 11 жыл бұрын
Ok. So by adding 1 or 2 to the pointer variable, we're actually adding it to the index of the array element it's pointing to.
@EPKX
@EPKX 11 жыл бұрын
nope as behind c++ is actually machine language and machine language follows basic arithmetic , which says b=a is exactly the same as a=b it doesnt treat them differently, although for us we can see that b is written first and a second the computer doesnt see those pixels, when it converts both into machine language it gets the same result, this is so as the compiler is made to optimize your code when transferring it into machine language.
@raghavmangal7419
@raghavmangal7419 6 жыл бұрын
in this video the address of bpo is 0x28ff08(at time 1:21) but later on it changes to 0x28ff00(at time 2:12) how?please explain
@smbossco
@smbossco 8 жыл бұрын
I like to think of it as moving two addresses down, rather than adding a number two in a mathematical sense.
@GameerGuyz
@GameerGuyz 10 жыл бұрын
char = 1 bit. this should help some ppl who are confused: char* jumpAddr(int*start, int toJump){ char*addr = (char*)start; addr += toJump; return addr; } if we use this function with (0033FAB0, 1) we will get 0033FAB1. if we want the next integer we basicly jump 4 and then cast it.
@Rishav_510
@Rishav_510 5 жыл бұрын
i am grateful
@blackneos940
@blackneos940 7 жыл бұрын
As always, thanks for the tutorials, Bucky..... :D Man, Caffeine is starting to make me TIRED..... Better abstain for a while..... :3
@proudsonofheaven
@proudsonofheaven 4 жыл бұрын
At 1:17 the memory address that bp0 is stored at ends with "08". but when bucky fleshes out the bp0, bp1, and bp2 (2:10), the address for bp0 changes to "00". i feel like the memory address should have ended with "00" to being with, why was it "08" at 1:17? if someone can help explain it would be much appreciated.
@heisenberg7945
@heisenberg7945 3 жыл бұрын
same question
@hetaeramancer
@hetaeramancer 2 жыл бұрын
maybe you can store them upwards or downwards in memory. like if you park in a big parking lot with letters and numbers, you can park next to A5 which is A6 or you can park at A4
@EPKX
@EPKX 11 жыл бұрын
thats because you have to tell the computer b3=b3= what 3 or 4 u just told it to equal the 3rd and 4th element it has a choice it can either make the array to:1,2,3,3,5 or 1,2,4,4,5 computer should never be given a choice as it cant decide hence a default protocol comes to play which ignores the change
@steve122288
@steve122288 8 жыл бұрын
it looks like it doesn't just point to the address of the next element, rather it just points where the next element would be in memory, based on how many bytes of memory is reserved for that data type in the array. As i noticed, an integer pointer +1 will move 4 bytes ahead in memory, where a double pointer will move 8 bytes ahead in memory . So effectively when you add to the memory address of a non array, it will move based on the amount of bytes reserved for that data type. So its almost like saying address of variable + 1 reserved bytes ). Or perhaps even &variable + sizeof(variable); Only problem there, is that it would return &variable+4 .. haha (assuming 4 bytes).. Which isn't what u want.
@codyaldaz4055
@codyaldaz4055 8 жыл бұрын
I've been watching these videos (1-40) at 2x speed but slowed the video down to normal speed for this video. It was shocking at first.
@netgirl111
@netgirl111 12 жыл бұрын
omg u r my hero ... thank u for making it so easy to understand ...
@Tnargav
@Tnargav 13 жыл бұрын
You don't stick anything anywhere and you don't add any values there - check that array has no value assigned at all. Array type is int so every pointer incrementation will 'jump' every 4 bytes - if int is stored in 4 bytes. If you store something different the 'jump' will increase/decrease accordingly.
@thesickbeat
@thesickbeat 13 жыл бұрын
@MrKilltastic Depends on the situation. First of all the place where u work makes a big difference and your grade makes a big difference. I am not sure which one earns more but I can tell you one thing; if you're good at programming you can make a shitload of money.
@sandy1995
@sandy1995 9 жыл бұрын
Oh Nice. it points to the next element with the ++.
@sherifnegm9046
@sherifnegm9046 11 жыл бұрын
u should open a school (thenewboston school)
@theseangle
@theseangle 4 жыл бұрын
He should open a city called "New Boston" and make New Boston Univercity
@sanchitverma2892
@sanchitverma2892 6 жыл бұрын
"thats how pointers work with meth"
@godpower97
@godpower97 11 жыл бұрын
So what are pointers actually used for in larger programming projects?
@evanmastermind
@evanmastermind 10 жыл бұрын
He doesnt reply
@UAslak
@UAslak 10 жыл бұрын
From what he says it appears they are useful in functions that has the purpose of changing the variables you pass to them. Also when you pass a non pointer variable to a function the computer makes a copy of that variable, which costs computing power. This doesn't happen when you pass a pointer variable to a function, and as such you save a hell of a lot of memory and computing power with pointers if one variable is to be passed to 100.000 different functions.
@godpower97
@godpower97 10 жыл бұрын
Ah, that would make sense. So much of this stuff doesn't seem practical until you see how it saves memory in large-scale projects.
@426Hemidodge426
@426Hemidodge426 10 жыл бұрын
Pointers are necessary when creating large data structures like linked lists, trees, etc. as the memory needs to be stored on the heap instead of the stack so the data is not terminated with the function.
@brianlaudrupchannel
@brianlaudrupchannel 9 жыл бұрын
Again im thinking....whats the point of pointers?
@waelal-zubieri5358
@waelal-zubieri5358 8 жыл бұрын
+billyblackburn87 pointers are used for efficiency purposes. While the difference between using regular variables and pointers might not be perceivable when making simple program like these, they do matter in a very complex programs like games, apps, and etc. Without using pointers, there will be a lot of resources that are going to be wasted on copying the value from one variable to another(instead of passing the address). That's super bad news for any programmer assigned to create a program.
@BradenBest
@BradenBest 8 жыл бұрын
Wael Not to mention, to have any effect on a data structure without using pointers, you have to pass it in raw AND have the function return the modified structure, such that `mystruct = modify_mystruct(mystruct);`, so the whole structure gets copied twice. This also has some rather ugly implications when you're dealing with a very large structure. Meanwhile, a pointer enables you to just send the address where the structure begins, and the function can use the pointer to access the same memory location, avoiding any copying. `modify_mystruct(&mystruct);` Pointers also make arrays possible. Arrays are special pointers with fancy properties like known size[1], compiler-enforced write protection[2], and the unique ability to be initialized with both an initializer list[3] and a string literal[4], if the array's base type is `char`. Of course, arrays very easily decay into pointers, and once that happens, those properties are lost[5] with the type information, meaning you can pass an array of char into strlen() and it will correctly return the size no matter how strlen is implemented (i.e. using an iterator or pointer arithmetic, the latter of which throws a compile-time error when directly attempted on an array) 1. (sizeof array / sizeof array[0]) // gives the array's length in units, but it only works on arrays 2. array++; // will throw a compiler error, saying the operand of ++ must be an lvalue 3. char array[] = {'H', 'e', 'l', 'l', 'o', 0}; // initializer list 4. char array[] = "Hello"; // string literal 5. array++; // now possible since array has decayed into a pointer.
@fablanta
@fablanta 7 жыл бұрын
So why can't you just use global variables to avoid copying?
@BradenBest
@BradenBest 7 жыл бұрын
+fablanta - because you'd quickly pollute your global namespace doing that. It's best practice to avoid global variables as much as possible. The more complicated your program state, the harder it will be to understand what the program does in the long run. (more on global state: softwareengineering.stackexchange.com/q/148108/198539) Take the following function, for example: int collatz(int n){ return (n % 2 == 0) ? (n / 2) : (n * 3 + 1); } It is obvious what this function will return no matter what you pass into it. However... int collatz_num; int collatz(void){ int n = collatz_num; return (n % 2 == 0) ? (n / 2) : (n * 3 + 1); } This implementation would be problematic not only because it makes more sense to just pass in an argument, but also because now the function relies on global state, and is therefore unpredictable. If another function happens to change collatz_num before you call collatz, it may result in an unexpected value. In the worst case, this will result in undefined behavior. In the best case, this will result in a software bug. Extend this to a more complicated global state (or learn the hard way and shoot yourself in the foot) and you'll see how this quickly gets retarded. However, global variables are not inherently evil. If used inappropriately, they will destroy a program's maintainability. If used elegantly, they are very useful. Generally, though, that's going to be things like an internal stack, memory pools, tables (arrays of structures), and more importantly, data that won't change often if at all. Typically, said global variables are declared static for internal linkage and only exposed to other files if absolutely necessary. And to expand on that: global _constants_ are preferable to global _variables._ I.e. if you explicitly have a single structure acting as the program state, then it's okay for it to be global as long as you are careful. Beyond that, you should take care that most of your functions are stateless (will have the same output for the same input), so that their behavior is obvious and thus easy to debug. Global variables can quickly make this a pain in the ass, especially if they're tightly coupled with half of the functions in your codebase. You should read more about global state and functional programming to understand why it's important to have functions that are as clean and self-contained as possible
@fablanta
@fablanta 7 жыл бұрын
@Braden Best I am just a hobby programmer. I have dabbled in other languages (Jean, dBase III, BASIC, Cobol, Fortran etc). City & Guilds, is the closest I have got to a low level language like C++. I'm just on lesson 41 so I don't really understand what you mean by, pollute my global namespace, and your example isn't obvious because the operators ? and : have yet to be introduced in this course. In other words your explanation relies on knowledge I don't have yet. At my level it seems like there would be no difference between using a global variable's name rather than using a pointer to indicate where that variable's data is located. I need more KISS, less confuse before it can sink in to my skull as to why pointers are better :)
@Jack-4242
@Jack-4242 13 жыл бұрын
can you please make an advanced c++ series with GUIs and explain Threads as well?
@hconel
@hconel 6 жыл бұрын
ok nice explanation but few questions: - if passing by reference is the most efficient way, why is it not the default passing method instead of passing by value? - putting a * before a variable already indicates that it is a pointer, so why put & before the variable of the memory to be accessed? - the type of the variable which we are trying to access its location is already known, why do we need to state the type of a pointer variable? i.e, can we use a pointer of type double to point the memory address of an integer? if so, what is the use? if not, why necessary?
@jttube9977
@jttube9977 11 жыл бұрын
Dont Ask me how ou chenged me Bucky!!!!!!1 Thank You and you are So Powerfull!
@artteaparty
@artteaparty 8 жыл бұрын
My head aches!!
@mousavi128
@mousavi128 12 жыл бұрын
Your tutorials are awesome bro thank you soooo much.
@jedwilliams7319
@jedwilliams7319 12 жыл бұрын
Question @ 3:00 regarding bp0 += 2. Why can you add an integer to a memory address type? I'm guessing this has something to do with the way you declare a pointer variable using int (i.e, int *newpointer = &x). Further insight would be much appreciated.
@XnfuSec
@XnfuSec 12 жыл бұрын
My post messed up, sorry: Isn't the reason that it is now allocated at FF08, is because intergers store 4 bytes of data, and you added two integers to it so FF00 + 8 bytes = FF08? So, bp0 += 0 == FF00 bp0 += 1 == FF04 bp0 += 2 == FF08
@blueballs5345
@blueballs5345 6 жыл бұрын
ok so lets say you change the memory address of a particular variable using an addition operator, variable, so we have two pointers that are pointing to the same memory address?
@_opulence08
@_opulence08 12 жыл бұрын
The 2 adds to bucky[0] since bp0 is referring to bucky[0]. So bucky[0+2], I think.
@MushroomStorm123
@MushroomStorm123 12 жыл бұрын
Hi, i'm 17 years old and i want to be a computer game programmer working at Valve. I find Bucky as a really good teacher to learn c++ from, before i go into studies. But i only have one question:What is the formula/algorithm, for calculating the number of possible combinations of numbers containing 1 byte ? I'm talking about a system that uses 8 bits for a byte. I know that you can calculate that value by taking a factorial, but that doesn't apply here because there can only be a value of 0 | 1.
@MukeshKumar-qh1bh
@MukeshKumar-qh1bh 5 жыл бұрын
Man you are a champ and an inspiration!👍😊 /* Like if you agree */
@Rat0nCrack
@Rat0nCrack 11 жыл бұрын
Thanks for the reply, that helped clear this up a bit :)
@Qazqi
@Qazqi 12 жыл бұрын
It has to support a range of at least [-32767, 32767]. That means at least 16 bits.
@seigeengine
@seigeengine 12 жыл бұрын
There's more possible memory indices, meaning you need more bits to reference a place in memory.
@jedwilliams7319
@jedwilliams7319 12 жыл бұрын
I think its just because every time you run a program, it allocates memory in some location, but it doesn't always do so in the same place. Not sure though.
@PS2Damon
@PS2Damon 9 жыл бұрын
about the bp0 he could just say that you change the variable not the memory address
@AlphaCrucis
@AlphaCrucis 13 жыл бұрын
I'll go to bed after one more tutorial!
@jacobcrabtree6265
@jacobcrabtree6265 4 жыл бұрын
AlphaCrucis same
@rabimoshehizskiaavnergolde3195
@rabimoshehizskiaavnergolde3195 8 жыл бұрын
pointers are the best:)
@sammy63611
@sammy63611 10 жыл бұрын
An int variable uses 4 bytes of memory. So does this means if we used a smaller variable like char in this program the memory address goes up by one as opposed to 4?
@040134
@040134 12 жыл бұрын
At 1:17 bp0 is pointing to 0x28ff08 but at 2:10 bp0 is pointing to 0x28ff00 and the previous address i-e 0x28ff08 is being pointed at by the pointer bp2. Why so?
@timesnow6205
@timesnow6205 3 жыл бұрын
my pointers are keep changing the addres, for example its location was at 004BFC48 but it changed into 0036FD00 is it because i am using visual studio 2019 instead of code::blocks?
@vladandrei51
@vladandrei51 11 жыл бұрын
Well by writing b3= b2 doesn't computer understands that the 4th element will take the value of the 3rd element? Because if a = 10 and b = 5 by writing b = a => b = 10 and I thought it would work the same with pointers :(
@lesssgooo8204
@lesssgooo8204 5 жыл бұрын
shouldn't it be that bp0 'points' at instead of bp0 'is' at.... because using 'is' means that we are talking about the memory of the pointer itself and not the memory address to which it points at--- which is the memory address of bucky[0]???
@AvZNaV
@AvZNaV 11 жыл бұрын
Is your full name Buckminsterfullerene?
@WaqarKhan-mw2de
@WaqarKhan-mw2de 7 жыл бұрын
As you said its point to next element , so the next element should be replace or not . sorry I am not quite clear on it. could you please give me short ex
@goldse26
@goldse26 11 жыл бұрын
If you mean IDE, it's codeblocks, if you mean compiler, I think it's Gnu CC MinGw or something, it comes with codeblocks
@Rat0nCrack
@Rat0nCrack 11 жыл бұрын
Thank you so much, this really helped!
@MrKilltastic
@MrKilltastic 13 жыл бұрын
i was just wondering.... which pays more, an entry-level programming position or an entry level art position at a videogame company?
@fullthrottle254
@fullthrottle254 8 жыл бұрын
how do i set an account on the forum for your website? i can find a sign in or new member hyper link
@yoloop93
@yoloop93 12 жыл бұрын
so you mean it change only the place of the memory address?
@abhisheksinghrajput4022
@abhisheksinghrajput4022 8 жыл бұрын
good going bucky
@vladandrei51
@vladandrei51 11 жыл бұрын
Hm, I have a question If we have int bucky[5] = {1, 2, 3, 4, 5}; and int *b2 = &bucky[2]; int *b3 = &bucky[3]; If I write b3 = b2 isn't the array going to be {1, 2, 3, 3, 5}? For some reason is still 1, 2, 3, 4, 5, can someone enlighten me?
@markross7102
@markross7102 8 жыл бұрын
instead of int array can you do char array? I tried it, it doesnt work? why is this?
@lucianoinso
@lucianoinso 12 жыл бұрын
"Introduction to 3D Game Programming with DirectX 11 (10 or 9 too)" from Frank Luna its the bible of Directx game programming with C++
@ScibbieGames
@ScibbieGames 8 жыл бұрын
64Bit Ram Addresses are longer . I guess that's why they can hold more addresses at the same time.
@toot-thenewgoogle3857
@toot-thenewgoogle3857 11 жыл бұрын
what does 0x28fed mean?
@_kj0916
@_kj0916 11 жыл бұрын
What compiler is this ??
@Catinkontti
@Catinkontti 10 жыл бұрын
Is McDonald's your sponsor? :D
@ziadhassan7
@ziadhassan7 8 жыл бұрын
but I didn't understand why when he print bp0 out the first time it said *"ff08"* not *"ff00"* ,can you please guys explain it for me?? ^_^
@ziadhassan7
@ziadhassan7 8 жыл бұрын
And why mine said *"ff20"* *"ff24"* *"ff28"* ??!
@craignichols9552
@craignichols9552 8 жыл бұрын
Well I assume it's because he's printing out the current address of that array element value located in memory on HIS computer, so therefore, the value he created has been outputted and it's showing the address of that value on HIS machine. In theory, this value that gets outputted following this tutorial should look different on everyone's machine due to it being pointer arithmetic and the fact the variables data is being put on the heap with tons of other data, this is why pointers are useful, it finds the address of element x anywhere on a system so you can perform manipulation of data. To summarise, what ever value you're outputting, don't worry, it should look different from what he's showing you. It all depends on what memory your variable is being allocated when you do your data declaration for any variable. I hope this makes sense? Hard to explain lol. Drop me a message if you're still not sure.
@ziadhassan7
@ziadhassan7 8 жыл бұрын
ohh, thanks a lot man... but I still don't understand why it said at first *"ff08"* and after three another elements it said *"ff00"* and that's all for the first element?! (and same on my machine but of course with *"ff20"* *"ff24"* *"ff28"* like I said ).
@BradenBest
@BradenBest 8 жыл бұрын
Craig Nichols the data doesn't get put on the heap. It gets put on the stack. The only time data is put on the heap is when you explicitly call a function that returns a heap'd pointer (such as malloc or strdup) or use C++'s `new` operator. Heap is for dynamic allocation. Stack is for static and automatic allocation. Heap is expensive because you have to have the kernel fetch available memory pages. Stack is cheap because it's a data structure that is allocated before program execution begins. You have a pre-allocated space where variables can go, which is much faster at runtime than allocating space for each individual variable (since space is allocated at load time). This is why it's considered best practice to avoid malloc until it's actually *necessary*. Instead of "mytype *mt = calloc(1, sizeof (mytype));", consider "mytype mt = {0};". Instead of "return strdup(buff)", consider making buff static so that the variable persists beyond the function's stack frame ("static char buff[BUFSZ];"). Also note that static and global variables (which are "kind of" static[1]) are automatically initialized to 0, and that this behavior is guaranteed by the standard. Hence using a statically allocated array is faster than using/returning a malloc'd pointer. Keep in mind that a static variable persists throughout the lifetime of the program, all addresses returned by the function will point to the same region of memory, so if you need one function to return multiple pointers that will be used simultaneously rather than synchronously, then an output argument[2] or malloc'd pointer is necessary. As for the addresses, a modern OS kernel will randomize the page in memory that the program is given access to. This is done for security reasons and can be manually disabled by meddling with system settings (though you shouldn't). 1. If you make a global variable static, you are making it invisible to outside modules ("compilation units"). Be careful with that. Static both makes a variable persistant and only directly visible to the scope it was created in. So, for example, you could return a static global variable from a non-static function, and then use that function from another file to gain access to a variable that the latter wouldn't otherwise be able to access. 2. An output argument is when you pass in a pointer to contain the function's output. The pointer's contents are modified as a side effect of the function. Such behavior can be seen in scanf(format, ptr1, ... ptrN) and memset(ptr, value, nmemb)
@ziadhassan7
@ziadhassan7 8 жыл бұрын
Thanks a lot for that detailed explanation.. 😊
@jonetgames
@jonetgames 9 жыл бұрын
My numbers keep changing when I run it. I get a string of random 8 digit numbers each time, for each bp variable.
@mithrilstrings762
@mithrilstrings762 9 жыл бұрын
+Jonét Harry This is normal. Each time the program runs it will assign it's memory addressing to whatever is available. The addressing will change.
@MoNeyAssasin
@MoNeyAssasin 8 жыл бұрын
memorry change each time so pointer will always point to correct address each time
@ameerjeffer
@ameerjeffer 13 жыл бұрын
eating mc donalds daily makes you a good programmer :P
@3dUber
@3dUber 13 жыл бұрын
Guys if your watching these tutorials you are not ready to start making a game. so stop asking wait alittle longer and learn more. then you can talk.
@Octayne93
@Octayne93 13 жыл бұрын
Safe to say, im pwning my class. Thanks bucky! lol
@Randomforyou8
@Randomforyou8 11 жыл бұрын
so..your sponsor is MacDonalds!
@odo432
@odo432 11 жыл бұрын
Thanks, helped a lot.
@CarlosBrito524
@CarlosBrito524 12 жыл бұрын
Some instructors are not bad, some just have to go at the rest of the class' pace.
@togmeister8604
@togmeister8604 9 жыл бұрын
My program works but is it correct if I get something like 0x28fed4 when you get something like 0x28ff04. Is there suppose to be 2 numbers at the end. I get fed with 3 letters instead of ff with 2. And I get 4 with 1 number instead of yours with 2 numbers.
@shredhappy101
@shredhappy101 9 жыл бұрын
Togmeister He says right at the beginning not to worry if it's a little different. Most of the time memory address allocation is dynamic and will change from computer to computer.
@togmeister8604
@togmeister8604 9 жыл бұрын
Yeah, he only says the end bit like 08 at the end might diffirent.
@mrjuicegamer
@mrjuicegamer 2 жыл бұрын
Ty
@Seannyoo900
@Seannyoo900 8 жыл бұрын
So I copied your code and tested it for all 6 elements and something strange happens, after bp2, it says that the memory address for bp3 ends in c, then for bp4 it starts at 0 again, then bp5 is 4...Is this supposed to happen? I thought it would just increment by 4 continuously?
@Seannyoo900
@Seannyoo900 8 жыл бұрын
yeah and it just repeats like this with more elements added to the array. What does the c represent?
@4Mining
@4Mining 8 жыл бұрын
0x28ff00 for example is in hex. Hex numbers are from 0 to f (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f) so when you increment the 8 at the end with 4, it wouldn't be 12, it would be c. See more about hex calculations here www.miniwebtool.com/hex-calculator/?number1=8&operate=1&number2=4
@quequequenananana
@quequequenananana 12 жыл бұрын
Why do you have a folder in your desktop named "Watermelon"? xD!
@0x_Anakin
@0x_Anakin 12 жыл бұрын
I beg you make an openGL 3.3+ tutorial series with vbos ibos shaders etc. After that I could die happy.
@Marius-vw9hp
@Marius-vw9hp 8 жыл бұрын
You love that Mc D.
@MARGINCALLS
@MARGINCALLS 8 жыл бұрын
After bp0 +=2, can I understand that bp0 = bp2, because they have same memory address, 28ff08? Or do they just point same memory address?
@BradenBest
@BradenBest 8 жыл бұрын
Though you shouldn't compare pointers with ==. There are no practical cases where you'll need to compare memory addresses except when checking for a null pointer (ptr == NULL). -Arrays- Pointers _typically_ represent an array _of_ or reference _to_ something. In neither case should you be, with ==, comparing two of these with the presumption that they are valid pointers. This often leads to the following newbie mistake: char *mystring = "Hello World"; if(mystring == "Hello World"){ // This will run } The programmer will see that this works, and carry on comparing strings like this, until disaster strikes when two strings with equal content but different addresses are compared, and it turns out false char myArray[] = "Hello World"; if(myArray == "Hello World"){ // This will never run } "Wait! What just happened? I KNOW I got this to work before. Why isn't it working!?" -Confused newbie The reason the first example works is because "Hello World" is converted to a number (pointer) that points to a read-only space in the executable containing the text "Hello World\0". Attempting to directly modify this space will usually crash your program (I say usually because strictly, this is undefined behavior), and every instance of it in the program, not unlike #defined preprocessor macros, will be collapsed to the same pointer. Thus "0xFFE0400 == 0xFFE0400". However, the latter example fails because myArray accepts "Hello World" as an initializer list, meaning: enough space is allocated to contain [strlen("Hello World") + 1] bytes, and then "Hello World" is copied into this space. Since "Hello World" is read-only and the array it's copied into is read-write, a new space must be allocated to make this possible; they are guaranteed by the standard to have different addresses. So it literally[1] will never run, no matter how sure you are that the strings are "equal". char *mystring = "Hello World" + 1; // "ello World" if(mystring == "ello World"){ // May run if your compiler happens to be clever enough to // optimize the above string literal to "Hello World" + 1 // I.E this is undefined behavior } Use strcmp or memcmp instead. 1. Unlike the people on the internet who misuse the word, I actually _mean_ literally here. There is no special case in C/C++ where the two pointers would have the same value except in a broken implementation where read-write memory is allowed to overlap with read-only memory, in which case the whole program is meaningless and isn't even C/C++. Rather, you just compiled your C with a compiler that targets (intentionally or not) some new, bizarre C-like language that allows nonsense to happen.
@BradenBest
@BradenBest 8 жыл бұрын
***** What you're basically saying is that pointer comparison is useful if you have a pointer with an _unknown_ value (a wild pointer) and want to check it against a _known_ value other than NULL. Therein lies the problem: How would you get to a point in a program where you _don't know_ if two pointers point to the same object? Such a check shouldn't become necessary unless you're checking against NULL; you should know where things point to, and keep memory from overlapping, at all times. When you free a malloc'd pointer, and know that it will exist outside of the scope where it was free'd, you should set it to NULL, when you dupe pointers in a function, the same function should make sure that the extra reference is thrown away before it exits. That means a global pointer pointing to an address obtained from a function's argument should be set to NULL before that function exits. Otherwise, you risk boundary violation from retaining dangling and/or wild pointers, which causes undefined behavior and puts your program in an unknown state. Once a program is in such an unknown state, the whole thing becomes meaningless.
@BradenBest
@BradenBest 8 жыл бұрын
***** Again, that just doesn't seem right. If you have an array of pointer, then there is no reason you should have to check when adding a pointer to the array, because unless you're doing something _very_ wrong, you shouldn't end up with the same pointer being passed in twice in the first place. When building this array, you would undoubtedly be using one of the malloc family of functions (or a function that uses them, like strdup) to obtain the pointers. And all of these functions are guaranteed by the standard to give unique pointers that do not overlap in memory. If any two calls to malloc gave you the same address both times, and there wasn't a free() inbetween said calls, that would mean you were given overlapping memory, and that the kernel is unstable and will probably crash soon. In such a case, there is nothing you can do to fix that. That is out of your control, and so is the imminent kernel panic. ...Or it would mean that you have a dangling pointer somewhere, and malloc just happened to pick up the same address, meaning that when you later go to free() that array of pointers, you're going to hit the now-twice-dangling pointer and encounter UB. Or that you had some complicated structure, and free'd a pointer without getting rid of all the references. Once again, a dangling pointer. In any of those cases, checking the address is unnecessary. In the latter two cases, any bugs resulting from the valid assumption that two pointers weren't overlapping/dangling would be the programmer's fault for writing broken code. Had the dangling pointer been set to NULL before being thrown into the array, then it would have been possible to check it, and free wouldn't have a problem with it, either -- free is perfectly happy accepting a null pointer and doing nothing with it. When you're writing your code correctly, though, "accidental duplicate pointers" isn't a thing. The common causes of bugs are overlapping memory (which is *always* the result of a programmer error), and doing things you shouldn't do with null, void, dangling and wild pointers. All of those things will lead to some form UB. And when you have UB, you can't possibly know what the compiler, nor your program, will do. A program that relies on UB is meaningless nonsense and should be considered broken.
@BradenBest
@BradenBest 8 жыл бұрын
I know this is a pedantic approach to programming, but when you're as close to the machine as you are when programming in languages like C, C++, or Fortran, you really can't afford _not_ to be pedantic.
@BradenBest
@BradenBest 8 жыл бұрын
***** "but there are other ways you could get hold of pointer too." For heap allocation? All allocations go through the brk/sbrk and mmap system calls (more generally: all allocations go through the kernel). What a typical implementation of malloc does is allocate a block of memory (measured in pages) using those system calls, and it uses the obtained block of memory to make further allocations. It only requests more memory when its heap has run out of space. But that's just one optimized implementation of malloc. _Regardless_ of how malloc is implemented, it must obtain the memory by explicitly requesting it from the kernel with a syscall. You cannot allocate memory in any other way. The kernel will not allow it. The only other way is to use what you already have allocated on the stack. And unless you are an _exceptionally_ incompetent programmer, it's pretty hard to fuck up memory management on the stack. So no, there is no other way to obtain a pointer to newly allocated heap without invoking undefined behavior. Whatever function you call or operator you use to obtain it, it's invariably going to result in a syscall being sent to the kernel. "Anyhow, lets say you have a program that keeps track of houses and what persons live in the houses. The House class contains a list of pointers to all the persons that live there. The House class has a function that can be used to make a person move in. When the function is called a pointer to the Person object is added to the list." Where does it get the information on which person to move in? Probably the user, right? And how does the user know that the pointer exists? Why isn't there a pool of "available" people that has a pointer removed when it is moved into a "house"? It also doesn't make sense for one person to live in two houses at once. Your mistake here is allowing multiple references to the same object to exist without tracking all of them. This is how you get dangling pointers. "It doesn't make sense for a person to live in the house twice so before adding the person the function loops through the list of pointers to make sure it doesn't contain the pointer to the person that is about to move in. If the person is found to already live in the house the function could simply ignore the request, or it could generate an error if that is more appropriate." You _could_ do that, but the design is already broken. The fact that you have to check for the pointer to make sure it isn't added twice proves that you have multiple references to it and aren't tracking them. The *only* correct way to handle multiple references is to keep a list of them and set them *all* to null after freeing the space, making it impossible to have a dangling pointer. I believe C++ calls this a smart pointer. Sorry, your algorithm is flawed.
you will never ask about pointers again after watching this video
8:03
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 346 М.
Buckys C++ Programming Tutorials - 38 - Introduction to Pointers
6:07
POINTERS in C++
16:59
The Cherno
Рет қаралды 1 МЛН
the TRUTH about C++ (is it worth your time?)
3:17
Low Level
Рет қаралды 838 М.
Buckys C++ Programming Tutorials - 27 - Random Number Generator
9:53
Buckys C++ Programming Tutorials - 31 - Recursion
8:19
thenewboston
Рет қаралды 551 М.
C++ Super Optimization: 1000X Faster
15:33
Dave's Garage
Рет қаралды 336 М.