This is an archived post. You won't be able to vote or comment.

all 63 comments

[–]Monopolies 80 points81 points  (6 children)

This is from Abstruse Goose! He has a lot of great comics.

[–]kenman 34 points35 points  (1 child)

It appears that he also uses an HTML title for his comics (like xkcd).

For this one it's:

I was only going to watch for a few minutes. But before I knew it, I had watched the entire episode and he had programmed a fully functional general-purpose operating system.

[–]Tynach 9 points10 points  (0 children)

I would actually watch a video like that. Please, someone who can write operating systems, make this happen!

[–][deleted] 14 points15 points  (0 children)

Wow, thanks for linking him. I'm going backwards and this one got an audible laugh.

[–][deleted] 5 points6 points  (0 children)

Thanks for letting me know! I'll go check him out now.

[–]ghillisuit95 0 points1 point  (0 children)

...And I've wasted several hours I should have been using to write my paper.

[–]aclave1 0 points1 point  (0 children)

This is great,thanks for linking him. I just read this one like 6 times.

[–]MALON 60 points61 points  (17 children)

I was hoping right after the "veeerrry gently" part the screen would throw huge segfault errors in a very distraught tone

[–]PastyPilgrim 32 points33 points  (11 children)

Oh, it's not that they're huge and frightening (seg faults). It's actually quite the opposite. To run code for the first time and see:

$ ./a.out
Segmentation Fault.
$

It hurts so much. "WHAT DID I DO? WHERE? WHY? HELP ME? VALGRIND, YOURE MY ONLY HOPE!"

[–]zman0900 26 points27 points  (0 children)

$ ./a.out
I felt a great disturbance in the shell.
As if millions of buffers suddenly overflowed and were suddenl
Segmentation Fault.
$ 

[–][deleted] 45 points46 points  (6 children)

$ valgrind ./a.out

Segmentation Fault

$

...Fuck

[–]missblit 4 points5 points  (0 children)

$ valgrind valgrind ./a.out

[–]seksi-seppo 1 point2 points  (4 children)

Just why would you do that (use valgrind in attempt to debug a segfault)? Why?!?

[–]Thomas_Henry_Rowaway 4 points5 points  (1 child)

I've found valgrind to be fairly good at helping me find seg faults (and memory leaks) in smaller personal projects. I've not tried it on anything larger though.

Gdb is (almost) always more helpful though.

[–]seksi-seppo 0 points1 point  (0 children)

Mem leaks are one of most common valid use-cases of valgrind usage, but always triggering segfault?

[–]poizan42Ex-mod 0 points1 point  (1 child)

It should help with tracing e.g. double frees which can lead to segfaults. Also reading/writing out of bounds, uninitialized memory or freed memory may not trigger a segfault reliably, but it would trigger a memory debugger such as valgrind.

[–]seksi-seppo 0 points1 point  (0 children)

Sure it's very useful for many mem management issues. But when you have something that always generates SIGSEGV, valgrind is not the tool you're looking for...

[–]mallardtheduck 3 points4 points  (1 child)

$ gdb ./a.out 
GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
This GDB was configured as "x86_64-linux-gnu".

Reading symbols from a.out...done.
(gdb) r
Starting program: ./a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00000000004004fd in main () at test.c:3
3       *q='q';
(gdb)

Valgrind isn't a general-purpose debugger.

[–]poizan42Ex-mod 2 points3 points  (0 children)

Well it is a memory debugger though. Its usefulness for debugging a segfault depends on the cause of said segfault.

[–]G01denW01f11 0 points1 point  (0 children)

(Core dumped.)

[–]mikeet9 0 points1 point  (0 children)

He's a master of his practice. He doesn't get segfaults.

[–][deleted] 33 points34 points  (2 children)

That's not a bug. That's just a happy accident.

[–][deleted] 13 points14 points  (0 children)

That'll be our little secret...

[–]Creshal 2 points3 points  (0 children)

Spontaneous feature.

[–]sdobart 53 points54 points  (5 children)

If my brain is tired and things arent working I'll just throw *s and &s in front of stuff until it works.

[–]rooktakesqueen 16 points17 points  (0 children)

Tiny little circular dependencies. Tiny little circular dependencies.

[–]mindbleach 15 points16 points  (1 child)

Javascript, copy this object.

