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. General Programming
  3. C / C++ / MFC
  4. References vs. Pointers

References vs. Pointers

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studioperformancetutorial
9 Posts 6 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.
  • Y Offline
    Y Offline
    Yonggoo
    wrote on last edited by
    #1

    I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!

    Yonggoo

    N

    J M 2 Replies Last reply
    0
    • Y Yonggoo

      I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!

      Yonggoo

      N

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Yonggoo wrote:

      using references is safer than using pointer

      Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two. Node* pNode = new Node; Don't know what the references line has to do with anything.

      Yonggoo wrote:

      Is using pointers faster than using references?

      With an optimizing compiler the performance is generally the same since internally references are pointers.

      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      Y J 2 Replies Last reply
      0
      • J Joe Woodbury

        Yonggoo wrote:

        using references is safer than using pointer

        Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two. Node* pNode = new Node; Don't know what the references line has to do with anything.

        Yonggoo wrote:

        Is using pointers faster than using references?

        With an optimizing compiler the performance is generally the same since internally references are pointers.

        Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        Y Offline
        Y Offline
        Yonggoo
        wrote on last edited by
        #3

        Is this code OK! Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; } Thanks!

        Yonggoo

        D J 2 Replies Last reply
        0
        • Y Yonggoo

          Is this code OK! Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; } Thanks!

          Yonggoo

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Yes, but you've failed to initialize Nodes[0]. The preferred style is:

          for (int i = 0; i < 101; ++i)
          ...


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • Y Yonggoo

            Is this code OK! Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; } Thanks!

            Yonggoo

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            No, since you can't convert Node*[101] to Node&. You could return Nodes[101] as a Node**. I'm not sure how you would return it as a reference or why you would bother.

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            1 Reply Last reply
            0
            • Y Yonggoo

              I have three questions? ONE: It is said "using references is safer than using pointer." Why? TWO: If I want node objects in heap, how can I assign the pointers from heap memory? example code) Node& Nodes[100]; ? = new Node(); THREE: Is using pointers faster than using references? Thanks!

              Yonggoo

              N

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Yonggoo wrote:

              Node& Nodes[100]; ? = new Node();

              I don't think you can have an array of references

              Yonggoo wrote:

              Node* Nodes[101]; refNodes& getNodes() { for(int i=1;i<=100; ++i) { Nodes[i] = new Node(); } return Nodes; }

              return Nodes; is returning a Node**. Is that what a refNodes& is? Mark

              "Do you know what it's like to fall in the mud and get kicked... in the head... with an iron boot? Of course you don't, no one does. It never happens. It's a dumb question... skip it."

              1 Reply Last reply
              0
              • J Joe Woodbury

                Yonggoo wrote:

                using references is safer than using pointer

                Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.) I don't understand what you are doing with two. Node* pNode = new Node; Don't know what the references line has to do with anything.

                Yonggoo wrote:

                Is using pointers faster than using references?

                With an optimizing compiler the performance is generally the same since internally references are pointers.

                Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #7

                Joe Woodbury wrote:

                Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)

                That is a red herring...

                struct A {
                int a;
                };

                void func1(A& r)
                {
                printf("r.a = %d\n", r.a);
                }

                void func2(A* p)
                {
                func(*p);
                }
                ...
                A* p = 0;
                func2(p);

                Guess where it'll blow up on you in virtually all C++ compilers? References in C++ are not safer than pointers. It's just an illusion...

                P 1 Reply Last reply
                0
                • J Jorgen Sigvardsson

                  Joe Woodbury wrote:

                  Because a pointer can be NULL. A reference means the object has to exist (of course, that object may not be fully initialized, but it does mean you don't have to worry about a NULL pointer.)

                  That is a red herring...

                  struct A {
                  int a;
                  };

                  void func1(A& r)
                  {
                  printf("r.a = %d\n", r.a);
                  }

                  void func2(A* p)
                  {
                  func(*p);
                  }
                  ...
                  A* p = 0;
                  func2(p);

                  Guess where it'll blow up on you in virtually all C++ compilers? References in C++ are not safer than pointers. It's just an illusion...

                  P Offline
                  P Offline
                  PJ Arends
                  wrote on last edited by
                  #8

                  Joergen Sigvardsson wrote:

                  void func2(A* p) { func(*p); }

                  The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe. And one wonders why so many products have stupid warning labels; to prevent them from being used in ways they were not intended to be used.


                  You may be right
                  I may be crazy
                  -- Billy Joel --

                  Within you lies the power for good, use it!!!

                  J 1 Reply Last reply
                  0
                  • P PJ Arends

                    Joergen Sigvardsson wrote:

                    void func2(A* p) { func(*p); }

                    The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe. And one wonders why so many products have stupid warning labels; to prevent them from being used in ways they were not intended to be used.


                    You may be right
                    I may be crazy
                    -- Billy Joel --

                    Within you lies the power for good, use it!!!

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9

                    PJ Arends wrote:

                    The only reason it blows up is because you are dereferencing a NULL pointer, not because references are not safe.

                    My point is that you can't trust a reference to be always valid, as many people suggest.

                    -- Fun for the whole family - except grandma and grandpa

                    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