I'd like to know if some kind of structure contains more than one primitive but its total size is less than or equal to size of a single cpu register like a 4-byte register, does it ever make sense for a compiler to put it in one of those 4-byte registers when passing it by value or reference to a function instead of making a copy of it on the callee stack or passing a pointer to it and in general when passing something more than a single primitive to a function like an array or an structure would passing in a cpu register ever come in handy?
sample of such structure:
struct sample{
public:
char char1;
char char2;
};
sample of passing the structure to a function:
void someFunc(const sample input){
//whatever
}
void someFunc(sample input){
//whatever
}
void someFunc(sample & input){
//whatever
}
void someFunc(const sample & input){
//whatever
}
This is defined in the application binary interface (ABI) of your execution environment. The standard does not say anything about processor registers when a function is called, so it is legal to create an environment where small structs are packed into a single processor register.
For the reference part, they are very likely to be passed as pointers anyway, since when inside the called function the address of a reference is taken, it must resolve to the address of the referenced object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With