How objects are stored

This is a discussion on How objects are stored within the C++ Programming forums, part of the General Programming Boards category; In the case of Code: Object obj1; Object obj2; obj1 = obj2; The object is copied. However in the case ...

  1. #16
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    In the case of

    Code:
    Object obj1;
    Object obj2;
    obj1 = obj2;
    The object is copied.

    However in the case of
    Code:
    Object obj;
    Object *p_obj2 = &obj;
    p_obj2 is assigned the address of obj, thus it points to that object.

    However, you have to realize that java uses something called reference counting. It will automatically release memory for you when it goes out of scope.

    ie in Java
    Code:
    public void method() {
            Integer x = new Integer(1);
            x = new Integer(2);
    }
    When you call method() a new Integer object is created and x is made to point to it. Then x is made to point to a different Integer object which hold the value of 2. Since the old Integer object is no longer referenced it can never be recovered, thus the JVM garbage collector will delete and re-use the memory where the Integer object that has a 1 is located.

    However, in C/C++ when you make a pointer point to another object you are expected to handle the old object. This can be an annoyance at first, but it's a bit of an advantage (performance wise) when you can decide when to delete the memory which was previously taken up by the object. In java there is no guarantee of when the garbage collector is going to be called.

    In java everything is pass by reference (except for primitives, which is pass by copy). In c++ everything is pass by copy. So when you don't want to copy an entire object, you just pass a copy of it's address (which is a pointer).

    We could go on and on about this all day.

    However, whenever I have a problem I go to .... no not google, but wikipedia and search it. Google is always second.

    Wikipedia has a great comparison of Java and C++. It's much more in depth than anyone here will feel like writing.

    cs_student

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by MTK View Post
    I thought C++ copies the whole object, not just a pointer.
    Unless you pass a pointer. That's the whole point. You can pass an object, making a copy of it. You could pass a pointer or a reference to the object. Either, as it suits the situation. So you have all the same choices you'd have in Java and then some.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by iMalc View Post
    To get closer to Java you probably want reference-counted smart-pointers, like shared_ptr.
    QFE!

    King Mir is also correct though, you don't necessarily need to be exactly like Java in most cases. Use the best tools that the language provides you. Often that will be references, rather than any kind of pointer or smart-pointer.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,756
    Quote Originally Posted by cs_student
    In java everything is pass by reference (except for primitives, which is pass by copy). In c++ everything is pass by copy. So when you don't want to copy an entire object, you just pass a copy of it's address (which is a pointer).
    On the contrary, in Java there is only pass by value (i.e., copy), but in C++ one has the choice of pass by value or pass by reference. However, variables of class type in Java are actually reference variables, whereas in C++ they really are object variables.

    Quote Originally Posted by cs_student
    We could go on and on about this all day.

    However, whenever I have a problem I go to .... no not google, but wikipedia and search it. Google is always second.

    Wikipedia has a great comparison of Java and C++. It's much more in depth than anyone here will feel like writing.
    Sure, but it is also prone to blunders by editors who do not really know what they are talking about. For example, you presumably stated that "In java everything is pass by reference (except for primitives, which is pass by copy)" after reading that article's statement that in Java "Primitive data types always passed by value. Objects passed by reference." However, that very statement has a reference to another article, which actually contradicts this conclusion in favour of what I stated: Java is Pass-by-Value, Dammit! So much for citing references in Wikipedia, eh?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I'm going to call it pass-pointer-by-value, haha.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,626
    Everything is pass-by-value in Java, and the fact that primitive types aren't really objects, thus does not use references makes the whole damn thing just worse. Of course, then Java have to provide wrapper types that are Objects to the primitive types. Again, what's the point of both?

    So pass a primitive type to a function - it is copied. Pass an object to a function - its reference, the reference you actually pass, is copied.
    Then there's the fact that you can't actually make a reference to a reference in Java...
    But it is possible to make a pointer to a pointer or reference to a pointer in C++.

    Oh yes, don't forget that there is a difference.
    Sometimes you WANT to make a copy of an object. In Java, this is tricky or annoying. Usually, constructing a new object and passing the old one in the constructor or using a clone member function.
    This works OK when you need polymorphism, but not for being able to copy objects in a simple way. It's way less tedious in C++.
    So Java's way isn't "better," because there are times when you want a copy of the actual object.
    Last edited by Elysia; 11-30-2009 at 02:29 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    A true pass by reference is
    Code:
    void foo(ojb a)
    {
       a= 100;
    }
    and when the function ends, a == 100

    In C++ this is not done. The type isn't "obj a". It is "obj& a". So a reference is passed by value. It is like C, except that C would use * rather than &. Their difference is
    Code:
    foo(a); //C++
    foo(&a); //C
    That is a syntax difference.

    Java and C# compine two different meaning in one thing. Pointer and object becomes a reference and that causes the confusion. But there is confusion only because you think in a C++ way when you use Java. In Java you are not supposed to think references as pointers, but as objects, even though they are actually pointers.

    So concluding, if you think everything in Java as an object then objects are passed by reference. If you think that references are pointers, then they are passed by value. The only exception is when you assign null. Then the "passing an object by reference" scheme doesn't work, but that is the only exception.
    I believe Java is indented to use the "passing objects by reference". It fits it more.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,756
    Quote Originally Posted by C_ntua
    A true pass by reference is
    That could very well be pass by name (or call by name, if you prefer). I posit that your example does not adequately express this "true pass by reference".

    Quote Originally Posted by C_ntua
    In C++ this is not done. The type isn't "obj a". It is "obj& a". So a reference is passed by value.
    The problem with saying that "a reference is passed by value" is that it is not. The reference is an alias, and that is at the heart of call by reference. As such, C++ references really do implement call by reference.

    Quote Originally Posted by C_ntua
    That is a syntax difference.
    I'd say that the syntax difference is important. One reason why it is important to note that a pointer is passed by value, even though passing a pointer gives call by reference semantics, is that it is possible to change the value of the pointer itself. It is not possible to change the value of a reference itself, and thus a reference is completely an alias.

    EDIT:
    Quote Originally Posted by C_ntua
    So concluding, if you think everything in Java as an object then objects are passed by reference. If you think that references are pointers, then they are passed by value. The only exception is when you assign null. Then the "passing an object by reference" scheme doesn't work, but that is the only exception.
    I believe Java is indented to use the "passing objects by reference". It fits it more.
    I suggest that you read the article that I linked to. As for references and pointers: pointers implement a reference mechanism; valid non-null pointers refer to something.
    Last edited by laserlight; 11-30-2009 at 04:49 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by MTK View Post
    I thought C++ copies the whole object, not just a pointer.
    if you say:
    Code:
    object obj1;
    object obj2 = obj1;
    then yes, it copies the entire object.

    but if you do the following:
    Code:
    object obj1;
    object& obj2 = obj1;
    OR
    object* obj2 = &obj1;
    then all it does is copy a 4-byte (or whatever the default size is for a pointer on your platform) pointer, and the second object IS the first object like in java.

  10. #25
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    This is what I'm talking about:

    Code:
    int main() {
        MyObject *o;
        o = createObject();
        cout << o->number << "\n";  //Is pointer o still valid, of did the o created by createObject() go out of scope, causing a segfault?
        return 0;
    }
    
    MyObject *createObject() {
        MyObject o;
        o.number = 4;
        return &o;
    }
    Last edited by MTK; 11-30-2009 at 07:44 AM.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,756
    Quote Originally Posted by MTK
    This is what I'm talking about:
    And this is what iMalc is talking about:
    Code:
    // ...
    
    int main() {
        std::tr1::<MyObject> o;
        o = createObject();
        cout << o->number << "\n";
        return 0;
    }
    
    std::tr1::shared_ptr<MyObject> createObject() {
        std::tr1::shared_ptr<MyObject> o(new MyObject);
        o->number = 4;
        return o;
    }
    Quote Originally Posted by MTK
    Is pointer o still valid, of did the o created by createObject() go out of scope, causing a segfault?
    The pointer is invalid since the local object that it points to has been destroyed. The behaviour is undefined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #27
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    So is there a way to create a function that returns a valid pointer to an object that it created?

    I know it can be done with malloc(), but it there a better "C++ Way"?

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,756
    Quote Originally Posted by MTK
    So is there a way to create a function that returns a valid pointer to an object that it created?
    Yes. I have shown you one way. You could use an ordinary pointer too, except that you would have to care of memory management. (But you could use a smart pointer in the caller to immediately take ownership of the object pointed to by the pointer returned.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Code:
    // ...
    
    int main() {
        std::tr1::<MyObject> o;
        o = createObject();
        cout << o->number << "\n";
        return 0;
    }
    
    std::tr1::shared_ptr<MyObject> createObject() {
        std::tr1::shared_ptr<MyObject> o(new MyObject);
        o->number = 4;
        return o;
    }

    So this is the way you do it?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,756
    Quote Originally Posted by MTK
    So this is the way you do it?
    That depends on what I am trying to do, and what I can do. If I had the choice, I would write:
    Code:
    // ...
    
    int main() {
        MyObject o(4);
        cout << o << "\n";
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of pointers to vector of objects
    By Litz in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2009, 02:29 PM
  2. Objects as attributes of other objects.
    By kbro3 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2009, 03:46 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

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