Question

In: Computer Science

Although pass-by-reference can be used to substitute all pass-by-value functions, why is pass-by-value be considered safer?...

  1. Although pass-by-reference can be used to substitute all pass-by-value functions, why is pass-by-value be considered safer?
  2. When passing an integer array to a function, why do you also need to pass the number of elements in that array?

Solutions

Expert Solution

  • Pass by value is considered safer as you cannot accidentally modify the parameter variable passed in the function and you may not run into issues related to this.
  • When passing an array to a function, the whole array is not passed to the function.
  • Instead, a pointer to the first element of the array is passed to the function.
  • Native arrays are not objects, they are just continuous memory locations of the defined data type. The name of the array is a pointer to the first element of the array. Example: int arr[10], is nothing but 10 continuous int memory allocations and arr is the address of the first element of the array. Adding 1 to arr will give a pointer to the next memory location.  
  • Since it's just a pointer that is being passed to a function, the only way of knowing where the upper bound of the array lies is to manually pass in the size of the array.
  • Keep in mind that even though the array has just 10 elements, even arr+15 will work and accessing the location may give a garbage value.

If you have any more queries, please post them down in the comment section.


Related Solutions

Give one example that will show that pass by value, pass by Reference, pass by name,...
Give one example that will show that pass by value, pass by Reference, pass by name, pass by value result are all different.
What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function...
What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function makes a change to that received array and why? Functions in C++ can return only one data item (e.g., variable) using the return statement and they also cannot return arrays. There is, however, another way to "return" more than one item from a function without using the return statement. Can you explain how and why? How are arrays represented in memory? What is the...
Although not considered the primary pacemaker of the heart, the atrioventricular node can indeed be considered...
Although not considered the primary pacemaker of the heart, the atrioventricular node can indeed be considered as playing SOME role in setting the rhythm of the heart (kind of a secondary pacemaker). Explain how/why.
Although US can be considered as an example of capitalism, discuss if the United States still...
Although US can be considered as an example of capitalism, discuss if the United States still fits the model. Provide examples to support statements.
Explain why NADPH is considered a reducing agent although the NADPH itself loses electrons.
Explain why NADPH is considered a reducing agent although the NADPH itself loses electrons.
Although tryptophan contains a heterocyclic amine, it is considered a neutral amino acid. (a) Explain why...
Although tryptophan contains a heterocyclic amine, it is considered a neutral amino acid. (a) Explain why the indole nitrogen of tryptophan is more weakly basic than one of the imidazole nitrogens of histidine.
Why can race be considered a reality?
Why can race be considered a reality?
We learned that when calling subprograms or functions that we can pass data to the subprogram...
We learned that when calling subprograms or functions that we can pass data to the subprogram or function by ‘value’ or by ‘reference’. Describe what each approach is, how it works and what the potential security disadvantage is when passing parameters by reference.
What are the four functions of money? Can something be considered money if it does not...
What are the four functions of money? Can something be considered money if it does not fulfill all four functions? Please give an example.
Although the Internet can be an invaluable resource to all of us, there is also a...
Although the Internet can be an invaluable resource to all of us, there is also a dark side to this resource. In this discussion we’ll take a look at the many high-tech ways that people misuse technology. For the purposes of this discussion go to: Special Collection: Safety & Privacy in a Digital World http://www.vawnet.org/special-collections/TechSafety.php 1. Choose one of the following links and read all of the accompanying articles: Stalking/Surveillance Bullying/Harassment Child Sexual Abuse/Exploitation Sexting Social Media/Networking 2. Respond to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT