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?

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.