2.72 Copy Int
★★
Problem:
You are given the task of writing a function that will copy an integer val
into a buffer buf
, but it should do so only if enough space is available in the buffer.
Here is the code you write:
This code makes use of the library function memcpy
. Although its use is a bit artificial here, where we simply want to copy an int
, it illustrates an approach commonly used to copy larger data structures.
You carefully test the code and discover that it always copies the value to the buffer. even when maxbytes
is too small.
A. Explain why the conditional test in the code always succeeds. Hint: The sizeof
operator returns a value of type size_t
.
The result of sizeof(val)
is usnigned, so the value of maxbytes-sizeof(val)
is also unsigned, the unsigned is always greater than 0, so the conditional test maxbytes-sizeof(val) >= 0
is always true.
B. Show how you can rewrite the conditional test to make it work properly.
Code:
Last updated