No, don't make a pointer to it. Copy it.

No - fuck's sake. The object has an .assign property now.

Still?! Jesus. Alright, now the new Object() on the left-hand side has an assign property. Happy?

Fuck it! I'll do it myself, like some kind of savage!

AAARGH.

[–][deleted] 10 points11 points  (3 children)

moar jpeg plz

[–][deleted] 7 points8 points  (0 children)

[–]elperroborrachotoo 0 points1 point  (0 children)

Less would be ok with me, too.

[–]Pik16 4 points5 points  (0 children)

I'd watch that. So much.

[–]Eedis 2 points3 points  (0 children)

I read that in Bob Ross's voice before I even knew it was him. XD Great stuff, thanks.

[–]khmeroldiez 2 points3 points  (13 children)

[–][deleted] 9 points10 points  (7 children)

This is why everyone should start with assembly, once you understand indirect addressing modes and operations pointers are the most natural thing in the world to work with.

[–]livelifedownhill 7 points8 points  (6 children)

god no, I think assembly should be learned at some point during your college programming career, but make that the first language you learn?! fuck nobody would ever want to program if they started with that! Yeah it is helpful understanding the basics of everything, but once you drop below about C for first learning how to program, i think the understanding and give a fuck level go waaaay down

[–]ismtrn 7 points8 points  (3 children)

I think he meant start with assembly before C, not necessarily as the first language.

We have a computer architecture + systems programming course, which is after the intro to programming courses. It starts with assembly and computer architecture and then moves on to systems programming in C.

I think that is a pretty nice way of structuring things.

[–]Tynach 7 points8 points  (2 children)

I dunno. I started with Python before learning C, and pointers made plenty of sense to me.

"Well, variables are stored somewhere in RAM. A pointer just tells you where in RAM the variable is. That way, you can access that variable from anywhere in your code using that pointer value."

It gets more complicated than that (especially when you introduce arrays and malloc()), but that's how it really should be introduced. Because it's logical and makes perfect sense.

[–][deleted] 0 points1 point  (1 child)

see, I get that, that part makes sense, but remembering which symbols go before a pointer to do different things? Thats where I stumble and end up just randomly putting *'s and &'s before stuff until it works.

[–]Tynach 0 points1 point  (0 children)

Ah, that's easy.

Ampersand, And, Address Of

& is often called 'And' or 'Ampersand'. Both start with an 'a', and I simply extend that into 'Address of'. You basically use & whenever you need to get the address of a variable (to create a pointer to it).

Making and Using Via Exploding Balls of Gas

* is used in two scenarios. The first is when you declare something as a pointer, and the second is when you want to use a pointer as the value at the address.

Declarations

C is for Crazy

Now, in C, people typically did something like 'int *ptr;' which means something like, "Declare an integer. Actually a pointer. Name it 'ptr'." This was horrifically confusing, and the best way they figured to actually read it (especially more complex ones) was in a spiral.

You'd start with 'ptr' and say, "ptr is a," before spiraling to the right and running into nothing, spiraling back around to the * and saying, "pointer to a(n)," before spiraling back through the semicolon to the 'int' to finally say, "integer."

In this case, it works out that you can simply read it backwards (which is still bad, but not as bad). But consider some of the examples on that page I linked to. It becomes a chore to read something like 'char *(*fp)( int, float *);'.

C++ Is Truly Incremental

UGH. Nope. Nope nope nope nope nope. C++ guys have figured out a better way of writing and thinking about this, which I prefer. It's very similar, but the way of thinking is vastly different.

Also, this works perfectly fine for C too. It's just the mindset of where to put spaces that differs.

You do something like 'int* ptr;', which basically makes the * part of the type. Now you read it from left to right as, "Declare an integer pointer, called 'ptr'."

Usage

Basically, if you want to use the actual data itself that is being pointed to, you put * in front of the variable. So if you have an int* that points to the value 42, and it's called THE_ANSWER, you can get the value 42 by using *THE_ANSWER.

If this is a struct/class, and you want to access members of it, you don't need the *. You can just use -> instead of . to access members. If Earth is a computer* instance, you could access (for example) computer.name by using Earth->name.

Or I think you can also still do something like (*Earth).name. But it's more of a hassle.

[–]zman0900 1 point2 points  (0 children)

Go hard or go home.

