Question

In: Computer Science

Assume we are going to copy a string "hello" from a source pointer src to a...

Assume we are going to copy a string "hello" from a source pointer src to a destination pointer dst.

From the viewpoint of common memory management errors, what is the problem with the following piece of code?

char *src = "hello";
char *dst;
strcpy(dst, src);

No memory space has been allocated for the pointer dst

No memory space has been allocated for the pointer src

The first statement (char *src = "hello";) could cause an error

The strcpy(dst, src) call should be strcpy(src, dst)

From the viewpoint of common memory management errors, what is the problem with the following piece of code?

int *x = (int *)malloc(sizeof(int));
printf("*x = %d\n", *x);

Forgetting to free memory before the malloc() call

Encountering an uninitialized read in the printf() call

No memory space has been allocated for the pointer x

Forgetting to free memory before the printf() call

Address translation transforms each memory access (e.g., an instruction fetch, load, or store), changing the virtual address provided by the instruction to a __________ address where the desired information is actually located.

pseudo

physical

internet

mobile

Solutions

Expert Solution

1.

Assume we are going to copy a string "hello" from a source pointer src to a destination pointer dst.

From the viewpoint of common memory management errors, what is the problem with the following piece of code?// allocates src to be an array 6 characters (5 for the string hello and 1 more for '\0' (end of string))
// src points to the address where h is stored
char *src = "hello";
// creates a pointer of type character but without allocating any memory to dst
char *dst;
// function that copies the contents of the source string to destination string
// the first argument is the pointer to destination array of characters(string) and second argument is the pointer to the source array of characters (string)
strcpy(dst, src);

But in the above code, destination array of characters is declared but not allocated any memory and strcpy just copies the contents of source array to the destination array, it doesn't allocate memory to the destination array. Hence the statement strcpy(dst, src); will generate an exception as dst is not allocated any memory.
Correct code:// allocate dst to size of src (strlen returns the size of string leaving the \0 character) so we add 1 more while allocating the memory
char *dst = (char*)malloc(sizeof(char)*strlen(src)+1);
strcpy(dst, src); // copy the contents of src to dst

1. No memory space has been allocated for the pointer dst - TRUE (Since dst is declared as pointer but not allocated any memory but an attempt to copy the contents from src to dst is made)

2. No memory space has been allocated for the pointer src - FALSE (as src has been allocated memory of size 6 which points to the memory where h is stored in string "hello")

3. The first statement (char *src = "hello";) could cause an error - FALSE (No error is caused, it is a valid C statement which initialized src to the string "hello" and allocates memory for src)

4. The strcpy(dst, src) call should be strcpy(src, dst) - FALSE (The syntax for strcpy is that first argument is the pointer to destination array and second argument is pointer to source array)

2. From the viewpoint of common memory management errors, what is the problem with the following piece of code?

int *x = (int *)malloc(sizeof(int)); // create a pointer of type int and allocate memory for storing an int value
printf("*x = %d\n", *x); // print the value stored in the memory stored in x (since x is not initialized with any value before printing, it will print garbage value)

1. Forgetting to free memory before the malloc() call - FALSE (x is declared and defined at the same time using the malloc statement. Before the malloc statement x is not a known variable and hence it is not possible to free the memory before the malloc() call. Also malloc allocates new memory to x and hence freeing of memory before malloc doesn't make any difference)

2. Encountering an uninitialized read in the printf() call - TRUE (x is allocated memory using malloc but is it not initialized any value before printf and hence it displays garbage value stored in the memory pointed by x)

3. No memory space has been allocated for the pointer x - FALSE ( malloc allocated the memory space for pointer x before the printf statement)

4. Forgetting to free memory before the printf() call - FALSE (deallocating memory using free before printf will still printthe garbage value as free releases the space and allows the malloc to use the memory allocated earlier to x to be allocated to other variable but if we don't set x to NULL, x will still be pointing to the memory earlier allocated to it and hence the output is still the same i.w garbage value)

3.

Address translation transforms each memory access (e.g., an instruction fetch, load, or store), changing the virtual address provided by the instruction to a __________ address where the desired information is actually located.

Address translation in memory management transforms each memory access (eg- an instruction fetch, load or store ) , i.e changes the virtual address provided by the instruction to a physical address where the desired information is actually located.

In case of memory access, since the size of main memory is small, it uses a concept of virtual memory which maps the memory address used by the program called virtual address in this virtual memory to the physical address in the computer memory where it is actually stored. Program addresses are addressed relative to the program which are virtual addresses stored by the computer to give the user a perception that the entire program is stored in contiguous memory location but in this is not the case. The virtual address is translated by the computer into the actual address where the instruction is actually stored so that it can be executed.


Related Solutions

Please don’t copy from the same site and No copy from any source. The work should...
Please don’t copy from the same site and No copy from any source. The work should be your own words and answer. And please don’t answers be handwriting Discuss an example of an expert system in any filed and explain the importance of creating this system.
We are going to assume that you are feeding 100 pigs from 15 to 260 lbs...
We are going to assume that you are feeding 100 pigs from 15 to 260 lbs of weight. Phase feeding utilizes a series of rations that meet the specific needs of swine based on the size of the pigs. Usually, there are 3 to 7 phases for finishing pigs, but for this exercise, we are only going to use 3. 15 pts Ration 1 - Starter- pig weight from 15 to 40 lbs consumes 50 lbs of an 18% crude...
Hello..I need a presentation (not copy paste from the internet, comprehensive and detailed) on the topic...
Hello..I need a presentation (not copy paste from the internet, comprehensive and detailed) on the topic of 6lowpan in networks and the topic is necessary
Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a...
Type into a character array, or copy from an online source, Lincoln’s Gettysburg Address. Write a C Language Program that will search for the following words and then print out the entire sentence from Lincoln’s Gettysburg Address for every occurrence of each of the specified words:             civil                  nation              nobly               people             place               proper             Your program should search through the Gettysburg Address and find the answers, not pre-load them to print out once a particular word is sought. This is...
**** PLEASE DO NOT COPY AND PASTE FROM ANOTHER SOURCE BECAUSE THE ANSWER IS INCOMPLETE********* Introduction:...
**** PLEASE DO NOT COPY AND PASTE FROM ANOTHER SOURCE BECAUSE THE ANSWER IS INCOMPLETE********* Introduction: IN C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The...
Please answer this in around 300 words. No copy and paste from another source. Explain why...
Please answer this in around 300 words. No copy and paste from another source. Explain why the process of new product development is frequently represented as a linear process and why this does not reflect reality?
Please answer at least in 300 words. Please do NOT copy and paste from another source....
Please answer at least in 300 words. Please do NOT copy and paste from another source. Discuss the differences between the traditional HDI in comparison to the “new” (NHDI) formulation. In what ways do you think either one is a better measure of human development?
Matlab work. Pleaae dont copy from any source. Write a function that will accept a structure...
Matlab work. Pleaae dont copy from any source. Write a function that will accept a structure as an argument and return two cell arrays containing the names of the fields of that structure and the data types of each field. Be sure to check that the input argument is a structure, and generate an error message if it is not.
In regard to society, where we have come from and where we are going? If any...
In regard to society, where we have come from and where we are going? If any person claims to be a member or participant of a society, that person must also observe and analyze what he or she is a part of, and hopefully aim to improve it. Please define (explain in detail) the qualities and characteristics needed for a successful society. Society is both a concept and a living, breathing, changing entity. Explain your subjective view of what makes...
48. Answer the following using your own words, do not copy from another source. A. Define...
48. Answer the following using your own words, do not copy from another source. A. Define the term enterotoxin and give an example of an organism (Genus and species name) that causes food poisoning and produces an enterotoxin. B. Define the term cytotoxin and give an example of an organism (Genus and species name) that causes food poisoning and produces a cytotoxin. 50. Choose ONE of the following questions to answer. You can write your answer on a separate sheet...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT