It's UWAweek 48

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

Displaying the 2 articles in this topic
Showing 2 of 919 articles.
Currently 4 other people reading this forum.


 UWA week 37 (2nd semester, week 7) ↓
SVG not supported

Login to reply

👍?
helpful
7:36pm Thu 15th Sep, Thomas M.

Hi Chris, I was wondering (mostly out of curiosity but also regarding the project) whether pass-by-value or pass-by-reference requires more memory? I would imaging that pass-by-value, would require a lot of memory for making copies, especially when dealing with large arrays or structs. On the other hand, I would expect pass-by-reference to require a small, fixed amount of memory no matter what, since the size of an address is fixed. Regards, Thomas


SVG not supported

Login to reply

👍?
helpful
5:54am Fri 16th Sep, Christopher M.

"Thomas Morton" <23*6*4*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> I would imaging that pass-by-value, would require a lot of memory for making copies, especially when dealing with large arrays or structs. On the other hand, I would expect pass-by-reference to require a small, fixed amount of memory no matter what, since the size of an address is fixed.
Hi Thomas, You are correct that pass-by-value will always be more expensive, both in terms of required space on the runtime stack and in the time required to copy the data before calling the function. On contemporary CPUs, all pointers are the same size, so pass-by-reference can also make the compilers' life easier. But pass-by-value is the default mechanism in most programming languages, to prevent the function from accidently modifying the caller's original values. We also know in C that the name of an array is is equivalent to the address of the array's first element, so passing an array (name) to a function is akin to passing the array by reference. But sometimes you don't want this, we don't want the function to be able to modify the original data. So a neat trick is to wrap an array inside a structure, as structures are always passed-by-value:
typedef struct {
    int   array[N];
} MYTYPE;

void function(MYTYPE parameter)
{
    parameter.array[0] = .... ;
}

    ...
    MYTYPE localstruct;

    function(localstruct);
    ...

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  1:17AM Sep 14 2022
Privacy policy