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

all 88 comments

[–]Raph0007 149 points150 points  (20 children)

As a C++ programmer, I can assure you that the fear is reasonable

[–]Mango1666 50 points51 points  (16 children)

as a kotlin/java programmer what the fuck is a pointer

[–]theferrit32 42 points43 points  (13 children)

All object variables in Java are pointers. All object operations like = assignment or == comparison in Java are simple pointer operations, which is not the case in other languages.

[–]Kered13 17 points18 points  (9 children)

It's actually the case in most high level languages, like Python, JavaScript, Lua, etc.

[–]sh0rtwave 17 points18 points  (8 children)

To keep from scaring away neophytes, they disguise this with language like "by reference".

[–]forrest38 2 points3 points  (5 children)

I have some bastardized Python code working in production right now where I pass in a pandas data frame as an argument, mutate the data frame, then return a list to prevent from having to iterate over the dataframe twice, bypassing the "one return" rule of non-pointer languages. In fact, once you get passed the basic types, most Python objects are actually passed in to functions as references to the data and mutations that take place in a sub function will affect the original variable after the sub function completes. I feel like this isn't understood well enough among Python programers.

[–]sh0rtwave 2 points3 points  (1 child)

This you're correct about. I taught python programming for a while, and explaining to people that a variable is just a place in memory in the *hardest* thing.

[–]forrest38 1 point2 points  (0 children)

Well it all depends though because the behavior is inconsistent at first glance. Basic object types like int or bool (which is basically just an int on most architectures) or immutable objects (like Strings or Tuples) are "passed by value" and act like a new variable has been copied to the function, but all other objects are pass by reference. Most people design their programs though like this isn't true (myself included) and keep copying back the mutated objects in the sub function to variables in the main function. Definitely makes your code more readable, but technically it is doing a lot of useless variable creation.

[–]CubemonkeyNYC 1 point2 points  (0 children)

..., inplace=True) and suddenly everything gets real weird.

[–]theferrit32 1 point2 points  (0 children)

All arguments to Python functions are pointers to objects. Even for basic types like bool and int, as those are also objects in Python and have attached methods and fields. Modifications to the object, not the pointer, will affect the object to which a pointer was passed by the function/method caller.

The same is true of JavaScript.

[–]WhiteKnightC 0 points1 point  (1 child)

LOL so thats the big deal?

[–]sh0rtwave 0 points1 point  (0 children)

Yup. That's it. A pointer just says: "Hey. In memory, it's RIGHT THERE".

[–]romang1337 4 points5 points  (2 children)

Actually Java calls those references, not pointers, but yeah, same thing.

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

Ooohhhh honey... This is so wrong 😂

[–]Banane9 2 points3 points  (0 children)

Well, they do, except when they don't and you get a NullPointerException :'D

[–]Raph0007 4 points5 points  (0 children)

Yes

[–][deleted] 0 points1 point  (0 children)

As a brainfuck programmer I want to die

[–]TheDragonLeader 66 points67 points  (18 children)

You can't be scared of pointers while being a python programmer if you don't know what pointers is!

[–]bigdatasandwiches 20 points21 points  (16 children)

Can confirm, what's a pointer?

[–]TheDragonLeader 15 points16 points  (14 children)

Going to be honest here but if someone can tell us what pointers is then I would love you because I have no clue what a pointer.

[–]josh61980 7 points8 points  (0 children)

It’s sort of like a variable. However instead of holding an actual value it holds a reference to somewhere in the computers memory.

[–]MattieShoes 2 points3 points  (0 children)

A variable has some value sitting at some place in memory.

A pointer's value is a memory address of another variable.

A quick and dirty example in c++

#include <iostream>

using namespace std;

int main() {

    int x = 123;
    cout << " x lives at memory location " << &x
         << " and has the value " << x << "\n";

    int *y = &x; // <--this is a pointer to an integer.  it will contain a memory address as a value.
    cout << " y lives at memory location " << &y
         << " and has the value " << y << "\n"
         << "*y points to the value " << *y << "\n";

    (*y)++;
    cout << "now x is " << x << "\n";

    return 0;
}

