3 ms·
I don't understand why is it illegal C? memcpy is just another function in some library. It might be libc or not. I don't expect compiler to care about it, just
by vbezhenar 6y ago
I don't understand why is it illegal C? memcpy is just another function in some library. It might be libc or not. I don't expect compiler to care about it, just put function call into object code and move on.
Warning might be useful.
- stephanimal 6y agoBecause it is a function in the C Standard Library. Programs are typically prohibited from declaring these functions. You can if you don't link with libc and pass in the correct compiler flags (gcc will turn some raw loops into a memcpy call as an optimization even without linking libc afaik) https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html https://www.gnu.org/software/libc/manual/html_node/Reserved-...
- HelloNurse 6y agoCompilation units are required to declare all standard library functions they call. They usually do so by including the compiler's header files rather than doing more work and risk errors with explicit declarations.
- jcranmer 6y agoIt's undefined behavior to call functions with a different set of arguments from its definition. The definition of memcpy is memcpy(void * , void * , size_t), and calling it with no arguments is obviously incompatible with the three required arguments here. > I don't expect compiler to care about it, just put function call into object code and move on. memcpy actually has a few semantics that cannot be legally expressed in C (specifically, strict aliasing doesn't kick in). All modern compilers actually define memcpy as a compiler-builtin, and they rely on memcpy's semantics for optimization purposes. Or, put more bluntly, memcpy does not correspond to a function call in object code. C is not a thin wrapper around assembly code, and has not been so for quite some time.
- HelloNurse 6y agoWell, no, in this test program memcpy is declared as having no arguments and it has to be called with no arguments. It's perfectly consistent, unless memcpy is so magical that some specific undefined behaviour is allowed (not likely). The actual test comes later: the real memcpy with three arguments, if it exists, is mislinked into the test executable; if memcpy cannot be found linking fails. The test program would hopefully crash badly in case it is actually run, but the test is not about memcpy behaviour or about what the parameters of memcpy are.
- Rusky 6y ago> It's perfectly consistent, unless memcpy is so magical that some specific undefined behaviour is allowed (not likely). memcpy is so magical that some specific undefined behavior is allowed. It's more than a mere library function- it's closer to a compiler intrinsic.
- cygx 6y agoAs far as the standard goes, it's just a name you're not allowed to define yourself with external linkage.
- acqq 6y agoI agree. If the compiler authors make some additional "magic", they are also responsible to "unmagic" the code which checks: "if memcpy is defined as having no arguments, and then a call to it is compiled (the call is never executed), will the memcpy routine from the standard library (which must be linkable only using the name) be successfully linked?"
- jcranmer 6y agoIt's not the declaration of memcpy that matters, it's the definition. And the definition of memcpy is given by the C standard. In the case of external functions, it's the user's responsibility to ensure that the declaration is compatible with the definition. If you want to use a non-standard function that coincidentally happens to be memcpy, you have to use special compiler flags to inform the compiler that C library semantics are not in effect (e.g., -ffreestanding). Of course, the entire point of these checks is to figure out if the C library has these functions in the first place, so using these options during the tests would defeat the purpose. > The actual test comes later: the real memcpy with three arguments, if it exists, is mislinked into the test executable; if memcpy cannot be found linking fails. The actual test both compiles and links the executable, although it (obviously) doesn't attempt to run it. Whether it fails in the compile or the link step is immaterial to the test, as it fails either way.
- jcelerier 6y ago> memcpy is just another function in some library. It might be libc or not. I don't expect compiler to care about it, just put function call into object code and move on. The compiler has special knowledge of memcpy and may introduce calls to it even if you don't call it anywhere in your code - see this : https://gcc.godbolt.org/z/_w78qd https://gcc.godbolt.org/z/_w78qd
- cygx 6y agoHence standard library function names are "reserved for use as identifiers with external linkage", ie unless your implementation is 'freestanding', you're not allowed to re-define them. The code above does not do so. Contrary to what other people have suggested, according to my understanding of the C language standard, the word `memcpy` is not magical in any way, and undefined behaviour only comes in because the code will call `memcpy` with incorrect signature.
- kyboren 6y agoAnother reason this asinine: Which platform has a C compiler but doesn't have memcpy()?! I guess this is not an argument that the above code to check for memcpy() is illegal, per se, but it's at the very least totally superfluous.