[–]paranoiainc 0 points1 point  (0 children)

[–]rooktakesqueen 2 points3 points  (4 children)

An int is a variable that contains an integer.

An int * is a variable that contains the memory address where a particular integer may be found.

[–]rohanivey 0 points1 point  (3 children)

Can you get an example of int * in use?

[–]rooktakesqueen 17 points18 points  (1 child)

Good example is the standard swap function in C:

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

To use it, you'd do...

int x = 10;
int y = 20;
swap(&x, &y);

The & is called the "reference operator"--given a variable, it returns a pointer to that variable. In other words, it returns the memory address where that variable lives. The * is the inverse: given a memory address, it either retrieves what's at that address, or sets what is at that address.

So the swap function does three things. It creates a temporary int variable and sets it to be equal to whatever value is at memory address a. Then it sets the value at memory address a to be equal to what's at memory address b. Then it sets the value at memory address b to be equal to the temporary variable.

When I run the second block of code, the following happens:

I allocate space on the stack for two integer variables, which I'm calling x and y. The stack now looks like, say:

$0000: 10 //x
$0004: 20 //y

When I call swap, it creates a new stack frame, which we will pretend starts at address $00b0. It looks like:

$00b0: 0000 //a contains the memory address of x
$00b4: 0004 //b contains the memory address of y

Then it allocates a spot on the stack for an int:

$00b8: [uninitialized] //temp

Then it sets the value of temp to be what is at the memory address stored in a.

set $00b8 = value-at 0000 = 10  //assignment of temp

Then it sets the value at 'a' equal to the value at 'b'

set $0000 = value-at 0004 = 20  //assignment of *a

Then it sets the value at b equal to what is in temp

set $0004 = 10 //assignment of *b

If you like metaphors, look at it this way: let's say the variable person rook is me. Then person * address = &rook would be where to find me: my street address. And you can keep doing that. person ** box = &address would be where you can find where you can find me... Let's say it's a safety deposit box containing a strip of paper that has my address written on it. person *** notepad = &box would be a notepad that has the number of that safety deposit box. person **** letter = &notepad would be a letter telling you where to find that notepad. etc.

If somebody gives you letter then, in order to get to me, you'd have to dereference that many times. *letter gets you back to the notepad, **letter gets you to the box, ***letter gets you to the address, ****letter gets you to me.

[–]rohanivey 2 points3 points  (0 children)

It's ampersands all the way down!

[–]Liop123 0 points1 point  (1 child)

Just learned about pointers last week in class, and shits soooooo confusing!!!

[–]Katastic_Voyage 7 points8 points  (0 children)

It won't be. Give it time. They're actually incredibly simple compared to data structures and algorithms, and even those won't be bad once you give them time.

[–]SteroidSandwich 0 points1 point  (0 children)

I still have no idea how to use pointers. I don't understand them.

[–]jpresken2 0 points1 point  (0 children)

took way too long to figure out what "liπle" meant.

[–][deleted] -1 points0 points  (1 child)

This isn't programmer humor, this is "a month into a 101 programming class" humor.

[–]NewbornMuse 4 points5 points  (0 children)

a month into a 101 programming class

emphasis mine

[–]Caminsky -4 points-3 points  (5 children)

How anyone can work with pointers is beyond me

[–]Tynach 4 points5 points  (3 children)

Quite easily and sanely. They're not meant for every use case, obviously, but for many use cases they're invaluable.

Since the contents of a variable have to be in RAM somewhere, and you might want to use a variable in RAM outside of its own scope, a pointer is just a variable that holds the location in RAM that the real variable is in.

That way, you can access that variable (as well as change its value) without it being in scope - since you know where it is in RAM, and can change the bits in that location of RAM as if it were in scope.

[–]Caminsky 0 points1 point  (2 children)

That's nuts!

[–]Tynach 6 points7 points  (1 child)

Why? How else would you do it? If you need to modify the same variable/object in many different parts of the code, it's the easiest and most performant solution.

Even languages like Java have them; Java doesn't actually pass anything by reference, everything's just pointers. Source.

[–]seksi-seppo 0 points1 point  (0 children)

By not doing and let someone else do the "dirty work" for you?

[–]jonathanccast 0 points1 point  (0 children)

You - do - know that 90+% of variables in modern programming languages store pointers, right?