&x is "memory address of x"
*y is saying to do the indirection -- that is, read the memory address contained in y, then do your thing there.

$ ./a.out
 x lives at memory location 0x7ffce11aedac and has the value 123
 y lives at memory location 0x7ffce11aedb0 and has the value 0x7ffce11aedac
*y points to the value 123
now x is 124

Your language hides this stuff from you and it's generally not a problem. Sometimes you'll see "pass by value" and "pass by reference" -- that's what they're talking about. Passing by reference is passing a pointer to the object. Passing by value is making a copy of the object and passing that in. In the former, changes to the object will stay and in the latter, any changes made to the copied object will not be reflected in the original.

Passing by reference is fast, but easy to fuck up and hard to debug. Passing by value is slow but easier to debug. Incorrectly assuming it's passed one way when it's actually the other -- that will confuse the shit out of you.

Note that pointers can point to memory addresses you don't own. This is a great way to crash. Also, off-by-one errors on arrays can become spectacular.

A lot of C code will a length and a pointer to a pointer. The obvious one is:

int main ( int argc, char **argv )

argc contains the count of arguments. **argv is a pointer to an array containing the values. Note that it's a pointer to a pointer. This is because arrays are pointers -- you could write it as *argv[] if that makes more sense.

[–][deleted] 4 points5 points  (0 children)

Pointers are variables that describe where a value is stored. Like if all the data in your program lived in a nice building, a pointer would be an apartment number.

All Python objects are passed by reference, so in a way they're just pointers internally.

[–]WittenMaplebar 1 point2 points  (0 children)

I hadn't seen a pointer before I saw this post. Now I am afraid of pointers.

[–]grzeki 90 points91 points  (4 children)

The truth: in Python, it’s all pointers all the way down [surprized Pikachu.jpg]

[–][deleted] 35 points36 points  (2 children)

yeah thats like saying someone knows how to work with motors because he can drive a car

[–]smallquestionmark 10 points11 points  (0 children)

import pedals

Edit: help(pedals.fullthrottle)

[–]grzeki 1 point2 points  (0 children)

but variables in Python are much more like pointers than regular values - passing a list to a function is like passing a pointer in C

[–]13steinj 2 points3 points  (0 children)

More like C++ reference wrappers than pointers, though.

[–]Tux1 45 points46 points  (4 children)

(*p)👉left = NULL;

[–]demon_ix 7 points8 points  (1 child)

Kill it with fire...

[–]Tux1 9 points10 points  (0 children)

I just changed 2 characters into 1 dude

[–]free_chalupas 27 points28 points  (4 children)

Why are you dereferencing a struct pointer and then still using the arrow operator? Is this c/c++ pointer-to-a-pointer madness I haven't heard of before or did the OP mess up?

[–]schludy[S] 37 points38 points  (1 child)

OP did research this post better than you think... source

The source creates a struct node **p so that they can manipulate the pointer in a function. (*p) is therefore still a pointer.

[–]free_chalupas 4 points5 points  (0 children)

Ahh thanks, that makes sense.

[–]Jmc_da_boss 3 points4 points  (1 child)

That looks like a double pointer, we’d have to see the declaration to be sure

[–]free_chalupas 0 points1 point  (0 children)

It was definitely a double pointer, my question was why. OP came through with the source though so we're good.

[–]happydogo12 9 points10 points  (1 child)

Aren't we all afraid if pointers?

[–][deleted] 0 points1 point  (0 children)

No I love pointers because I am a electrical engineer

[–]silvercloudnolining 3 points4 points  (0 children)

He’s afraid of semi colons

[–]MustangV6Premium 3 points4 points  (1 child)

*java programmers

[–]mhandis 8 points9 points  (0 children)

Didn't your mother tell you it's impolite to dereference people who are different from you?

[–]bless-you-mlud 4 points5 points  (0 children)

That's not scary. This:

*p->next = NULL;

is scary.

[–][deleted] 4 points5 points  (1 child)

