Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hi,
- We should change the question number 2 from the tests to this:
- ----begin----
- What is the output when the following code fragment is executed? Also, please identify any "potential" problems. Hint: printf function call
- main() {
- char smallNumber=123;
- char *p=new char[100];
- memset(p,0,100);
- strcpy(p,"Text containing 24 bytes");
- printf("%d %d %d",sizeof(*p),sizeof(p),smallNumber);
- }
- ----end----
- The answer would be like this:
- Output:
- 32 bits machine: 1 4 123
- 64 bits machine: 1 8 123
- Potential problem:
- 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:
- printf("%"PRIz"u %"PRIz"u %"PRId8,sizeof(*p),sizeof(p),smallNumber);
- 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.
- Overall assessment:
- Single answer: 1 4 123 - between beginner and moderate experience
- Full answer (both 32 and 64 bits) - above moderate experience
- Identifying the problem: - very high chances this could be a real experienced guy
Add Comment
Please, Sign In to add comment