During the interview today, the interviewer asked a question about the implementation of the memcpy function prototype. The question was very simple, but I don't know how I was wrong at the time, I am so tired. I feel like this job has been ruined. If something happens, I will miss it if I miss it. in this way, we can record this matter and remind ourselves of it ~
This problem is naturally not difficult for friends who have been familiar with it. The problem is to give yourself an analysis method and how to solve similar problems.
Memcpy implements memory copy. Based on this problem, we can extract the following points:
1. Any data can be copied, and the data type cannot be limited.
2. The source data cannot be changed.
Through the above two points, we can determine that the function prototype is void * memcpy (void * dest, const void * src). Is this enough? When will the function copy end? At that time, I used this function prototype. Because it is copying any data, it cannot be specified with a clear ending mark, in this case, only the copy size can be explicitly specified. so the function prototype becomes void * memcpy (void * dest, void * src, size_t count); well, since the function prototype has been confirmed, the rest is to write the function, wait a moment. Do not rush to write functions. In fact, for C language developers, it is not the implementation of function functions, but the processing of function errors, if you use Java or C # To throw an exception, a software crash will not affect others. C will paralyze the entire system if it is not good, the so-called "not moving, grain and grass first", I should first consider the problem of errors! Based on the function prototype,
Void * memcpy (void * dest, const void * src, size_t count );
1. The NULL pointer problem. If dest and src are both NULL or one of them is NULL, it will not be enough;
2. The copy size count is less than or equal to 0, which is naturally incorrect;
3. Does the target have enough size to accommodate the source data? This cannot be guaranteed within the function, but we have to think of it ourselves.
4. Does the memory address overlap? We will not consider this for the moment.
With the above tips, it is easier to write.
# Include <stdio. h>
Void * memcpy (void * dest, const void * src, size_t count)
{
If (NULL = dest | NULL = src | count <= 0)
Return NULL;
While (count --)
* Dest ++ = * src ++;
Return dest;
}
The above code is correct when using gcc for compiling in Linux, but there is a warning, so it is changed to this:
# Include <stdio. h>
Void * memcpy (void * dest, const void * src, size_t count)
{
If (NULL = dest | NULL = src | count <= 0)
Return NULL;
While (count --)
* (Char *) dest ++ = * (char *) src ++;
Return dest;
}
Okay, that's it. If the interviewer asks a memory overlap question, you can joke with him again.
My interview is ruined.
Conclusion: Don't take it easy. Launch a prototype based on your needs and deduce the problem based on the prototype. This is a lesson !!!
Supplement
Here, I am very grateful to the bloggers for seeking for the blind. This kind of netizen pointed out two errors in my program.
1.
2. size_t
So
Expect attention from others
Void * memcpy (void * dest, const void * src, int count)
{
Void * ptr = dest;
If (NULL = dest | NULL = src | count <= 0)
Return NULL;
While (count --)
* (Char *) dest ++ = * (char *) src ++;
Return ptr;
}