Question

In: Computer Science

In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...

In C/C++ programming language, write down the lines of codes (and figure) to show-

a) assignment-by-sharing

b) dangling reference

Solutions

Expert Solution

Though 'assignment-by-sharing' term does not clears the aim on its own but checking the second part of dangling reference, 'assignment-by-sharing' must be assignment by sharing using reference.

Refer the lines of C++ codes below to understand :

a) Assignment-by-sharing

int main()

{
int x = 10;

int &y=x; //y is reference to x

y=30;

cout<<"x = "<<x<<endl;

x=50;

cout<<"y = "<<y<<endl;

return 0;

}

Here the output will be

30

50

This is because both x and y are sharing the same reference. Hence assigning a value to x will lead to assignment in y and vice - versa.

b) Dangling Reference

int &fun()

{
int i = 10;

return i;

}

int main()

{
int &x = fun();

cout<<x<<endl;

return 0 ;

}

This code seems to be correct but is not. Here 'i' is returned as a reference to fun() and 'i' is local to this function. So when the function fun() is finished, it will deleted 'i' from its stack. Means 'i' is no longer available and 'x' is pointing to 'i'. In this case, 'x' has no idea that 'i' is no longer available. Hence it is a dangling reference.


Related Solutions

Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED Description: Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html), develop a program that generates all permutations for the set {1, 2, 3, 4, 5}. The output should contain all 5! = 120 permutations. Output Sample: P#1: 1 2 3 4 5 P#2: 1 2 3 5 4 P#3: 1 2 4 3 5 ... P#120: 5 4 3 2 1 Hint: - Thoroughly study the...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Complete the following assignment in C programming language. Get the user’s first name and store it...
Complete the following assignment in C programming language. Get the user’s first name and store it to a char array Declare a character array to hold at least 20 characters. Ask for the user’s first name and store the name into your char array. Hint: Use %s for scanf. %s will only scan one word and cannot scan a name with spaces. Get 3 exam scores from the user: Declare an array of 3 integers Assume the scores are out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT