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

3

u/Nerdz2300 Apr 05 '22

Pointers. I still dont understand them. They are used a fair amount in embedded code as well. I just dont get it.

Also unions (not the labor kind).

2

u/ExtraFig6 Apr 06 '22

Are you familiar with RAM and memory addresses?

1

u/Nerdz2300 Apr 06 '22

Not really, I mean, the compiler takes care of that for me I thought. On the micro level I know how they work, but not on the macro level between compiler and hardware.

1

u/ExtraFig6 Apr 06 '22

Are you familiar with how this relates to pointers?

1

u/Nerdz2300 Apr 06 '22

Thats the issue I think I have. I know it points to a specific spot, but I dont see how or why thats useful.

1

u/ExtraFig6 Apr 06 '22

Think about linked lists. Each node holds a pointer to the next. What would you do without pointers?

0

u/warlaan Apr 06 '22

Unions (in C++ at least) are basically a "free pass" for casting.

Let's say you have a Customer class that has one member of type int for the customer id. You also have a class "NetworkInt" that serializes an integer over the network.

When you want to send a customer over the network you would want to turn the Customer into a NetworkInt, which in C++ is not a problem since they are both of the same size. They are in no way related, so just casting one into the other is no allowed, but you could get a pointer to the Customer and cast it to a pointer to NetworkInt.

Or you could just declare a union that contains one entry for a Customer and one entry for the NetworkInt. That tells the compiler that whenever an instance of that union is created it reserves the space or either of the "members" in that union. And then you can store something in union.customer and read from union.networkInt. Since the union uses the same memory for both you will effectively be casting between the two, even if that is formally not allowed.

This example should give you a sense that there hardly ever is a good reason to use unions. One notable exception is classic code from Microsoft where a 32bit int can be used to store two pieces of information, so such an int may be used as one 32bit number or as a combination of two 16bit numbers. By using a union you can either access the whole 32bit number or the 16bit high and low word separately.

0

u/ExtraFig6 Apr 06 '22

even if that is formally not allowed.

It's not allowed because some comepilers will optimize assuming you don't do this except in special cases. You should probably use memcpy or bitcast instead

1

u/warlaan Apr 06 '22

As I said: "This example should give you a sense that there hardly ever is a good reason to use unions."

1

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

Every variable has a place somewhere in memory. Think of a street with houses. When you write int x;, "x" is the name of a new house in that street. When you write x = 4;, a delivery van drops out a 4 at the house called "x". Makes sense?

So, each house also has an address. A street number.

If you write &x you get the address of x. If you write *address you get the value at the address. If you write *(&x) you just get the value at the address of x, ... so just x.

You would think that the type of an address is an int – a whole number, but that's not the case. I think of addresses as numbers, but in C they have special types. An address of an int is written as int* and an address of a char is written as char*.

Some people write the star with a space like this:

int x = 4;
int *anIntAddress = &x;
    // = "If you'd dereference anIntAddress, you'd get an int."
int value_at_that_address = *anIntAddress;  // like this

I find this less confusing:

int x = 4;
int* anIntAddress = &x;
    // = "anIntAddress is of type: int-address".
int value_at_that_address = *anIntAddress;

The special types for addresses instead of ints helps with safety, so the programmer and the compiler knows when a variable contains an address and which type it can be "dereferenced" to.

Another thing is "pointer arithmetic".

If you add 1 to an int-address, it actually adds four bytes, but if you add 1 to a char-address it only adds one byte.

char* text = "Hello";   // put five characters into memory and give me the starting address
char c1 = text[2];
char c2 = *(text + 2);  // actually the same thing – the value at the address stored in text + 2

Pointers are just like indexes to a big array.

There is another concept called references, that look like this int& x. They look easier, but I find them actually more complicated. They are kind of secret pointers. I'd ignore them at first until you've understood pointers.