Dereferencing Pointers help

This is a discussion on Dereferencing Pointers help within the C++ Programming forums, part of the General Programming Boards category; When dereferencing pointers does it create a whole new object for you to work with or does it refer back ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Dereferencing Pointers help

    When dereferencing pointers does it create a whole new object for you to work with or does it refer back to the original object without creating a new object?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    It refers back to the original object.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Okay, I'm going to assume when you say refers that you mean something like this:

    int i;
    int& j = i;

    In which case it will be a high performance thing when deferencing big classes and structs. Thakns.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    That code uses a reference, not a pointer. You can almost always interchange them though -- except for the few things pointers can do that references can't.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok ya that was a bad example. What I ment was dereferencing a pointer is like doing this:

    int i = 5;
    int& j = i; //int j = *i would be equivilent to this line of code right?

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Edit: Sorry for the double post, I am really having a hard time thinking today.


    Ok I think I got it all figured out. I just had to think and do a little testing, my brain had a hard start this morning.

    Code:
    int i = 5;
    int* p = &i;
    
    int j = *p;
    j = 7;  //j equals 7 but i equals 5 (it copied the contents i to j)
    
    int& q = *i;
    q = 7  //q points to i, i now equals 7
    Last edited by Rune Hunter; 07-13-2007 at 10:52 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes, you've got it right. Dereferencing a pointer returns a reference, but if you assign that to a non-reference variable then a copy occurs.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright perfect! Now that I got confirmation it all makes sence.

    Thanks everyone!

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Rune Hunter View Post
    In which case it will be a high performance thing when deferencing big classes and structs. Thakns.
    No, all pointers are the same size, so its exactly the same regardless of whether you're dereferencing a pointer to a large object, or a pointer to a built-in type.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Not so. Pointers may be different sizes, even between different types, not to mention between different architectures. Granted, having sizeof(type1 *) != sizeof(type2 *) is rare, but it's allowed by the standard (the C standard anyway, I'm assuming that the C++ one is of the same mind).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by dwks View Post
    Not so. Pointers may be different sizes, even between different types, not to mention between different architectures. Granted, having sizeof(type1 *) != sizeof(type2 *) is rare, but it's allowed by the standard (the C standard anyway, I'm assuming that the C++ one is of the same mind).
    I think its a given that between different platforms, then all bets are off for the size of any type (including the number of bits in a byte)

    Although within the same platform, I wonder, how would the situation be handled, when casting between void* and T* where sizeof(T*) != sizeof(void*) ?

    I was also under the impression that there was a relationship between the size of the address bus, and the size of a pointer (within any given platform), which would make it somewhat nonsensical to have varying pointer sizes. (unless perhaps the platform for some reason is dealing directly with more than one area of addressable memory)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. dereferencing pointers to pointers
    By Potterd64 in forum C++ Programming
    Replies: 3
    Last Post: 07-11-2006, 05:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21