Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

((A)) The following program, written in C, writes data all over the data section

ID: 3736034 • Letter: #

Question

((A)) The following program, written in C, writes data all over the data section, starting from the local variable i. Compile and run the program, then answer the questions. Note that the results vary with different compilers and operating systems, so specify the systems you used. Note the behavior of the while loop. The first statement in the loop (*j=0;) writes a zero to the RAM address stored in variable j. The second statement (j=j-1;) moves the address downward in RAM each time the loop executes. Each step of j moves by a “short integer,” which is 2 bytes (16 bits) long. main() { short i, *j; j = &i; while(1) { *j = 0; j = j-1; } }  Does the program behave differently if we add 1 instead of subtracting 1 in the second statement? Try it while storing zeros and find out. Describe what happens. ((B))  Write a program that reads text into an input buffer that is 10 bytes long. Run the program and type in 20 bytes. What happens? Do you get a different result if you use a different language, for example, C versus Java?

Explanation / Answer

main()  

{  

short i, *j;  

j = &i;

  

while(1)  

{

*j = 0;

j = j-1;  

}

}

if we add 1 then the address will be incremented bby 2 places

if we run till 20 bytes on a modern com plier access violation ocurs

on leagay compilers we get randonm data

if we impliment it in java then the pointers are not valid as java dosent use pointers

shoot any questios you have ?