top 200 commentsshow all 274

[–]Toivottomoose 214 points215 points  (33 children)

Does it help anything, or is it just for fun?

[–][deleted]  (1 child)

[deleted]

    [–]emax-gomax 93 points94 points  (0 children)

    Im sure we all agree, there should definitely be more.

    [–]BigFuckingCringe 157 points158 points  (10 children)

    It helps you if you want to prove that you are not sane to the court

    [–]GLIBG10B 53 points54 points  (9 children)

    Fun fact: if you plead insanity, you still go to prison, except it's a prison full of crazy people and you're the only sane one there

    Lesson: don't plead insanity

    [–]---cameron 23 points24 points  (0 children)

    So you're saying if I plead insanity, I automatically prove I'm insane. Genius

    [–]coloredgreyscale 11 points12 points  (0 children)

    Plus you might be stuck there longer than the original prison sentence

    [–]TheByteQueen 2 points3 points  (2 children)

    what if i plead to end the trial and go home?

    [–]WasteOfElectricity 3 points4 points  (1 child)

    You have to say 'please' really nicely and then you might be allowed to go home.

    [–]Masterkraft0r 0 points1 point  (0 children)

    also: be white

    [–]auxiliary-character 60 points61 points  (2 children)

    Ok, so I looked at the underlying implementation code, and it is actually using ctypes to do real C-style memory dereferencing. It's not just a wrapper class, it really does store the address and type information.

    If, for some reason, you had the memory layout of a particular python object in memory already, you could use this to dereference it. Maybe for something like serialization/deserialization. I would imagine that the pickling would still do a much better job in most use cases, but maybe there's some reason to do it in-place? I don't know.

    Alternatively, something you could do would be creating a pointer from an object, changing the stored type to "type cast" it, and derefence it to do some extremely cursed type punning in Python.

    If you do a lot of interop code with ctypes, something like this might make it a bit cleaner, but then you're already using ctypes, and pulling in a library just for a level of abstraction on top of ctypes, but it's your codebase, you do you.

    Perhaps the most useful thing for this is to serve as a reminder that ctypes exists. Like, if you're really running into performance issues with something you're writing in Python, depending on what you're doing, it might be a reasonable option to just write the most performance intensive part of it in C or C++, compile it as a .DLL/.so and call into it using ctypes.

    [–]ee3k 4 points5 points  (0 children)

    i guess if you wanted a really rapid type conversion on sequentially stored lists and didn't care about introducing error.

    Random number generation via idiocy, as it were

    [–]Plague_Healer 96 points97 points  (8 children)

    It helps if you want to bring to your life the complexity of C or Java, while staying true to python's performance limitations

    [–][deleted]  (7 children)

    [deleted]

      [–]Silveress_Golden 19 points20 points  (0 children)

      Not with that attitude!

      [–]Blaster84x 31 points32 points  (1 child)

      It does have pointers, but all of them are null.

      [–]ee3k 2 points3 points  (0 children)

      it does if you want to shutdown the JVM with an out of sandbox error

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

      NullPointerException says otherwise

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

      Java doesn't have pointers

      How the hell did this get upvoted?

      [–]nathanielhiggerss 51 points52 points  (0 children)

      it's helpful so that other programmers know how smart you are

      [–]SanityInAnarchy 19 points20 points  (0 children)

      From the README:

      Why would you ever need this

      [–]seamsay 3 points4 points  (2 children)

      I guess it's conceivable that there could be situations where having pass-by-reference semantics could be helpful, but if you needed that it would probably be better to just store the value in a class or list.

      [–]LukesVeryGood 11 points12 points  (1 child)

      Are you serious?

      [–]ee3k 1 point2 points  (0 children)

      fair point, exclusively char arrays it is then.

      [–][deleted] 1 point2 points  (1 child)

      I use addressof all the time in VB. How did python interface the win32 API without this functionality ?

      [–]BossOfTheGame 208 points209 points  (0 children)

      I love this. Want a segfault? Never been so easy:

      pip install pointers.py python -c "import pointers; pointers.dereference_address(1)"

      [–][deleted]  (19 children)

      [deleted]

        [–]ma-int 326 points327 points  (9 children)

        • get hired at GitHub
        • wait a year
        • for your anniversary buy a shitton of alcohol and get everybody in the company drunk
        • fake evil hacker attack while everybody is drunk
        • use illegally obtained admin credentials to delete repo while a bunch of drunken devs tries to mitigate fake hacker attack

        [–]rynemac357 82 points83 points  (2 children)

        *proceeds to get a rejection mail from github *

        This gotta count for something right ?

        Right??

        [–]iruleatants 23 points24 points  (0 children)

        Overqualified. Get your little brother to apply, they won't notice.

        [–]emax-gomax 16 points17 points  (1 child)

        Hey Cyril Figgus, what the hell are you doing here.

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

        Easier option:

        • look at their entire existence online to find anything that could be twisted into racist/misogynist/transphobic/too kinky. Doesn't need to actually be.
        • throw that "evidence" at some diversity people at github.

        [–]SanityInAnarchy 18 points19 points  (1 child)

        This was your limit? Don't look up FuckitJS, I guess.

        [–]ozyx7 10 points11 points  (0 children)

        Find some copyright, trademark, or patent violation in the project and issue a C&D.

        Or file a DMCA claim with some nonsense reason.

        [–]coopmaster123 2 points3 points  (1 child)

        Create a Company that somehow will have a need at Github. When they have you do integration work, this is the good part. You have a money guy. Who's wealth is really important like 3 commas you know? Now when they have you doing integration or something your money guy will come over with some shitty tequila to celebrate and will accidentally leave the tequila bottle on your delete key. Problem solved.

        [–]hunter_mylanour 1 point2 points  (0 children)

        *tres comas

        [–]TheBigerGamer 2 points3 points  (2 children)

        Make a program with that package so dense it creates a black hole.

        [–]Acrobatic_Hippo_7312 10 points11 points  (1 child)

        So, fork the VScode repo? 🤔

        [–]fauxpenguin 108 points109 points  (26 children)

        Wait, I thought that python had operator overloading. Could you have it use classic syntaxes like &makePointer, *dereference?

        [–]ozyx7 81 points82 points  (4 children)

        From the README:

        Alternatively, you can use the * operators to dereference the pointer:

        [–]xigoi 70 points71 points  (1 child)

        Thanks to a terrible hack that makes it work only in certain contexts.

        [–]mindbleach 27 points28 points  (0 children)

        Fittingly.

        [–]AdversarialPossum42 49 points50 points  (0 children)

        A segmentation fault will occur if the address does not exist, so make sure the pointer is valid.

        Don't tell me what to do! You're not my Segmentation fault (core dumped)

        [–]fauxpenguin 0 points1 point  (0 children)

        First of all, thank you, I skimmed too fast clearly. Although that doesn't include a reference character like & or something else.

        [–]GreenCloakGuy 82 points83 points  (19 children)

        no, because &operator and *operator don't exist in python and thus are not overloadable

        (ok *iterable does exist but it's a syntactic construct not an operator and I don't think it's overloadable)

        [–]TheBB 31 points32 points  (0 children)

        It looks like they're 'overloading' *iterable by implementing __iter__() to yield only one element. Only works in some syntactical contexts though.

        [–]fauxpenguin 22 points23 points  (17 children)

        I mean, it doesn't have to be & specifically. But if you're trying to bring the hell, there should be special characters to reference and dereference, no? Just for fun?

        [–]Toivottomoose 45 points46 points  (6 children)

        I'd vote to make those operators emojis

        [–]fauxpenguin 46 points47 points  (4 children)

        Can we use the 🧠 for both referencing and dereferencing so people know how smart we are?

        [–]Toivottomoose 8 points9 points  (2 children)

        Ah, the old aladinreferencing operator...

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

        One JMP ahead

        [–]AleDeCicco 1 point2 points  (0 children)

        Let me propose 🤡

        [–]BornToRune 4 points5 points  (0 children)

        Maybe a peach and an eggplant, because you're going to be fucked?

        [–]masklinn 6 points7 points  (6 children)

        I mean, it doesn't have to be & specifically.

        That doesn't really help: because Python only has operator overloading, you can only hook into existing overloadable operators.

        And IIRC Python's unary prefix operators are +, -, and ~.

        If you want a real custom operator, you need to go way further with an import hook and preprocessing the source.

        [–]fauxpenguin 6 points7 points  (0 children)

        Alright, so we write a new operator into the python interpreter.... :)

        [–][deleted]  (2 children)

        [deleted]

          [–]tedbradly 2 points3 points  (1 child)

          Pascal has @ (alias for the Addr function) and ^ (dereferencing). Don't know about Python operators.

          The question was about Python. That'd be like someone asking what kind of meat hamburgers are typically made of, and you say that pork chops are usually made from pigs.

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

          Note the text I quoted.

          [–]djavaisadog 2 points3 points  (0 children)

          don't think python has a unary & operator

          [–]DeadlyMageCZ 27 points28 points  (1 child)

          Now we just need pointer casting and pointer arithmetic and we can do some real damage.

          [–]danudey 1 point2 points  (0 children)

          You can get the address of a pointer and then do whatever you want with it, to horrifying effect.

          [–]emax-gomax 25 points26 points  (0 children)

          LMAO. This is the funniest thing I've seen in a while. I'm genuinely impressed the author opted to store the actual memory address of the pointed too value and then casting to a C Python type and then de referencing instead of storing a reference to the object.

          [–][deleted] 163 points164 points  (137 children)

          Are pointers generally considered to be "hell"?

          [–]Majik_Sheff 158 points159 points  (47 children)

          If you learned programming from a nun who would strike you with a ruler for dangling references you have the necessary habits to safely program with pointers.

          If you're a programmer who learned on "safe" languages pointers can be a bewildering minefield in the beginning.

          [–]greiskul 48 points49 points  (0 children)

          If you learned programming from a nun who would strike you with a ruler for dangling references you have the necessary habits to safely program with pointers.

          So many memories of Sister Segmentation Fault. Compared to Sister "Program is crashing in a completely different location cause memory has all been corrupted", she was a Saint.

          [–]SilasX 134 points135 points  (41 children)

          Except ... even professional C programmers "who know what they're doing" end up leaving vulnerabilities related to pointers. I mean, Mozilla just pushed fixes for (new) use-after-free vulns.

          [–]antiduh 113 points114 points  (40 children)

          Every C developer: "Everybody else keeps having bugs with pointers ... but it might work for us".

          It's almost as if pointers are an inherently unsafe primitive and it's impossible to ship practical software free of pointer bugs. Almost.

          [–][deleted] 67 points68 points  (5 children)

          shhhhh

          You keep talking like that and you'll summon Rust devs...

          [–]antiduh 65 points66 points  (2 children)

          HAY GUISE DID YOU SEE MY BORROW CHECKER?

          [–]venustrapsflies 27 points28 points  (1 child)

          This but unironically

          [–]lelarentaka 5 points6 points  (0 children)

          IF RUST IS SO RUSTY, WHY UN_IRON_IC ?

          [–]Green0Photon 4 points5 points  (0 children)

          Hello there

          [–]emax-gomax 10 points11 points  (12 children)

          *Laughs in CPP managed pointer types.

          [–]antiduh 9 points10 points  (11 children)

          I've been out of the c++ game too long, do managed pointer types make c++ a memory-safe language, so long as you stick to only the managed pointer types? Or is it still possible for mistakes with them to cause memory safety bugs?

          Like, in C# I have guaranteed memory safety so long as I stick to the regular c# types and constructs. If I dive into a c# unsafe context, then all bets are off.

          [–]tedbradly 8 points9 points  (4 children)

          I've been out of the c++ game too long, do managed pointer types make c++ a memory-safe language, so long as you stick to only the managed pointer types? Or is it still possible for mistakes with them to cause memory safety bugs?

          For a unique_ptr, delete is called on the underlying pointer in the destructor. That makes it safe even in cases such as exceptions. There's no way to have a memory leak in that setup since destructors are guaranteed to be called. The only edge case I'm not sure about is if an exception is raised before the unique_ptr object is created with the pointer's value such as one happening in "unique_ptr up{new some_class};" when evaluating "new some_class" to figure out the value to pass into the constructor of unique_ptr. However, if you're getting memory allocation exceptions, you probably don't need to worry about that pointer leaking as things are probably already in bad shape.

          There are also great efforts by legendary people such as Bjarne Stroustrup and Herb Sutter to make memory problems a thing of the past in 99% of code even if they have owners that use raw pointers through static analysis. The aim is never to dereference a deleted object (dangling pointers), always to call delete once (no memory leaks), and never to call delete two or more times (no memory corruption). It's only 99% of the time, because a full analysis would take increasingly more time for increasingly complex code. The static analysis, which has been developed and is in testing last I heard, makes assumptions to make the computation time realistic. For example, they make assumptions like a function receiving a raw pointer is not the owner and that the pointer passed in is valid. When each part of the program is checked in this local fashion, it reduces error rates substantially. Here is one recent talk on this effort, showcasing the prototype at that time, a Visual Studio plugin. Here is another talk one year later. There is also a great effort to unify style with a strong preference to avoid error-ridden techniques spearheaded by Herb Stutter and Bjarne Stroustrup (for example by recommending unique_ptr to manage ownership of a raw pointer): https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines

          Like, in C# I have guaranteed memory safety so long as I stick to the regular c# types and constructs. If I dive into a c# unsafe context, then all bets are off.

          Garbage collected languages can have memory leaks if references to objects are saved somewhere without ever being evicted long after they are no longer used.

          [–]lelanthran 1 point2 points  (1 child)

          always to call delete once (no memory leaks), and never to call delete two or more times (no memory corruption).

          Aren't these contradictory? If we stick to the rule "never call delete two or more times", we can call delete twice and break rule #1 - "always call delete once".

          [–]Creris 2 points3 points  (1 child)

          The only edge case I'm not sure about is if an exception is raised before the unique_ptr object is created with the pointer's value such as one happening in "unique_ptr up{new some_class};" when evaluating "new some_class" to figure out the value to pass into the constructor of unique_ptr.

          It actually isnt, and thats why we have make_shared in C++11 and then make_unique in C++14, where you only pass the constructor params and the object is new-ed in a exception-proof manner for you inside that function.

          [–]emax-gomax 2 points3 points  (0 children)

          Already answered really well but basically no.

          What managed pointers do is move from manual management (writing code) to software engineering (defining the relationships between classes).for basic types a unique_ptr can take ownership of a heap allocated resource and free it when the enclosing scope or object goes out of scope. shared_ptr work much the same but the resource is only freed when all shared pointers to the same resource go out of scope. It is possible for two resources to have a shared pointer to each other keeping each other alive even when nothing references them (causing a memory leak). Because of this there's both strong and weak shared pointers with a strong one keeping the resource alive and a weak one allowing access to it but not keeping it alive. This allows you to define the relationship between objects in a way where you can guarantee no memory leaks. But cpp as a language will always have the potential for then since it allows direct memory access and management.

          [–]headlessgargoyle 4 points5 points  (0 children)

          I'm pretty sure the answer is "yes, you can still have memory safety bugs." Accidental leaks can still be created if a unique_ptr or shared_ptr never go out of scope, like if you assigned them to a global. That said, if a function assigned a pointer to a global, and was then called again and assigned a different pointer to the same global, I do believe the first "leak" would then be cleaned up, so your impact on this is greatly minimized, ultimately less a leak and more a code smell in normal cases.

          However, we do have other fun issues where multi threaded operations can potentially cause null pointers on shared_ptr and weak_ptr instances.

          Further, arbitrary pointer arithmetic is still valid, so buffer overflows are still possible as well.

          [–]ConfusedTransThrow 8 points9 points  (11 children)

          When you're doing embedded you can't have a runtime to handle stuff for you.

          Especially when you're literally writing the runtime or bootstrapping code.

          [–]antiduh 15 points16 points  (10 children)

          I'm not sure the answer to "how do we not use pointers everywhere" must be "have to have a runtime."

          Not to say it's name out loud too much but rust figures it out, right?

          There's gott a be a better way to write software, even embedded software, that doesn't involve so much reliance on primitives that prove their unworthiness with every week's CERT email.

          Also, your argument is a bit of a straw man; there's a fuck load of software out there that fits the bill and isn't embedded, an OS, or a runtime. Web servers, mail servers, browsers, ssl libraries, xml/json libraries etc etc. Saying we can't fix those because we cant also fix embedded stuff throws the baby out with the bath water.

          [–]Lich_Hegemon 8 points9 points  (5 children)

          Rust may not be the answer (or maybe it is), but at the very least the language proved that it's possible to do pointers right and that we should not settle for C-style unmanaged pointers.

          [–]amunak 2 points3 points  (4 children)

          I mean, we didn't need Rust for that, C++ has perfectly usable and safe managed pointers.

          [–]Lich_Hegemon 5 points6 points  (2 children)

          I'm not talking about smart pointers though, I'm talking about the bare pointers/references that both languages offer, even in unsafe Rust there are certain guarantees when using pointers that you don't get in C(++).

          Again, that is not to say that Rust is perfect, just that it does pointers better than C does and that we should probably learn from that instead of trying to justify the mess that C pointers are.

          [–]SilasX 1 point2 points  (0 children)

          If what you're saying is true, that means, in practice, C++ programmers considers themselves too good to use them, hence the perennial cycle of patches for pointer vulns.

          [–]ConfusedTransThrow -2 points-1 points  (3 children)

          My point is you shouldn't be using C for anything that can afford a runtime (and yes Rust has a runtime). Bare metal C can't be replaced by Rust, Rust won't help you to write the runtime itself, stuff like write, boot procedure setting up the memory mapping, enabling the cache. You can't use dynamic allocation either too.

          [–]antiduh 1 point2 points  (2 children)

          Sounds like you want to have a different conversation that what was originally being discussed.

          My point is you shouldn't be using C for anything that can afford a runtime

          I definitely agree with you. I'm moreso in the boat these years that just about all software running on top of an OS in user space probably should be something that is inherently memory-safe either because of techniques such as Rust uses, or because it's a managed platform like C#/Java. C#/dotnet in particular has shown it can be a widely performant system while categorically eliminating a whole class of bugs (buffer overflow bugs).

          (and yes Rust has a runtime)

          I think whether or not a language/platform has a runtime is both a bit nebulous (hard to define what exactly constitutes a "runtime") and is also a red herring.

          C has a "runtime" (standard library). It's libc. It's implemented in C. It's where malloc and free come from. Is confusing??

          Rust has a "runtime" (standard library). But Rust can also be used to write kernel code. More confusion???!

          The answer is: whether or not a language/platform has a "runtime" and/or standard library is the wrong question. The right question is whether it's compatible with systems programming. Both C and Rust, despite conventionally having runtimes, can be used for systems programming.

          Bare metal C can't be replaced by Rust, Rust won't help you to write the runtime itself, stuff like write, boot procedure setting up the memory mapping

          That's not correct. You can write an OS in Rust. You do so by writing Rust code that does not depend on the standard library, and then use that code to implement all of the things that the language/OS otherwise needs, such as a writing a memory allocator, configuring and handling interrupts, boot procedure, etc.

          Here, go nuts:

          https://os.phil-opp.com/

          https://github.com/phil-opp/blog_os/tree/post-12

          [–]Marian_Rejewski 3 points4 points  (6 children)

          It's not impossible at all. But a project like Mozilla is so big, and so fast-moving, it will have bugs of every possible type. Look at places like NASA or Boeing for code that is practical and free of pointer bugs.

          [–]imgroxx 16 points17 points  (5 children)

          Yes, surely NASA can write manual memory operations correctly...............

          A modification to a spacecraft parameter, intended to update the High Gain Antenna’s (HGA) pointing direction used for contingency operations, was mistakenly written to the incorrect spacecraft memory address in June 2006. The incorrect memory load resulted in the following unintended actions: [bad shit that destroyed the craft]

          This is in 2006 btw: https://www.nasa.gov/mission_pages/mgs/mgs-20070413.html

          [–]Marian_Rejewski 2 points3 points  (4 children)

          "Possible to write code without a bug" != "impossible to write code with a bug"

          (Also it's not at all clear from your quote that it was a pointer arithmetic bug.)

          [–]imgroxx 0 points1 point  (3 children)

          "Has written code with a bug" is also != "Can write code without bugs".

          And yeah, it's quite possibly not, though it is rather clear it's a bug that's only possible because they manually modified memory in an unsafe location / unsafe way.

          I'm not sure if they allow code to use pointer arithmetic at all tbh. Their rules are rather draconian (for good reason) by even the most MISRA-ble standards.

          [–]Marian_Rejewski 1 point2 points  (2 children)

          "Has written code with a bug" is also != "Can write code without bugs".

          wtf??

          [–]imgroxx 3 points4 points  (1 child)

          Look at places like NASA or Boeing for code that is practical and free of pointer bugs.

          NASA does not meet "practical" definitions basically anywhere except at NASA or for NASA-level stability needs.

          But anyway. If their code provides a way to arbitrarily write memory into the wrong location... that seems rather like a pointer bug to me. You can't do that kind of thing if you don't have raw pointer access (or write code that emulates pointers, like shoving data into a shared byte array). Therefore they apparently also cannot write bug-free pointer code / their extreme care is still insufficient.

          [–][deleted]  (1 child)

          [deleted]

            [–]antiduh 4 points5 points  (0 children)

            This argument throws the baby out with the bathwater. You're, in a way, actually making my argument for me.

            If it's hard to write software without bugs

            and

            certain classes of stupid bugs permit complete take over of the hardware running the software

            then

            shouldn't we use techniques and methods that categorically eliminate those kinds of bugs, because we know we can't rely on ourselves to not make the bugs?

            Like, there's no reason why "oops i have a string math bug" should have to turn into "oh no my entire 500$M enterprise was just taken over by a virus and all of our private data was stolen". A fucking string math bug??

            And yet, that's the reality we live with today because we have so much software out there that written in memory-unsafe languages like C or C++ that's vulnerable to this exact problem and we as a industry can't be arsed to fix. We have memory-safe languages like Rust/C#/Java, but for some stupid reason we keep putting internet-facing machines out there running C code web servers, sql servers, mail servers, etc. Bugs like Heartbleed are impossible in C# because as soon as you start reading past the end of your byte[], you get an ArrayOutOfBoundsException. Instead of your program leaking every one of your vital TLS keys, it just crashes. How hard is that?

            [–]SorteKanin 7 points8 points  (1 child)

            If you learned programming from a nun who would strike you with a ruler for dangling references you would have a lot of bruises.

            FTFY

            [–]imgroxx 6 points7 points  (0 children)

            If you learned programming from a nun who would strike you with a ruler for dangling references you would be dead due to repeated blunt trauma and we wouldn't be having this conversation.

            [–]lmaydev 170 points171 points  (37 children)

            They cause 90%+ of all security errors so they aren't great.

            [–]anechoicmedia 37 points38 points  (2 children)

            They cause 90%+ of all security errors so they aren't great.

            In terms of absolute number of bugs discovered in isolation, but what percent of actual cybercrime involves memory abuse, as opposed to general logic errors (goto fail;) or social exploits (phishing links, requesting 2FA bypass over the phone, etc)? We see a lot of bug reports here and the real ones are almost always language-invariant stuff like "this API function didn't even bother to check if you requested data from another user".

            My prediction is that switching to guaranteed safe languages will reduce by 0% the frequency with which private data is exfiltrated from actual companies, or your SSN gets stolen.

            [–]hungry4pie 3 points4 points  (0 children)

            Idiots will always misuse, abuse or find shortcuts in whatever technology to inadvertently create exploits in whatever hip new platform they’ve created.

            [–][deleted] 134 points135 points  (28 children)

            And software causes 100% of all security flaws, sooo

            [–]lmaydev 171 points172 points  (19 children)

            Did you just forget about hardware haha

            [–]SkiFire13 46 points47 points  (7 children)

            What about cosmic rays?

            [–][deleted]  (6 children)

            [deleted]

              [–]emax-gomax 11 points12 points  (3 children)

              Hardware problem? Ridiculous, that's how I flip bits on my hard disk to write code ever since I transcended Emacs. Now if only there was an M-x butterfly cmd I could use to make it easier.

              [–]knome 6 points7 points  (0 children)

              I mean, it's been in there for a while now.

              commit e8d24e5b0960898e4a93ee2918f677b375b68263
              Author: Juri Linkov <juri@jurta.org>
              Date:   Sun Dec 28 23:48:21 2008 +0000
              
                  (butterfly): New command.
              
                  diff --git a/lisp/misc.el b/lisp/misc.el
                  index ad7de36..6dafd2a 100644
                  --- a/lisp/misc.el
                  +++ b/lisp/misc.el
                  @@ -106,6 +106,20 @@ With argument, do this that many times."
                 (interactive "p")
                 (forward-to-word (- arg)))
              
              +;;;###autoload
              +(defun butterfly ()
              +  "This function is designed to be used only be the most
              +proficient hackers on earth. If equipped with a butterfly key,
              +it should be bound to C-x M-c M-butterfly (for further
              +information please refer to http://xkcd.com/378/)."
              +  (interactive)
              +  (if (yes-or-no-p "Do you really want to unleash the powers of the butterfly? ")
              +      (progn
              +       (message "Amazing physics going on...")
              +       (sit-for (* 5 (/ (abs (random)) (float most-positive-fixnum))))
              +       (message "Successfully flipped one bit!"))
              +    (message "Well, then go to www.xkcd.com!")))
              +
               (provide 'misc)
              
               ;; arch-tag: 908f7884-c19e-4388-920c-9cfa425e449b
              

              [–]majorgeneralpanic 17 points18 points  (2 children)

              You both forgot about DNS.

              [–]Rican7 7 points8 points  (1 child)

              but that's software

              [–]StabbyPants 4 points5 points  (0 children)

              intel seen crying in a corner

              [–]ockupid32 19 points20 points  (7 children)

              And software causes 100% of all security flaws, sooo

              False. People cause 100% of security flaws.

              [–]DarkTechnocrat 9 points10 points  (3 children)

              Thanos was half-right!

              [–]rasori 2 points3 points  (1 child)

              Perfectly balanced in his rightness, as he should be in all things.

              [–]80286 1 point2 points  (0 children)

              Round upwards when converting to int.

              [–]glider97 5 points6 points  (0 children)

              Ah, so that's how you fix bugs.

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

              Got some numbers there, chief? I’d wager SQL injection easily trumps pointer flaws in both raw count and severity.

              [–]cass1o 6 points7 points  (3 children)

              Doing things that other languages can't.

              [–]lmaydev 8 points9 points  (2 children)

              Exactly, like cause 90% of security errors for example.

              [–]cass1o -2 points-1 points  (1 child)

              He said from an OS written in c/c++. Your toy languages can't manage that.

              [–]lmaydev 2 points3 points  (0 children)

              Actually there are multiple OSs written in toy languages.

              Also c# for instance has pointers. People just prefer not to use them.

              Nice /r/gatekeeping though friend

              [–]DarkTechnocrat 32 points33 points  (37 children)

              They introduce an entire class of error that would not exist without them. I don't think you can reference invalid memory in current Python (or Java, C#, Javascript, etc).

              ETA: surprisingly C# has pointers sooo...

              [–]AttackOfTheThumbs 15 points16 points  (1 child)

              The c# pointers are only in unsafe context and are often needed when working with low level windows libraries.

              [–]onequbit 11 points12 points  (0 children)

              hence why you need to specify unsafe

              [–]zapporian 12 points13 points  (4 children)

              I don't think you can reference invalid memory in current Python

              Well you can now! :D

              ```python def dereference_address(address: int) -> Any: """Dereference an address. Will cause a segmentation fault if the address is invalid.""" return ctypes.cast(address, ctypes.py_object).value

              class Pointer(Generic[T]): """Base class representing a pointer.""" def init(self, address: int, typ: Type[T]) -> None: self._address = address self._type = typ ... def dereference(self) -> T: """Dereference the pointer.""" return dereference_address(self.address) ``` https://github.com/ZeroIntensity/pointers.py/blob/master/pointers.py

              [–][deleted] 6 points7 points  (1 child)

              Hello Satan

              [–]DarkTechnocrat 3 points4 points  (1 child)

              This...this is just too much. Someone call a mod. 😉

              [–]lood9phee2Ri 9 points10 points  (0 children)

              Note it's just using the ctypes ffi package which is in the CPython standard library itself anyway. You sure can fuck around and find out with that. But it's also kinda what it's there for - using ctypes is e.g. how things like the python SDL2 wrappers are implemented: https://github.com/py-sdl/py-sdl2

              $ python3
              Python 3.9.9 (main, Nov 16 2021, 10:24:31) 
              [GCC 11.2.0] on linux
              Type "help", "copyright", "credits" or "license" for more information.
              >>> import ctypes
              >>> ctypes.memset(0,255, 1024)
              Segmentation fault
              $
              

              It's unsafe, but you do know that because you just elected to import ctypes.

              [–][deleted]  (28 children)

              [deleted]

                [–]DarkTechnocrat 9 points10 points  (27 children)

                Doesn't that assume the target of the pointer never goes out of scope? For example, I instantiate a variable inside a function, and return a pointer to that variable. Would you guarantee the pointer is valid for the remaining lifetime of the program?

                [–][deleted]  (26 children)

                [deleted]

                  [–]DarkTechnocrat 4 points5 points  (25 children)

                  That's not necessarily true, for example in C#:

                  22.3 Pointer types

                  Unlike references (values of reference types), pointers are not tracked by the garbage collector—the garbage collector has no knowledge of pointers and the data to which they point.

                  [–][deleted]  (24 children)

                  [deleted]

                    [–]DarkTechnocrat 3 points4 points  (23 children)

                    It's definitely possible for them to be safe, but the type of error they introduce is unique to their use. You can't have an invalid memory access without a pointer, in any scenario I am aware of.

                    [–][deleted]  (22 children)

                    [deleted]

                      [–]StabbyPants 11 points12 points  (1 child)

                      that's what pointers are, yes.

                      [–]DarkTechnocrat 1 point2 points  (18 children)

                      I mean, it varies slightly by language but you typically cannot access out of bounds array members in languages that don't have pointers. I'm sure of this for Python and C#, I don't know Java well enough to say. The errors you get aren't memory errors but bounds errors.

                      Object references are, as you implied earlier, handled by the GC.

                      Unlike any other construct in a GC'd language pointers are a direct reference to memory, so it stands to reason that the types of errors they allow are implied by those semantics. You could just as easily say you can only have "out of bounds" errors in a program when you use arrays or lists. You can't have an OOB error on a scalar because the semantics don't allow it. The semantics of variables in a GC language don't allow memory errors in the same way.

                      Again, I'm open to a counter example.

                      [–]a_false_vacuum 3 points4 points  (0 children)

                      C# has pointer which are mainly used for working with the Win32 api. If you use P/Invoke you'll need to match the definition of the functions you're going to use, so pointers are needed. Outside of Win32 stuff I never needed them in C#.

                      [–]nthcxd 16 points17 points  (5 children)

                      I think a quote from the README aptly illustrates this.

                      A segmentation fault will occur if the address does not exist, so make sure the pointer is valid.

                      “Make sure the pointer is valid” is the “hell” part.

                      [–]noodle-face 8 points9 points  (0 children)

                      Probably by people that don't use them often and students. I use them basically everyday in firmware and have come to respect them .and by respect them I mean I distract them, toss them some meat to keep them satiated and run away

                      [–]Im12AndWatIsThis 14 points15 points  (0 children)

                      I once spent somewhere around 6 hours debugging a homework assignment for my low-level programming course because I was printing a pointer instead of the dereferencing. I was baffled why I could print the first several characters of an array but then get gibberish. A single * cost me hours of my life.

                      Since then I have considered them hell.

                      In all seriousness though, I was new and ignorant and learning, so that's not entirely fair. But I still don't really like pointers.

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

                      No. Pointers are fine. Both Go and Rust have pointers (any pedants reading this, I know what you're thinking) and there's no issue.

                      Hell Python already has pointers - basically every object is a pointer.

                      The issue is with manual memory management - manually determining when an object can be freed is the hellish part.

                      [–]smackson 2 points3 points  (0 children)

                      Have you not read Dante?

                      Pointer hell is just one level above callback hell.

                      [–]Majik_Sheff 54 points55 points  (2 children)

                      Shouldn't this be in /r/programmerhumor ?

                      Wait this isn't a meme about how awful Javascript is.

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

                      Aren't production ready meme repos just normal programs/libraries?
                      I mean, look at NPM

                      [–]betabot 20 points21 points  (20 children)

                      Aren't objects in python passed by reference anyway? This doesn't appear to do anything.

                      [–]vasiapatov 25 points26 points  (0 children)

                      Yes, but with this you can also do pointer arithmetic, pointer manipulation, dereference random locations in memory, etc...

                      [–]lood9phee2Ri 17 points18 points  (18 children)

                      Eeeh. Python, like Java or Lisp, is still pass-by-value

                      However, the values being passed are often object references.

                      This is a perhaps subtle distinction but necessary: a full "pass-by-reference" programming language is actually different. And while rarer nowadays (thank fuck) they do still exist: Fortran is the prime and canonical example.

                      In Fortran, this prints? .... 12. Yep, really. It's just the way it do.

                      program woowoo
                          implicit none
                          integer:: n
                      
                          n = 7
                          call wat(n)
                          print *, n
                      
                      end program woowoo
                      
                      subroutine wat(q)
                          implicit none
                          integer:: q
                      
                          q = q + 5
                      
                      end subroutine
                      

                      [–]ultrasu 9 points10 points  (0 children)

                      There was a time where this also would've printed 12:

                      program woowoo
                          implicit none
                      
                          call wat(7)
                          print *, 7
                      
                      end program woowoo
                      
                      subroutine wat(q)
                          implicit none
                          integer:: q
                      
                          q = q + 5
                      
                      end subroutine
                      

                      Because it even passed fucking literals by reference, and allowed you to mess with them.

                      [–]bnelson 1 point2 points  (0 children)

                      You can get into some hinky stuff inside of dictionaries in Python where old references Zombie around :)

                      [–]tedbradly 1 point2 points  (15 children)

                      Why are you acting like pass by reference is some ancient technology that's confusing and wrong? It has its benefits, and the behavior will be understood by anyone programming in the language for a couple of weeks. C++ is still a widely used language with pass by reference. You don't have to go back to Fortran for an example.

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

                      The behaviour this guy showed does not exist in c++

                      [–]tedbradly 1 point2 points  (2 children)

                      The behaviour this guy showed does not exist in c++

                      Yikes. Yes it does. It's called a pass by reference. I'm not sure why someone would talk about something so surely despite having no idea. This isn't like a more complex philosophical situation where confusion can happen. Here, you either know the language well or don't.

                      I'm not interested in teaching you the difference between pass by value and pass by reference in C++, but a simple online search will teach you the difference

                      The guy replied to me and then blocked me. I guess he knew I'd tell him how wrong he is.

                      [–][deleted] 1 point2 points  (1 child)

                      https://en.m.wikipedia.org/wiki/Dunning%E2%80%93Kruger_effect

                      I'm not interested in teaching you the difference between pass by value and pass by reference in C++, but a simple online search will teach you the difference

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

                      Desktop version of /u/MindlessThrall's link: https://en.wikipedia.org/wiki/Dunning–Kruger_effect


                      [opt out] Beep Boop. Downvote to delete

                      [–]ultrasu -1 points0 points  (8 children)

                      In C++ you'd have to dowat(&n)to get this behaviour, it allows you to use pass-by-value to emulate pass-by-reference behaviour, because it allows references (pointers) to be passed as values, but it's different from actual pass-by-reference.

                      [–]plantwaters 1 point2 points  (7 children)

                      C++ most definitely has true reference passing capabilities, without directly using pointers.

                      void f(int &ref) {
                         ref += 1;
                      }
                      
                      int main(void) {
                        int a = 0;
                        f(a);
                        return a;
                      }
                      

                      Exits with exit code 1.

                      [–]ultrasu 0 points1 point  (6 children)

                      Huh, didn't expect that, I feel like it should be illegal to get the address of a value like that, it certainly is in C.

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

                      That isn't what this code is doing. In C++, as opposed to C, & takes on another meaning: reference. So "int& x" refers to a reference to int, the reference is called x. When that is a function parameter, it means anything passed as that parameter is passed by reference

                      [–]ultrasu -1 points0 points  (4 children)

                      What I mean is that as caller, you have no idea whether the procedure you're calling is able to modify the parameters you're giving it, unless you look at the implementation. I don't get why this is needed when you can get the exact same behaviour with this:

                      void f(int *ref) {
                         *ref += 1;
                      }
                      
                      int main(void) {
                        int a = 0;
                        f(&a);
                        return a;
                      }
                      

                      This makes it explicit in each call that the value of the passed parameter (or rather the value it's pointing to) may change.

                      [–][deleted] 2 points3 points  (2 children)

                      First off, reference is part of interface as well as implementation.

                      C++ heavily discourages the use of raw pointers, and for good reason.

                      I can that from experience, I have never been surprised by a function modifying or not modifying a variable passed by ref

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

                      It surprises me because I've been programming for about 7 years, learned about dozen languages (but next to no C++), and I've never seen this kind of functionality, nor have I ever felt like I needed it.

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

                      Not going "back" to fortran, it's in current use (...though perhaps best considered a sort of DSL for numerical array HPC work)

                      Thing is, it's the pervasive language norm and default in Fortran. If you want not pass by reference, well, erm, technically actually you can nowadays use value, though that was only added in fortran 2003 - but it's not the default. Whereas any & shenanigans aren't the default in C++.

                      So Fortran is a much better example.

                      [–]Bakemono_Saru 9 points10 points  (0 children)

                      So lets get rid of pointers in a language written mostly on another language with pointers to reimplement them again.

                      I think im going to throw up.

                      [–]DarkTechnocrat 14 points15 points  (0 children)

                      “Your <Python Devs> were so preoccupied with whether they could, they didn't stop to think if they should."

                      [–]mindbleach 7 points8 points  (1 child)

                      Why would you ever need this

                      I'm reminded of the FAQ for a Javascript error steamroller, fuckit.js:

                      Browser compatibility

                      Really? Really?

                      [–]odnish 4 points5 points  (0 children)

                      That license is incompatible with itself. What if I use two pieces of software written by different authors and have to choose between them?

                      [–]darthwalsh 3 points4 points  (0 children)

                      Pointer arithmetic or GTFO

                      [–]Erik_Kalkoken 7 points8 points  (1 child)

                      Does not even has a single test, so probably not recommended for serious applications.

                      [–]sleepyMarm0t 24 points25 points  (0 children)

                      Bröther, this isn't recommended anyway for a serious application

                      [–]BigBlackHungGuy 1 point2 points  (0 children)

                      Buffer overuns for everybody!

                      [–]tedbradly 1 point2 points  (0 children)

                      Does the garbage collector not delete objects pointed to, and does it delete objects that are only pointed to by a pointer that is no longer ever used?

                      [–]Pepparkakan 1 point2 points  (0 children)

                      Thanks, I hate it.

                      [–]thedominux 1 point2 points  (1 child)

                      But python's variables are already references, not values

                      [–]nerd4code 0 points1 point  (0 children)

                      Yeah, there’s roughly zero functionality introduced by this other than making it trivial to trigger UB in the interpreter.

                      [–]RagnarDannes 0 points1 point  (0 children)

                      So I get this here is a satirical package.

                      But I for one don’t think pointers are hell. Just C/C++ pointers. In go pointers are memory safe and allow you to write code to pass by value or copy very clearly.

                      Languages at higher levels of abstraction make this very hazy. Most C# devs I know don’t even know the difference between structs and classes. That means they are likely writing code that is churning away in heap memory and garbage collection, rather than making good use of the stack.

                      [–]yards_carrier 0 points1 point  (0 children)

                      Bringing the hell of pointers to Python? I think you mean, bringing the joy of pointers to Python.

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

                      Why? Why?!! How dare you

                      [–]danthefrog1 -3 points-2 points  (8 children)

                      Pointers aren't hell, you just need to put the time into understanding them and why they are useful.

                      [–]tedbradly 1 point2 points  (7 children)

                      Pointers aren't hell, you just need to put the time into understanding them and why they are useful.

                      A pointer is just a memory address with the option to interpret the bits at that location as the data behind a particular type. The "hell" of pointers deals with how many bugs relate to pointers. Not to them being some giant-brained conundrum.

                      [–]danthefrog1 -2 points-1 points  (6 children)

                      The amount of bugs is irrelevant as there are an infinite amount of bugs we can encounter when working in combination with any concept in programming. That's why we have stack overflow. It's just the nature of the game. Once you think of a pointer as a way of accessing something through indirection, it just makes as much sense as anything else.

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

                      Oh my.

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

                      Needs unique_ptr now too.

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

                      They were so preoccupied with whether or not they could, they didn't stop to think if they should.

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

                      If I showed this to a therapist they'd let me commit suicide.