would ever passing a primitive to functions by reference be more effecient ?

Discussion in 'C' started by Garrett, Oct 10, 2010.

  1. Garrett

    Garrett New Member

    Joined:
    Oct 10, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    In a scenario that a caller function needs to pass one of its local variables of a primitive type to a callee and doesn't necessarily need to pass it by reference to track that variable later after callee is done, I know that it'd be said to pass it by value and that makes total sense for primitive types with a size equal to or smaller than size of pointer; but I'm curious that from an assembly point(performance-wise or space-wise), wouldn't be there a situation when passing a variable of a primitive type with a size bigger than pointers like long double on my platform which has a size of 8 bytes and is bigger than pointers that have a size of 4 bytes; by reference would be more efficient? like an imagined situation where pointer can be loaded directly into some register by caller but the primitive itself not and thus no need to load the pointer from callee stack frame to some register by callee and there's 8 more free bytes of stack memory in the end comparing to pass by value where there'd be 8 more used bytes of stack memory.
    If in this specific case, passing by reference might ever be more efficient, how can we know to pass by reference or value?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Design choices like this always hang off the application design requirements. If it's needed to pass by value then pass by value. If by reference then by reference. If pass by value is inefficient, first profile the code and find out where it's not performing to requirements, then work on the most inefficient areas first. You may find this particular "inefficiency" doesn't actually cause any significant problems.

    If you're getting stack overflows and the stack can't be extended, then it's appropriate to look at such issues, but again this is requirements driven.

    But to address your question directly, yes of course it's more efficient to pass by reference than by value when the value is bigger than a reference, for the simple reason that you're copying fewer bytes. The *important* question is whether that inefficiency is significant and is worth spending time on.
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice