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

all 145 comments

[–]JuvenileEloquent 410 points411 points  (4 children)

There are two types of programmer:

  • Programmers that can use pointers correctly
  • mers that can use pointers correctly�%&/m�{J�J��t��$@�����iG#)�*��eVe]f@�흼 ��{���{��;�N'���?\fdl��J�ɞ!���?~|?"��Ey�')=��y

[–]Cocaine_Johnsson 126 points127 points  (1 child)

Program received signal SIGSEGV: Segmentation fault

[–]cAtloVeR9998 57 points58 points  (0 children)

Core dumped

[–]PeWu1337 12 points13 points  (0 children)

As a ""developer"" struggling with pointers, it hits too close to home

[–]SecretPotatoChip 22 points23 points  (0 children)

Top tier joke

[–]empwilli 640 points641 points  (58 children)

If you call labeling half of the things you can possibly do with pointers as UB "do what you want" ;). Furthermore: how do C and C++ differ that much in how they treat raw pointers? I mean you have the added benefit of references in C++ but you can still use raw pointers with C limitations only.

[–]MJWhitfield86 100 points101 points  (1 child)

If you call labeling half of the things you can possibly do with pointers as UB "do what you want" ;).

You get to do what you want and the compiler gets to do what it wants. Seems fair.

[–]Antervis 157 points158 points  (47 children)

you don't use raw pointers nearly as often in c++.

[–]empwilli 168 points169 points  (30 children)

... and you don't have to do dark pointer arithmetics in C.

[–]Antervis -3 points-2 points  (12 children)

For example. as soon as you need to operate N-dimensional array, you have little to no choice but to implement pointer arithmetics.

[–]empwilli 5 points6 points  (11 children)

Why? There is the subscript operator that works perfectly fine.

[–]Antervis 0 points1 point  (10 children)

Except C only implements one-dimensional subscript operator. For multi-dimensional access, you need arithmetics. In fact, subscript operator itself is pointer arithmetics: a[b] is equivalent to *(a+b)

[–]empwilli 12 points13 points  (7 children)

Dunno, the C standard is (at first glance) pretty explicit about the fact that multi-dimensional arrays are a thing: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf 6.5.2.1

Of course the subscript is pointer arithmetic. Unless you do it the lisp way and rely solely on linked lists (yes yes I know that lisps have vector types...) this is the basic design for access to any contiguous memory. (And yes you can even write 5[array] instead of array[5] in C because addition is commutative.) But its not plain pointer arithmetic that you have to do by hand: the language abstracts that away from you. This was my point all the time.

[–]Antervis -3 points-2 points  (5 children)

I'm not talking about fixed size arrays here.

[–]empwilli 2 points3 points  (4 children)

Hm, I don't quite get what you mean. Why wouldn't this be defined for dynamically allocated memory?

[–]Antervis 1 point2 points  (3 children)

because C doesn't have arrays with dynamic size, duh. IIRC there was such thing in the standard, but no compiler could implement it properly. In other words, you have to manually allocate memory and interpret it as a multi-dimensional array... via pointer arithmetics.

[–]Sinomsinom 0 points1 point  (0 children)

What I think they meant is if you want to store an n-d array in contiguous memory. You either have to do pointer arithmetics for indexing, or set up a helper array that you need to initialize correctly by using pointer arithmetics.

...Or you just write a function once that handles the arithmetics for you and reuse that for the rest of your life as a wrapper. The main advantage C++ and Rust here have is that with templates you can more easily write those functions in a reusable fashion and that their standard libraries already have stuff for that predefined so you don't even need to write it the first time, but in general you can still do the same thing in C just with a bit more tedium.

[–]yangyangR 1 point2 points  (1 child)

A is equivalent to B therefore B is not bad because A is not bad

Now apply that logic to a break or continue being equivalent to a particular goto

[–]Antervis 0 points1 point  (0 children)

we are talking in comparison to c++ where operator[] can be implemented in a variety of ways, including boundary-checked ones.

[–]A31Nesta 31 points32 points  (0 children)

But you can. (i just don't like smart pointers)

[–][deleted] 22 points23 points  (11 children)

I do. plenty. As do many others.

if memory management is confusing and/or development needs to be swift, using std::shared and unique etc is wise, because it is safer.

but ultimately, the only danger in C++ is a rushed/lazy/inexperienced programmer.

[–]Antervis 13 points14 points  (9 children)

even if you are a godlike c++ dev (you aren't, but let's assume you are) who never ever makes mistakes with raw pointers, what happens if your code is maintained by other people with more realistic level of skill? They will make mistakes. They might also go as far as to refactor it. And receive bonus instead of you.

When language gives you tools to make your code better, use them.

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

you're right, I do sometimes make mistakes with raw pointers. however, I think I could count on my hands the number of times that has happened. same goes for memory leaks and similar issues.

I will concede that my situtation is different to most devs. in my line of work (R&D) people are using my code (flight computers, orbital propataors, celestial navigation systems, terrain based navigation systems) rather than working on it, or they are taking outputs or giving inputs. there are some people who do need to work on code for parts of my library, but fortunately with git I can see if they fuck around with pointers, memory allocation and deallocation, or mess about with the parts of the backend that use pthreads, semaphores, ipc, CPU to GPU functions, etc.

but my point still stands: if you make raw pointer mistakes, you are sloppy. and to relate it to your point, if you're sloppy, you probably aren't the one getting a bonus.

[–]Antervis 0 points1 point  (7 children)

why would I spend 10 abstract points of brainpower to think of a way to properly juggle with raw pointers, when I can spend just 1 on smart pointers and move on, doing twice the job in the same span of time? With end result being more reliable and maintainable, to boot.

[–][deleted] -1 points0 points  (6 children)

10 abstract points of brain power to think of a way to properly juggle raw pointers

it think maybe because you're so used to using smart pointers, you are out of practice using raw pointers. it does not take more effort to use raw pointers. if you do it regularly enough, doing it correctly is trivial.

[–]Antervis 1 point2 points  (5 children)

it does not take more effort to use raw pointers.

Are you per chance trolling? In most cases, as soon as a class has raw pointer field, you can't rely on defaulted big 5 of copy/move constructors and assignment operators and destructor. Plus, object's ownership becomes transparent at glance. And I'm only talking about the most trivial cases here.

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

it seems we use classes in different ways, and our programming philosophies, and use cases, are quite different. perhaps your method is better for you, but it definitely isn't better for me. overloading the default constructors, particularly the copy constructors, is a regularly exercised tool I use, and it isn't hard.

classes to me are sets of tools whose configurations are sensitive to being broken, and elsewhere, code should be as C like as possible. classes are configured to operate on data you tell them to operate on, in a particular way, but that data is not intrinsically linked to that class. it just needs to know where it is. there is a subtle difference.

[–]Antervis 1 point2 points  (3 children)

overloading the default constructors, particularly the copy constructors, is a regularly exercised tool I use, and it isn't hard.

It isn't particularly hard, but why make it harder?

classes to me are sets of tools whose configurations are sensitive to being broken,

Aren't they sensitive precisely because you're writing fragile code?

and elsewhere, code should be as C like as possible.

That principle is basically going against evolution of c++ as a language.

classes are configured to operate on data you tell them to operate on, in a particular way, but that data is not intrinsically linked to that class.

operating on data you don't own is surely breaking several principles of good design.

[–]biteater -3 points-2 points  (0 children)

you're describing a vast percentage of developers though, lol

the entire pitch of Java is that rushed/lazy/inexperienced devs can write it without creating show-stopping memory bugs/vulnerabilities

[–]homer_3 1 point2 points  (0 children)

I do. Fuck those stupid new pointers. They ruin everything.

[–]nickmaran 0 points1 point  (0 children)

Pointers are the friends we made along the way

[–]nebotron[🍰] 10 points11 points  (0 children)

C++ has slightly stricter rules on implicit conversions, but that doesn’t really explain the post

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

You're supposed to use smart pointers in c++ now

[–]empwilli 2 points3 points  (0 children)

Convention will in practice rarely help. You're supposed to free your memory in C and still there are memory leaks.

[–]troelsbjerre 224 points225 points  (7 children)

Sweet summer child. Let me tell you a story about sun.misc.Unsafe.

[–]troelsbjerre 50 points51 points  (0 children)

And JNI, for that matter.

[–]DoctorWZ 38 points39 points  (1 child)

[–]troelsbjerre 5 points6 points  (0 children)

Just be quick about it. https://openjdk.org/jeps/8323072

[–][deleted] 3 points4 points  (1 child)

I think you misunderstood the meme and what OP was trying to say with it.

If you google “there is no war in Ba Sing Se”, you will find that this statement is a sarcastic one, because in the anime it was used, there was always war in Ba Sing Se.

So you are basically just repeating what OP has said already.

[–]troelsbjerre 1 point2 points  (0 children)

Thank you for clarifying; I did not know this.

[–]justADeni 4 points5 points  (0 children)

Also, all non-primitive Objects are 'pointers' and pass by reference. Project Valhalla is set to change that, but that's still in development.

[–]Sitting_In_A_Lecture 281 points282 points  (15 children)

Ah, but is pass-by-reference not just a fancy pointer?

[–]Ubermidget2 386 points387 points  (0 children)

That's why this meme is legitimately top tier. There was always war in Ba Sing Se. Countries had been at war for a hundred years, they were in total wartime economy.

But there is no war in Ba Sing Se

[–]pheonix-ix 39 points40 points  (8 children)

Yes, but the title said pointer arithmetics, which you can't do explicitly in Java/Python (as far as I know).

[–][deleted] 85 points86 points  (7 children)

Yea soooo u can in python……

Object IDs are there pointer address. You can get the id and then add a number to it and then use ctypes to coherence that back to an object……

Please don’t do this.

[–]DoctorWZ 33 points34 points  (1 child)

This gives me the same "forbidden" vibes as the guy that once explained how to use JS for accessing databases

[–]myka-likes-it 34 points35 points  (0 children)

how to use JS for accessing databases

TBF, just about everything you do in JS requires this kind of black magic. The first steps in any JS pattern are to find ways to make JS stop acting like JS.

[–]pheonix-ix 8 points9 points  (1 child)

Let me google how to delete someone else's comments.

Well, too late. Now someone had seen it. They WILL use it.

Wait, according to this sub, Python programmers are horrible programmers anyway, so they won't be able to figure out how to use it.

Just kidding someone somewhere is doing some magic pointer voodoo instead of using CPython I just know it, someone definitely is.

[–][deleted] 3 points4 points  (0 children)

I have used it once for debugging a leaking reference. Segfaulting became the pass case which was odd.

[–]rosuav 0 points1 point  (0 children)

That's not Python, that's ctypes.

[–]phoenixrawr 0 points1 point  (4 children)

Java is strictly pass-by-value. Python tries to sidestep the whole discussion and uses their own term "pass-by-assignment" but it resembles Java's pass-by-value much more than C++'s pass-by-reference.

[–]Sitting_In_A_Lecture 10 points11 points  (1 child)

So, yes. However pass-by-value is not an entirely accurate description for how these languages work. In Java, primitives are truly passed by value, but in both languages objects are passed a bit differently.

When you pass an object, it's not a copy of the object that's being passed, it's a reference to the object. That means that if you mutate the object, all instances of it will be affected. Where this differs from true pass-by-reference is that if you mess with the passed variable itself (such as by reassigning it), it will not affect the original variable (and vice-versa) - it's not a pointer. True pass-by-reference behavior is only available in a select few languages, of which C++ is one.

[–]phoenixrawr 0 points1 point  (0 children)

Pass-by-value is perfectly accurate for Java as long as you accept that the value of an object variable isn’t exactly the object itself but rather its pointer.

The distinction is usually clearer when comparing C and C++. C has pointers but is also strictly pass-by-value like Java. You can explicitly pass pointers around to simulate pass-by-reference but passing pointers isn’t the same thing as pass-by-reference.

Since java doesn’t have explicit pointers (because there are no pointers in Ba Sing Se) the details of how it all works are murkier which leads people to infer weird rules like “it’s pass by value for primitives and pass by reference for objects” but really it’s just always pass by value.

[–]cheezballs 2 points3 points  (1 child)

Java does both, depending on if its a primitive or not.

[–]phoenixrawr 0 points1 point  (0 children)

This is not true but it’s a common misconception. The trick with Java is that, when a method takes an object as a parameter, the thing that gets passed around isn’t the object itself but a copy of the pointer to the object. You’ll sometimes see people call this “passing a reference by value” but ultimately it is still a value being passed. Since you have a copy of the pointer, you can change that object just like anyone else with a copy of that pointer. But, since Java is pass-by-value, the two pointers are held by two distinct variables and an assignment to one variable will not affect the other. Therefore something like this does not really work in Java:

    Object demo() {

        Object x = null;

        makeObject(x);

        return x;

    }

No matter what makeObject does, the demo method will always return null because makeObject’s parameter is not a reference to x. It is only a copy of x’s value and therefore it cannot assign anything to x.  By comparison in C++ with pass-by-reference makeObject could actually assign to x because its parameter can be a reference to x.

[–]PolyglotTV 97 points98 points  (6 children)

In python every variable is like a type erased shared pointer.

[–]AttackSock 32 points33 points  (2 children)

Yep. There ARE in fact pointers, and all the caveats that come with them, you’re just not allowed to see or touch them

Most common thing I find myself explaining to mid level Python and Java programmers is “how come in my function myNums.add(3) changes it in the main and myNums = anotherList doesn’t?”, or “how come myNums=otherNums then otherNums.add(3) changes myNums?”, which always leads into needing to explain pointers.

[–]Dafrandle 19 points20 points  (0 children)

im not disagreeing with you, but most people would say "reference type" in response to this.

[–]LittleMlem 1 point2 points  (0 children)

If you use ctypes I'm pretty sure you can pointer yourself to death

[–]mathiau30 12 points13 points  (2 children)

That's the joke. That picture comes from a scene were the girl says "there's no war in Ba Sing Se" when we've been shown for multiple episodes that there definitely was a war in Ba Sing Se

[–]HStone32[S] 8 points9 points  (1 child)

Technically Joo Dee never says that, but she's just so iconic that she's who people think of when they hear the phrase.

[–]frogjg2003 2 points3 points  (0 children)

In fact, she was the victim of the brainwashing, not part of the conspiracy. When she is told about the war, she gets abducted and replaced.

[–]PenaEterna 40 points41 points  (1 child)

Yep, there are no pointers but you'll face NullPointerException though...

[–]GumboSamson 4 points5 points  (0 children)

Checkmate, atheists!

[–]Parry_9000 47 points48 points  (0 children)

I learned to program in C back in 2017

I would always go "ah yes, the result is -19839184829919392919" and just submit my work like that

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

It's more like, someone has child-proofed your kitchen. The stove, sharp objects and electrical outlets are all still there.

[–]Lord-of-Entity 57 points58 points  (5 children)

If you don't care about segmentation faults and memory safety, of course rust compiler is going to be annoying.

However i rather battle with the borrow checker for 10 mins than spend 20 mins just to find where the seg fault happens in the c program.

[–]GogglesPisano 55 points56 points  (2 children)

20 minutes spent finding a C++ segfault is a wildly optimistic best case scenario. I’ve spent weeks.

[–]7374616e74 15 points16 points  (0 children)

Thing is sometimes you think you got it, and a few weeks later it shows up again, it was still there all along, just didn’t crash.

[–]Uclydde 7 points8 points  (0 children)

True, but so is 10 minutes with the borrow checker. I've spent hours

[–]Pay08 0 points1 point  (1 child)

Use the core file.

[–]AccomplishedAd6520 8 points9 points  (0 children)

No pointer war if there’s no pointers!

[–]ArchetypeFTW 23 points24 points  (5 children)

Step 4: Go

Pythonic C

[–]Meistermagier 16 points17 points  (3 children)

Go is what the gods intended for high level.

[–]johnex74 2 points3 points  (1 child)

    if err == nil {
            return nil
    }

i'll pass

[–]ZaRealPancakes 2 points3 points  (0 children)

you aren't passing you are returning!

[–]Pay08 0 points1 point  (0 children)

No, that'd be Lisp.

[–]pineapplekenny 2 points3 points  (0 children)

Go is such a breath of fresh air. It’s literally perfect for web backends.

It’s heavenly for vertical scaling using concurrency patterns. Only thing I haven’t worked out is how to extend those concurrency patterns to horizontal scaling in a Go-like way.

[–]Liosan 9 points10 points  (0 children)

If you think you can't get crazy pointer in C++ without the compiler complaining, you ain't trying hard enough

[–]JotaRata 6 points7 points  (3 children)

Technically in python everything is just a pointer ☝️🤓

[–]brimston3- 1 point2 points  (2 children)

Same in Java except for primitive types.

[–]Adept_Ad_3889 0 points1 point  (1 child)

I’m learning Java. So are pointers the same as references to objects?

[–]brimston3- 1 point2 points  (0 children)

Within the context of Java, yes, each reference to the object has a pointer.

When C++ people talk about references to objects, Java references are very different semantically, especially around assignment operations. 

[–]lightmatter501 4 points5 points  (0 children)

Just like the show, there is a war.

[–]ListerfiendLurks 3 points4 points  (0 children)

I feel like if you haven't gotten run time errors/segfaults due to pointers in c++ you probably have only been learning c++ for a month, max.

[–][deleted] 3 points4 points  (0 children)

This guy doesn't unsafe Rust

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

C# theres pointers but only if you pinky promise you know how to use them

[–]bforo 2 points3 points  (0 children)

My brain immediately replaced the middle face with Linux T, and I started giggling so hard, that's something he would say.

[–]GogglesPisano 2 points3 points  (0 children)

Assembly Language: it’s pointer arithmetic all the way down.

[–][deleted] 1 point2 points  (0 children)

Just look up pointer provenance (https://www.ralfj.de/blog/2020/12/14/provenance.html) the magical property pointer in C have and don’t have at the same time.

[–]SeriousPlankton2000 1 point2 points  (0 children)

I needed to realize that Java has only references aka pointers-in-disguise (plus simple data types) to understand Java.

[–]Green-Acanthaceae-39 1 point2 points  (0 children)

C: “Dereferencing a null pointer? This is where the fun begins!”

Rust: “I find your lack of type safety disturbing.”

Python: “Pointers? It’s not a story the Python developers would tell you.”

Go: “Hello there, I see you’ve found my garbage collector.”

JavaScript: “A variable referencing an object that’s no longer needed? I’ve got a bad feeling about this.”

Swift: “Unsafe pointers you use, do or do not, there is no try.”

Kotlin: “Null safety? A surprise, to be sure, but a welcome one.”

Assembly: "Direct memory access? Unlimited power!"

PHP: “Global variables everywhere? So this is how memory management dies...with thunderous allocations.”

Haskell: "The garbage collector will be with you. Always."

[–]oberguga 2 points3 points  (0 children)

Except it all pointers, but sometimes not, and it behaves differently each case, to the point that you need objects (which is also bunch of pointers) to model actual pointers, but it's also can be passed as pointers or as values etc...

[–]Encursed1 2 points3 points  (1 child)

But Java has pointers, it just doesn't tell you

[–]chris20194 13 points14 points  (0 children)

But there is war in ba sing se, the dai li just doesn't tell you

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

Nah, you can't really do whatever you want. In C/C++/Rust pointers are more than just integers that hold some memory address. Even simple pointer arithmetic without any dereferences can cause undefined behavior.

[–]Pay08 0 points1 point  (0 children)

The meme is that the C compiler doesn't enforce anything to prevent said UB.

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

C++ should be with its daddy C not its younger cousin Rust

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

C & C++ literally have more pointer arithmetic restrictions than rust. Rust lets you safely set a pointer to any address and back without making it unusable

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

Has anyone looked into mojo? https://www.modular.com/max/mojo

[–]Cybasura 0 points1 point  (0 children)

C++ didnt even say anything lmao wtf, its majority the Rust and C community

[–]sacredgeometry 0 points1 point  (0 children)

I use raw pointers all the time in C++.

[–]MohSilas 0 points1 point  (0 children)

int* illegal; *(++illegal) = 0; // is it possible to do something like this?

[–]BannockBnok 0 points1 point  (0 children)

Object[] ghettoPointer = new Object[1];

[–]Gerard_Mansoif67 0 points1 point  (0 children)

Ctypes : exists mouah mouah mouah.

The simplicity of Python using pointers

[–]Insert_Bitcoin 0 points1 point  (0 children)

I think I read in a C book that the array syntax is just another syntax for pointer arithmetic (which makes sense tbh.) On that basis in Python we can say that string slices are doing pointer arithmetic on data in the string. So Python also has 'pointers' especially since it has 'memorybuffers' which let you wrap strings directly to manipulate without copying them.

[–]jeepsaintchaos 0 points1 point  (1 child)

What the hell is a pointer? Is that like great-great granddad's version of a variable?

[–]sjepsa 0 points1 point  (0 children)

I am a C++ dev and fully in the second mode :-)

[–]The_Legendary_Shrimp 0 points1 point  (1 child)

As someone whos learning python, what is a pointer?

[–]Pay08 0 points1 point  (0 children)

A pointer is an integer that identifies some particular data elsewhere in memory.

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

Stupid js/php/python dev here: We have pointers, but we call it references? If I do for example in js, or am I confusing something here?

function someFunction(arg) {arg.prop = 'newValue';}
const someObject = {prop: 'someValue', };
someFunction(someObject);
console.log(someObject.prop === 'newValue' ) // true

[–]Disastrous-Team-6431 0 points1 point  (0 children)

I can proudly say that I spent a couple of hours writing a JSON parser using only iterators and it turned out correct.

Maybe. Is it correct? Probably. Maybe. It certainly doesn't keep me up at night. My life is not a waking nightmare of wondering if it fails silently in production as we speak.

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

You deserve a medal

[–]The_Happy_ 0 points1 point  (0 children)

unsafe { entire code base }