Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Just a C++ tutorial

Just a C++ tutorial

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++comtutorial
14 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndreFratelli
    wrote on last edited by
    #1

    http://www.functionx.com/cpp/examples/returnpointer.htm[^]

    Fratelli

    K M C S V 5 Replies Last reply
    0
    • A AndreFratelli

      http://www.functionx.com/cpp/examples/returnpointer.htm[^]

      Fratelli

      K Offline
      K Offline
      Kevin Drzycimski
      wrote on last edited by
      #2

      one of the many "DON'TS" in C++ i hope this tutorial is just a "do not"

      OriginalGriffO 1 Reply Last reply
      0
      • K Kevin Drzycimski

        one of the many "DON'TS" in C++ i hope this tutorial is just a "do not"

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        I'm afraid not - it seems to be a genuine "tutorial" site... Perhaps he is just doing job creation: "If I teach them to code like this, I can get the job of fixing it"?

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          I'm afraid not - it seems to be a genuine "tutorial" site... Perhaps he is just doing job creation: "If I teach them to code like this, I can get the job of fixing it"?

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Elephanting scary. Allocate a variable on the local stack frame, then return a pointer to it. X| Definitely a make-work for somebody. Cheers, Peter

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          OriginalGriffO 1 Reply Last reply
          0
          • P Peter_in_2780

            Elephanting scary. Allocate a variable on the local stack frame, then return a pointer to it. X| Definitely a make-work for somebody. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            I know what you mean. Way too easy to do by mistake, and a real PITA to find and fix in a big project. Definitely not something you should be teaching people to do. :omg:

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            K 1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              I know what you mean. Way too easy to do by mistake, and a real PITA to find and fix in a big project. Definitely not something you should be teaching people to do. :omg:

              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

              K Offline
              K Offline
              Kevin Drzycimski
              wrote on last edited by
              #6

              nevertheless the pointer is immediately dereferenced, so in this example it is perfectly OK :laugh:

              OriginalGriffO 1 Reply Last reply
              0
              • K Kevin Drzycimski

                nevertheless the pointer is immediately dereferenced, so in this example it is perfectly OK :laugh:

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                Of course! :laugh: Don't you just love code that works more by luck than by judgement? ;)

                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                A 1 Reply Last reply
                0
                • A AndreFratelli

                  http://www.functionx.com/cpp/examples/returnpointer.htm[^]

                  Fratelli

                  M Offline
                  M Offline
                  Mike Winiberg
                  wrote on last edited by
                  #8

                  You what! Hmm, I spent many happy hours tracking down a strange bug in a database library I was using that was a subtle variant of this: The library passed data objects by value, but treated them as passed by reference: this worked when compiled with Borland C++, because the optimiser effectively took away the creation of the temporary stack objects that are required by the spec and passed a reference. gcc did it properly though, and allocated the temporary objects and then threw them away - result, the system worked perfectly on Windows (compiled with Borland) and failed in mysterious ways on Linux. To their credit, once I told the authors, they fixed it very quickly... 8) Perhaps this chap used to work for them...

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Of course! :laugh: Don't you just love code that works more by luck than by judgement? ;)

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    A Offline
                    A Offline
                    Andy Bantly
                    wrote on last edited by
                    #9

                    A sudden sense of not being a hack has temporarily settled down over me.

                    1 Reply Last reply
                    0
                    • A AndreFratelli

                      http://www.functionx.com/cpp/examples/returnpointer.htm[^]

                      Fratelli

                      C Offline
                      C Offline
                      Cesar de Souza
                      wrote on last edited by
                      #10

                      How could this possibly work? If we declare a pointer to a variable declared in the stack, the variable will be valid only as long as the current stack frame is active. When the control returns to the caller, wouldn't the variable be lost in stack? I mean, if we call another function in sequence, wouldn't it be overwritten?

                      Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                      A 1 Reply Last reply
                      0
                      • C Cesar de Souza

                        How could this possibly work? If we declare a pointer to a variable declared in the stack, the variable will be valid only as long as the current stack frame is active. When the control returns to the caller, wouldn't the variable be lost in stack? I mean, if we call another function in sequence, wouldn't it be overwritten?

                        Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                        A Offline
                        A Offline
                        AndreFratelli
                        wrote on last edited by
                        #11

                        Why do you think it's a code horror? =P

                        Fratelli

                        C 1 Reply Last reply
                        0
                        • A AndreFratelli

                          Why do you think it's a code horror? =P

                          Fratelli

                          C Offline
                          C Offline
                          Cesar de Souza
                          wrote on last edited by
                          #12

                          Immediately after posting I realized where I was X|

                          Interested in Machine Learning in .NET? Check the Accord.NET Framework. See also Handwriting Recognition Revisited: Kernel Support Vector Machines

                          1 Reply Last reply
                          0
                          • A AndreFratelli

                            http://www.functionx.com/cpp/examples/returnpointer.htm[^]

                            Fratelli

                            S Offline
                            S Offline
                            starmerak
                            wrote on last edited by
                            #13

                            Shouldn't that one be under "coding horrors"?

                            Nothing exists, everything is opinions ( and no, the opinions don't exist either )

                            1 Reply Last reply
                            0
                            • A AndreFratelli

                              http://www.functionx.com/cpp/examples/returnpointer.htm[^]

                              Fratelli

                              V Offline
                              V Offline
                              Vladimir Svyatski
                              wrote on last edited by
                              #14

                              This tutorial should be called: "How to get your first access violation runtime error in C++".

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups