r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

5

u/SlyHolmes Apr 05 '22

Pointers made me quit C++ and programming. Any chance anyone has any good resources/videos explaining them?

0

u/[deleted] Apr 05 '22

[deleted]

5

u/Autarch_Kade Apr 06 '22

And if someone wanted a ride in your car, but you weren't a pointer, you'd build an entirely new copy of your car in front of them to give them a ride in, while the other car stays in the lot. This is expensive in time and resources.

1

u/thetrailofthedead Apr 06 '22

I started my journey with CS50, and it actually does a great job of explaining pointers.

But then good ol' references came along and pass by value / reference parameters and now it's all jumbled up in my head when I try to remember all of the differences.

It's like a word that I once learned the correct spelling of, such as "labor". Then one day I see it spelled "labour" and even though I end up working it out in the moment, my memory of how to actually spell the word is gone forever, and I'll second guess the spelling for the rest of my life.

5

u/ruat_caelum Apr 06 '22 edited Apr 07 '22

So Let's look at "low level memory"

In computer hardware we have a "register" in the CPU it can hold 32 bits or 64 bits (depending on the architecture of the CPU.) Some are 8 bit, etc.

If we have a 32 bit computer we can fit 32 bits of information ONLY.

  • 4 GB is the theoretical maximum of memory (ram) you can use with a 32-bit OS

  • Why?

So why can we not use 8 gigs or 16 gigs of ram in a 32 bit computer? Because the computer cannot access the rest of that memory.

Why not?

Each "memory location" in Ram has an address. 0 is the first 32 bits EDIT 8 bits/1byte of memory in ram. Then 1 or 0000000000001 is the "2nd" address which also hold 32 bits 8 bits of information, but these are offset so if we counted from the beginning we would have 8-15 (address location)

Eventually our 32 bit REGISTER can reach the LAST ADDRESSABLE memory location at 11111111..11111 (thirty-two 1's)

232 = 4,294,967,296, which means a memory address that's 32 bits long can only refer to 4.2 billion unique locations (i.e. 4 GB).

  • Okay so what does that have to do with pointers!

  • In the very low level CPU all that is happening is data is being moved from somewhere TO BE ACTED ON, and then moved somewhere else (possibly back to the same place it came from.)

  • to move data from memory location 0, we us a pointer to say to the CPU "copy 8 bits from memory location 0 to register A"

  • The "pointer" is pointing at the 32 bits 8 bits that start the first 32 bits 8 bits of memory (because we asked for position 0)

  • Now the DATA held in those first 8 bits could be anything. Our program is going to move the DATA from that LOCATION to the REGISTER and say Add 3 to it (just as an example of something to do to the data.)

  • Pointers then "grew" from this very low level concept into something greater in terms of programming, but in general it is all the same: There is an ADDRESS and a VALUE IN THAT ADDRESS

  • so a pointer to a class is just an ADDRESS or LOCATION of that class in memory, and a pointer to an INT is just a LOCAITON of SIZEOF(INT) in memory, etc.

  • When we use an ARRAY what we are doing by saying X[0] is really POINTING to a memory location that holds a specific size, (so if X was defined as INT then it holds that many bytes, or CHAR then that many)

  • So the easiest place to understand pointers is arrays.

  • If I say Int array x[] = [1,10,90,2]

  • The VALUE of x[0] == 1

  • The "pointer" to the value is x[0] which is just a location

  • the value (currently) is 1.

  • If I made a program that does x[0] = x[0]+x[1]

  • The program can run as many times as it wants. X[0] and x[1] will always be pointers but the VALUE (at least at x[0]) will change as the programs moves forward while the pointer stays the same.

  • EDIT I mistakenly said the memory address (which is 32 bits) would access 32 bits of memory. It does not it accesses 1 byte or 8 bits of memory. So while the address is 32 bits wide the size of the memory at that addess is not 32 bits, but instead 1 byte/8 bits.

2

u/inaruslynx2 Apr 05 '22

I could give you a run down of what they are. C++ is not very high above machine languages. It allows for hardware control. Part of hardware is memory. Memory is addressed. There is a lot of addresses in modern machines. With a pointer you are able to save the address of a variable.

Now why would you do this? When you pass a variable into a function c++ creates a spot in memory for this copy variable. If you change the value of this copy, the original is unaffected. If you pass in a pointer in and use the address, if you change the value, you change the original variable and not a copy.

This is useful if you don't want to return a whole lot of variables in the function.

1

u/__Fred Apr 06 '22 edited Apr 06 '22

You can learn other languages than C++ that don't need you to understand pointers, like Python. Java or JavaScript look similar to C++ and you also don't need to understand pointers. (You kind of do with Java.)


I think of pointers in c++ like indexes to a giant array. A pointer is a variable that holds a memory location.

&foo = index of foo in the giant array of all memory

*44859375 = data at the index 44859375 of the giant array

*(&foo) == foo

Normally, you would of course use a variable and not a literal number.

All the tricks you can do with array-indexes (or "indicees"), you can also do with pointers. For example you have an array with huge datatypes and you want to return a search result. You can just return the index pointing at the result instead, if you don't want to copy the whole structure/object. Similarly you can return the memory address of an object.