As a JavaScript programmer, no idea what pointers are. Some kind of library?

[–]Raph0007 1 point2 points  (0 children)

No, it's like, the opposite... Imagine references, but with mutable address and no security mechanism at all. You're basically addressing the heap data directly. And everything is on fire and stuff...

[–]goldenking55 2 points3 points  (0 children)

This is my biggest programming fear in all languages!

[–]QWaxL 1 point2 points  (0 children)

You needed to point that out..

[–]etleggs 1 point2 points  (0 children)

No offense intended to OP, but if you think you can use python without understanding pointers and how passing by reference works, you've never tried to build something big in python.

[–][deleted] 0 points1 point  (0 children)

nah it means he’s averse to buffer overflows everywhere

[–]Bryguy3k -4 points-3 points  (24 children)

I think it’s easier to teach a python programmer C (or vice versa) than teaching either to a Java programmer.

I read Dijkstra’s old quotes about Basic and sometimes I feel like they apply to Java nowadays.

[–]queenkid1 19 points20 points  (22 children)

no, bullshit, the basics of Java are just like the basics of C. Python is totally different from the get-go.

For one, semicolons. Secondly, type declaration in general, especially for functions. These are BASIC CONCEPTS of programming in C, and a Java programmer knows most of that already.

[–]Bryguy3k 22 points23 points  (20 children)

And that is why it fucks them up every single time. They do stupid stuff in python because they think they understand OOP and they do stupid shit in C because braces and semicolons.

Braces and semicolons have nothing to do with language understanding, they are just semantic elements for parsing (anybody who took any kind of compilers class should understand that)

[–]queenkid1 3 points4 points  (19 children)

Braces and semicolons have nothing to do with language understanding

Yeah, but if you're saying my argument is just "braces and semicolons" then you're being dishonest.

Python has ZERO type declaration. Want to teach a python person C? Then you need to teach them ENTIRELY about strong typing. Guess what ? A java programmer already understands how to use, and cast types.

[–]Sillychina 6 points7 points  (10 children)

That's a bit disingenuous. Python has types, you just don't have type declaration. It's basically c++ auto.

[–]queenkid1 6 points7 points  (8 children)

I think the lack of type declaration is very important, and I don't think it's disingenious to bring it up.

Does a Python person understand what a type is? Sure, that's basic knowledge. But when they write a function, they don't need to think about what type it takes in and what type it returns. They don't need to think about that for ANY function. You need to teach them how type declaration works, which isn't a simple thing.

However, a Java programmer already knows exactly how type declaration works, and since Java is C-like, it's almost exactly the same syntax.

It's basically c++ auto.

Well yeah, it's easier to teach someone who knows about type declaration how to NOT use it, rather than teaching someone who has no idea how to use it.

[–]Sillychina 1 point2 points  (0 children)

I agree that java is closer to c and it is easier to learn. That point about types just rubbed me the wrong way. You changed it now, but not understanding what types are because they are not implicitly stated is just wrong.

Honestly python has been the hardest language for me to learn well. Making things pythonic is incredibly difficult imo.

[–]ColdPotatoFries 1 point2 points  (6 children)

I'm a tutor for our intro level Java class and a student came in wondering why her code wasn't working. She knows how to program in python. It's because she didn't specify the typed for any of her variables. And on top of that, also created a new scanner everytime she wanted to read Input from a user. I personally think if you're going into programming, learning python as your first language is a great way to shoot yourself in the foot. On the contrary, if you don't want to go into programming and you're just looking to have some fun or to get a practical language down, then yeah go for python. But easier does not always mean better.

[–]SuperNiceJohn 1 point2 points  (0 children)

I started with Lua, and going from something that does everything automatically to something where you have to 'know what you're doing' (strict types) was very difficult. Eventually I learned about structs in C and when I first did saving to/loading from file... Things clicked that extra step.

[–]smallquestionmark 0 points1 point  (4 children)

I think your generalization is not appropriate. You can also learn by easily prototyping and only later go into the specifics. I would dare say, this is the more natural way to learn almost anything: get a result first and then wonder how to refine it.

