06
Mar

You do. It is really bad. I have seen your code. It saddens me to the point where I wanted to cry little tears of "what the hell were you thinking" all over your Java programming ass. I swear, if I were writing a program right now, I would make a variable string iSuckAtPointers = "<yournamehere>"; and later a pointer string *whoSucksAtPointers = &iSuckAtPointers; From there I would use the whoSucksAtPointers variable throughout the entire program, not just to prove a point, but to show that you are in fact, the worst coder ever. Welcome to C++. We actually do this thing called "rationalizing logic" here - lets try to accomplish it today.

Java has you spoiled. Java is like your rich daddy who buys you anything you want. Unfortunately for you, you never asked daddy to buy you any sort of intelligence. I am going to make up for this. Just as daddy, I will spoil you, but instead of buying you a boat when your pony is sick, I will teach you something useful.

But of course you cry out "Why daddy? Why must I learn about pointers?" Because real coders use pointers. Pointers "point" to a specific hexadecimal location in memory, and if done correctly, the pointer will "point" to an object. This proves useful when you have to pass large amounts of data to a function or when creating copies of variables.

While you will rarely have to use a pointer for a primitive data type such as "int", it greatly simplifies the explanation and theory of a pointer, so I will use it for most of the examples.

  • 1.
  • int hacker = 1337;
  • 2.
  • int *pwnHacker = &hacker;
  • 3.
  • *pwnHacker = 0;

The variable hacker is initialized with a value of 1337. Then a pointer pwnHacker is created to point to the memory location where the variable hacker is stored. Finally, the pointer is used to set the value of hacker to 0. Very simple and easy. The "*", or "unary dereferencing operator" is used to indicate we are creating a pointer variable, and the "&" is called the "dereferencing operator". The "*" is simply the inverse of "&". For example, *&hacker=1337; is equivalent to hacker=1337; Be sure to include the & preceding the variable you want to reference. Failure to do will make the pointer point to some random space in memory, which could ultimately cause your program to crash.

Instead of the last line being *pwnHacker = 0 lets make it *pwnHacker += 1.

  • 1.
  • int hacker = 1337;
  • 2.
  • int *pwnHacker = &hacker;
  • 3.
  • *pwnHacker += 1;

What do you think will be stored in hacker? I know, daddy is not around with his calculator, but lets just pretend you are smart and capable of something a first grader can accomplish. Did you come up with 1338? Congratulations. Math skills +1. Lets change the code up up more time...

  • 1.
  • int hacker = 1337;
  • 2.
  • int *pwnHacker = &hacker;
  • 3.
  • *pwnHacker++;

I knew it. You guessed 1338 again. WRONG. I cannot believe I have to teach you about the rules of precedence in programming... God must be punishing me for being better than you. *pwnHacker++ is not equivalent to *pwnHacker += 1. *pwnHacker++ actually changes the memory location of the pointer to one unit higher than what it originally was, while *pwnHacker += 1 changes the value that is pointing to. Easy enough to remember? I hope so. Do not do anything rash as printing out this tutorial for reference or anything...all this swearing and abuse would lead your boss to believe that you are really a masochist suicide threat.

Finally, we are going to go over one more thing before your tiny Java head explodes. Comparing pointer variables is not that hard. Its very straightforward.

  • 1.
  • int foo = 5;
  • 2.
  • int bar = 5;
  • 3.
  • int *fooPointer = &foo;
  • 4.
  • int *barPointer = &bar;
  • 5.
  •  
  • 6.
  • bool a = (fooPointer == barPointer);
  • 7.
  • bool b = (*fooPointer == *barPointer);

No, they are not equivalent. Stop thinking they are. (fooPointer == barPointer) checks to see if the two variables are pointing to the same memory location, while (*fooPointer == *barPointer) checks if the values stored in the addresses are equal. Very simple.

Next week I am going to over the "new" operator and how it is used with objects. Hopefully by next week you will have stopped sucking, but I doubt it. God does not grant miracles like that often.

Note: This blog entry is a play off of the hysterical Donnie Hoyle and his You Suck At Photoshop Series.

11:23 PM, Thu Mar 6th 2008
by Hellkat

Hahahaha. Thats probably the funniest programming tut I've ever read. Keep up the good work.

12:30 AM, Fri Mar 7th 2008
by Stephen

"Pointers "point" to a specific hexadecimal location in memory".

As opposed to a specific decimal location in memory...

6:01 AM, Fri Mar 7th 2008
by Anonymous Coward

Ran into the following last night:

unsigned long long int***

:s

9:45 AM, Fri Mar 7th 2008
by Anonymous Coward

"The variable hacker is initialized with a value of 1337."

Umm...where?

9:49 AM, Fri Mar 7th 2008
by Roo

You suck at examples. For the love of Knuth, please include all variables that you are referencing in your examples *in your fucking examples*.

1:56 PM, Fri Mar 7th 2008
by kometbomb

While not trying to sound elitist or overly negative, I have to comment on the fact pointers 101 seems to be more than elementary knowledge in C. I respect the attempt to educate the masses, but I also feel things like pointers should be introduced as something else than a weird feature used by ultramacho code wizards. At the very best, that will only mystify such basic programming concepts even further. Thank you.

1:57 PM, Fri Mar 7th 2008
by Wishmaster

@Anonimous coward whose post is two posts above mine - See table, first row "int hacker = 1337;" ???

That's where.

Funny example and nice, simple tutorial, but the whole concept of pointers, while neccecary, is at the same time ridiculous. makes shooting yourself in the foot too easy.

"You accidently create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying "That's me, over there.""

So very true : /

4:31 PM, Fri Mar 7th 2008
by Eric

I'm so glad I'm doing Java now and don't have to bother with that crap anymore.

5:04 PM, Fri Mar 7th 2008
by Will

I agree with Eric. You seem to be saying that pointers are useful, but you provide no examples where there isn't a Java equivalent. There's deep/shallow copies in Java too and it's much nicer. :)

5:13 PM, Fri Mar 7th 2008
by sir jorge

Those are really cool.

7:02 PM, Fri Mar 7th 2008
by Adrian Rosebrock

I fixed a lot of the problems with the comment system. I'm sorry to those who tried to comment but only found an error. My blog is hand coded and so far the small bug in the comment system has been the only error.

Anonymous Coward was saying that all my example code was there. The example code was not stored in the database correctly - that has been fixed now. The entire example code is there.

Also, I am not saying that Java cannot do most of the things C++ can do. It can. However, there are instances in which C++ is preferred or even required over Java. If you are using C/C++ you need to know how to use pointers.

8:28 PM, Fri Mar 7th 2008
by Hater

Java sucks. Honestly I don't trust code that other people write and just assure me that it will work with what I need without letting me see it easily. A language that revolves around these hidden classes is simply bullshit. I'll make it myself, and do it better. Then I'll actually compile it, and it will actually run fast.

8:31 PM, Fri Mar 7th 2008
by Nick

I really agree. Pointers are just about the most important tool you have for manipulating data. Not to mention the flexibility and better utilization of memory. I think programmers should start out in basic assembly. THEN they will truly understand the significance of pointers.

8:52 PM, Fri Mar 7th 2008
by Jeremy Williams

Nothing like a good old "motivational" tutorial to give you the warm fuzzies, right?

Really, there is nothing warm or fuzzy about pointer hell and as getting your operators right will help people avoid that horrible, evil place... They'll be glad for the strictness in the end.

They'll thank daddy someday!

9:34 PM, Fri Mar 7th 2008
by spayced

I liked it. I agree that pointers are not fun and can be avoided if we wish, but there is no good reason to refuse to learn them.

What is happening is programming is becoming sufficiently advanced that there are totally different people writing lower level languages (assembly, C) than upper level languages (java, visual basic). Some people need to create quick and dirty code to get a job done, others need to establish efficient running code to underpin systems. In a perfect world, we quickly make AND efficiently run code, but this isn't a perfect world. We have to trade overhead for convenience. There is no right answer, everybody works on a different level.

Don't argue with each other. Recognize we tackle problems differently, and help each other. He's trying to help with these tutorials and you shouldn't be angry, his combative tone is just sarcasm.

10:10 PM, Fri Mar 7th 2008
by mjt

thanks for the "pointers 100" lesson (no, not "pointers 101"). this isn't even an intro.

that's the problem with today's blogging world, complete with Stumble. everyone with an Internet connection and a bit [sic] of high-school (OK, and a little bit of college) programming is a self-appointed expert.

when you've written [lots of] code next to the metal, then maybe you can enlighten us.

regards, lis ...

3:18 AM, Sat Mar 8th 2008
by kometbomb

I love the heated discussion any mention of pointers always creates. I believe the valuable information is in the post comments. So, in a perverse way not so great posts could lead into good source of information if you just scroll down a bit.

I agree on the learning pointers via assembly thing. To me, doing exactly that was what made pointers, well, concrete. I also think most pointer tutorials try to explain too many things at once. They should start with introducing the concept of a memory address (which is a very, very alien thing to most people who have never really thought of it). Then, they should introduce the idea that a pointer is just another number which equals to an address. And only then they should hop to incrementing pointers by the object size and so on.

And all that with a lot of pictures. Seeing how a variable occupies four blocks of memory really makes a difference.

In fact, I think I'll write up something for everyone to flame about when I have time.

7:21 AM, Sat Mar 8th 2008
by Captlobo

Hater: you obviously never programmed for a living. "I'll make it myself, and do it better." The same probably applies to your home, car, computer, OS,...

Beginners make excessive use of existing code, advanced programmers do everything themselves. But the best ones know when not to reinvent the wheel.

Have fun writing the 105577th implementation of a linked list.

The general discussion is pretty pointless anyway. A lot of code out there, including most commercial applications and many of the open source ones, is politely put suboptimal. There are not many projects out there that follow the so-called good practice emphasised in handbooks and courses. I find most of the comments idolising one paradigm or another come from students. (Just like 1st graders trying to explain quantumphysics because they had a 101 course...)

9:09 AM, Sat Mar 8th 2008
by Henrik

Why have 2 variables point at the same value? is it to confuse everyone? In Java, all but primitives are pointers. You have to do extra work to make something immutable. If not apparent, concurrent programming will pound this into your head

9:52 AM, Sat Mar 8th 2008
by David Mills

Actually, in this context, the '&' is the "address-of" operator, not the "dereferencing" operator. Maybe you should read my pointer tut.

10:57 AM, Sat Mar 8th 2008
by Andreas

ITT: hobby programmers

12:50 PM, Sat Mar 8th 2008
by Anonymous

In b4 real programmers program in binary

6:18 PM, Sat Mar 8th 2008
by Egon

Just like Stephen said: you suck at math. To reference a location in the memory as a hexadecimal location, is proving you don't know shit about programming, nor math. Hexadecimal is just a way of representing a number, as is a pointer just a way to refer to data. All beginning programmers make this mistake by saying: why do we use 0x0A, and the other time we use 10. It's the f*cking same thing. There is NO SUCH THING as a hexadecimal location, there is such a thing as an address but it can be shown in binary, ternary, decimal, octal, hexadecimal, ...

So, you can think you are a 1337 programmer because you get the concept of pointers. Real programmers (and I consider myself to be a lesser programmer than a real programmer, but certainly worth more than you are) will laugh at what you state. If one makes such mistakes, the rest of you post isn't even worth reading.

For people who don't know much about math: it's almost the same as stating that some date in the Jewish/Muslim calendar isn't the same as the Julian/Gregorian (=Western) date. It is exactly the same date, only the way of counting is a little different. Jews and Muslims live at the same day as any other people; they just use other ways to represent time.

The same way: a hexadecimal, decimal, octal, binary ... representation of a number all represent the same number, but they all look different; the concept of the number is the same. Another example: different languages. The French might say "un lapin", the Dutch might say "een konijn", ... , the English might speak of "a rabbit" but they all mention the same object/concept of a rabbit.

If you, as a '1337', can't understand what a concept is, you aren't even worth to be called a programmer.

11:05 PM, Mon Mar 10th 2008
by rj ryan

aww that's nothing :).. you didn't even do double/triple/etc pointers, function pointers, or const pointers!

const pointers are the screwiest imho:

Say we have a class, Object. What's the difference between these three pointers:

Object* const o1 = new Object(...)

const Object* o2 = new Object(...)

const Object* const o3 = new Object(...)

gah const is such a trainwreck. Anyway.. here goes:

o1 is a const pointer, you cannot re-assign it to another location.

o1++ fails, but *o1 = *o2 is valid.

o2 is a pointer to a const Object*. you can reassign o2, but not modify *o2

o3 is a const pointer to a const Object*, you can't do o3 = o1 or *o3 = *o1

name:
e-mail:
website:
comment:
creativecommons.org by-nc-nd
© chaoscoding 2008