In a scenario that caller doesn't necessarily need callee to use memory of one of its local variables of a primitive type to track that variable later for anything, I know that when passing that variable to a callee function; it'd be done 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 passing by reference might ever be more efficient, how can we know to pass by reference or value?
Garrett2011
0
Light Poster
Recommended Answers
Jump to PostI'm not really sure what you're asking, but I think you're asking about the efficiency of passing conventions.
In your example, if you were originally passing an 8-byte long and you change it to a 4-byte pointer it wouldn't make an 8-byte difference. It would only make a 4-byte …
Jump to Postwould ever passing a primitive to functions by reference be more efficient ?
Yes, because you avoid passing a copy. So you save memory and execution time
All 5 Replies
Fbody
682
Posting Maven
Featured Poster
Stefano Mtangoo
455
Senior Poster
Garrett2011
0
Light Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
TechnoCat
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.