Guest User

Untitled

a guest
Apr 24th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. Hi,
  2.  
  3. We should change the question number 2 from the tests to this:
  4.  
  5. ----begin----
  6. What is the output when the following code fragment is executed? Also, please identify any "potential" problems. Hint: printf function call
  7.  
  8. main() {
  9. char smallNumber=123;
  10. char *p=new char[100];
  11. memset(p,0,100);
  12. strcpy(p,"Text containing 24 bytes");
  13. printf("%d %d %d",sizeof(*p),sizeof(p),smallNumber);
  14. }
  15. ----end----
  16.  
  17. The answer would be like this:
  18.  
  19. Output:
  20. 32 bits machine: 1 4 123
  21. 64 bits machine: 1 8 123
  22. Potential problem:
  23. The format specifiers are incorrect. Last %d from the printf call will try to access a 32 bit value while the supplied value (smallNumber) is an 8 bit number. Depending on the compiler implementation, this could generate a buffer overrun. The correct way of re-writing that is like this:
  24. printf("%"PRIz"u %"PRIz"u %"PRId8,sizeof(*p),sizeof(p),smallNumber);
  25.  
  26. While this is a very simple question, the answer to this one could provide us with great insights about the subject's level of experience. I don't expect everybody to identify the issue, but the output should be like that.
  27.  
  28. Overall assessment:
  29.  
  30. Single answer: 1 4 123 - between beginner and moderate experience
  31. Full answer (both 32 and 64 bits) - above moderate experience
  32. Identifying the problem: - very high chances this could be a real experienced guy
Add Comment
Please, Sign In to add comment