Revise the snapshot of the stack, when the following code is executed and run at
ID: 669440 • Letter: R
Question
Revise the snapshot of the stack, when the following code is executed and run at the execution point reaches the beginning of line# 3 in func2().
void func2(char * str2) {
char buf2[16];
strcpy(buf2,str2); /* line #3 in func2() */
}
void func1(char * str1) {
char buf1[256];
strcpy(buf1,str1);
func2(buf1);
}
void main() {
char large_str[256];
int i;
for( i = 0; i < 255; i++)
large_str[i] = 'A';
func1(large_str);
printf(“str copied! ”);
}
Please Explain Answer!
Explanation / Answer
char *strcpy(char *dest, const char *src) is a library function in c. it copies the source string into destination string.
String copy should be handled carefully. Otherwise, errors like buffer overflow, null termination errors may occur, while copying the source to the destination.
Buffer overflow:
For example, while writing the string to a buffer, it may overrun the boundary or overwrites in memory adjacent locations.
In the given program also, line 2 (i.e, char buf2[16]) buf2 is defined with fixed size 16. In the line 3 of function2, the code is tried to copy the large string into small buffer. This raises a run time error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.