Thread: Newb Question on Passing Objects as Parameters

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    28

    Question Newb Question on Passing Objects as Parameters

    Does passing a pointer to an Object to a function instead of the Object itself save memory and the little time spent in copying memory?

    Passing pointer would be:
    Code:
    myFunc(Class *object)
    Object itself:
    Code:
    myFunc(Class object)
    I know it does when you pass a struct, and I know it is recommended only when the amount of data passed is very large. What I don't know is how do the objects behave when passed by parameter...

    Perhaps passing an Object actually means passing a reference to it (which would seem logical to my OOP learnings), and thus the idea of passing a pointer to it is useless (given my purposes).

    I should remark time and memory is going to be a serious issue on my project, since there will be a lot of data processing involved.


    Thanks in advance,

    Mariano López Gappa.


    PS: Me = huge fan of this forum, and ppl in it Really helpful, great work!

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    If you pass a copy you create a local copy of the class, including all it's members, so it will save space, and probably time needed to construct the object. You get the same result passing a reference, so you don't have to use arrow operators to interact with the object

    Code:
    myFunc(Class &object)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Passing by address means that the address of the object is passed to the function, thus a pointer. Passing by value means that the object is copied to a new variable, inside the scope of a function.

    The speed and memory difference is insignificant. Never optimize before you have a bottle neck. That is, if you have 10 elements in an array and want to sort them out, using quick sort would be useless.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    Ok, so you mean like this right?

    When I invoke:
    Code:
    myFunc(&object);
    When I declare:
    Code:
    myFunc(Class &object){...}

    Thx for the way-early reply!!!

    Mariano L. Gappa

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The speed and memory difference is insignificant.
    It depends on the size of the object, the complexity of the copy-constructor
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Mariano L Gappa
    Ok, so you mean like this right?

    When I invoke:
    Code:
    myFunc(&object);
    When I declare:
    Code:
    myFunc(Class &object){...}

    Thx for the way-early reply!!!

    Mariano L. Gappa
    actually you invoke
    Code:
    myFunc(object);
    if you declare a function as having a reference parameter, then when you pass an object, it just gets the reference of the object.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    When I declare:

    Code:
    Code:
    myFunc(Class &object){...}
    yes

    When I invoke:

    Code:
    myFunc(&object);
    No
    The correct way to pass a reference is
    myFunc(object);

    PS. Note that if you do not plan to change the object you can declare parameter as const reference:
    int myFunc(const Class &object);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    Quote Originally Posted by Desolation
    Passing by address means that the address of the object is passed to the function, thus a pointer. Passing by value means that the object is copied to a new variable, inside the scope of a function.
    Yeah, I was familiar with those definitions, what I didn't know was if they were applicable to objects as well (I mean, of course it is, but perhaps it wasn't needed).

    Quote Originally Posted by Desolation
    The speed and memory difference is insignificant. Never optimize before you have a bottle neck. That is, if you have 10 elements in an array and want to sort them out, using quick sort would be useless.
    I agree, but sadly Imma be working with say 10-minute processing algorithms, and a lot of Object-passing inbetweens, so you could say nanosecond delays would turn into second delays...


    Thx for the insight, let me know if you still think I shouldn't optimize it...

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A general rule of thumb is to pass by value built-in types and small classes/structs that are easily copied. Larger classes or classes that are expensive to copy should be passed by const reference.
    Code:
    void foo(int, const std::vector<int>&);
    
    //...
    
    std::vector<int> vec(1000);
    int i = 500;
    foo(i, vec);
    If you are modifying the variable, then a non-const reference should be used. If the variable could be null, then use a pointer.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    28

    Wink

    Quote Originally Posted by vart
    No
    The correct way to pass a reference is
    myFunc(object);
    My bad . Imma try it now. I thought I'd get type mismatch that way...

    Thx for replies

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    903
    When you have your program done, use a profiler to find the bottle necks, you'll know what to change and what not to change.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Premature optimization is bad, that is true, but using a clearly less efficient option when all else is equal is also bad. There is no reason not to pass classes like vector or string (or your own classes with members of vector, string, etc) by const reference.

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    Thx for all the replies. I've learned lots about the subject. Thx Daved for your expertise, you've been of great help eversince I've joined back in 2005. I'll be back really soon

    Mariano Lopez Gappa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Question about passing & receiving parameters
    By TCB in forum C Programming
    Replies: 9
    Last Post: 04-11-2006, 06:08 AM
  4. Passing objects
    By xshapirox in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2004, 11:34 AM
  5. Passing objects
    By Wiggin in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 08:00 AM