In: Computer Science
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"; |
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)); |
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 |
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.