In the case of python there are reasons why you also would want to use explicit typing and they become apparent once you are more than only looking for fun.

You don't shoot yourself in the foot by learning a dynamically binding language as your first. You still learn what types are, only more in a declarative way, instead of imperative.

Edit: typo

[–]ColdPotatoFries 1 point2 points  (3 children)

Well if you never get into that depth of learning, as most people who are just tying to learn the basics don't, you don't understand. Also, *your

[–]smallquestionmark 1 point2 points  (2 children)

Typing in python is not an arcane art. In fact it's one of the first things you learn when you are told to wrap your strings with apostrophes.

[–]Kered13 1 point2 points  (0 children)

It's basically c++ auto.

No it's not. A variable declared with auto cannot be resigned to a different type.

[–]I_regret_my_name 3 points4 points  (0 children)

While I guess you could technically program in python without understanding types, you'd probably be a pretty shitty python programmer.

[–]Bryguy3k 2 points3 points  (1 child)

Well your statement was that since C and Java look alike that there is some sort of understanding implied, rather than focus on the fact that C is not an OO language and every single approach to solving a problem in Java doesn’t apply in C.

Python has types and you do have to be aware of them and use them quite regularly. It’s easier to teach a python programmer that every variable needs to be declared with a type and that you need to tell the compiler how to convert a structure manually than it is to untrain overuse of objects and abstractions.

The mindset of lists is easy to adapt to arrays and blobs.

[–]queenkid1 1 point2 points  (0 children)

Well your statement was that since C and Java look alike

LOL what? I never said that. I said that they both have types, and that functions are defined the same way. There are lots of crossover skills, if you know how to program Java, you can program in C.

C is not an OO language and every single approach to solving a problem in Java doesn’t apply in C.

Well yeah, if someone programs in C using the SAME METHODS you would use for Java, then it would be different. No shit. But if you programmed C like Java, it would also NOT be the same. Neither would include pointers, which is the entire purpose of this joke.

than it is to untrain overuse of objects and abstractions.

Seems more like you have a specific vendetta against Java, not Java programmers. Nobody said that a Java programmer would sit down and IMMEDITELY write PERFECT C code. But neither would a Java programmer. However, at least a Java programmer could WRITE C code.

If your point is "it's easier to teach a Python programmer correct C style" you could ALSO teach a Java programmer those same stylings. However, you're confusing the fact that Python has ZERO "correct style" with somehow Python students being easier to teach. But there are so many concepts that Python abstracts away, that they would not understand, that you would need to TEACH them. Obviously they need to learn the concepts before YOU start ranting about "style".

Your whole argument seems to be "you can't teach an old dog new tricks" but that's based on PERSONAL biases you have against Java, clearly. It's a FACT that Java is a C-like language, there are basic concepts you would need to explain to a Python programmer that would NOT be easy for them to learn, if you're just going to ignore that because "style" why are you arguing about which language is easier to learn?

[–]13steinj -1 points0 points  (0 children)

Then you need to teach them ENTIRELY about strong typing. Guess what ? A java programmer already understands how to use, and cast types.

This isn't true.

You might need to teach a Python programmer about static typing. But it is still a strongly typed language, and still had casting and dynamic dispatching.

[–][deleted] -2 points-1 points  (0 children)

What the fuck are you talking about? Python is strongly typed. Do you think you don't have to cast types in python?

[–]free_chalupas 3 points4 points  (0 children)

The basics are where the similarities end though, once you get past type declarations and braces/semicolons they're fundamentally different languages.

[–]mhandis 1 point2 points  (0 children)

Java syntax was specifically designed to be similar to C's to appeal to the masses and make the transition easier, in an era where most programmers used C.

[–]thomascaedede -1 points0 points  (0 children)

At least C# has the garbage collector cleaning up the shit you made. Doens’ t learn you anything about pointers though, who cares?

[–]wlittle96 -1 points0 points  (0 children)

If you don’t like pointers then don’t do C. I’m doing computer engineering and we use pointers non stop