Win32 API functions vs. their CRT counterparts (e.g. CopyMemory vs. memcpy)

后端 未结 2 741
走了就别回头了
走了就别回头了 2021-01-05 00:46

In writing Win32 C/C++ code, is there any advantage (e.g. performance?) in using Windows-specific functions like lstrcpyn or CopyMemory instead of

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 01:42

    At least some CRT functions use the Win32 functions internally. Also the CRT requires additional initialization (e.g. thread specific data for functions like strtok) and cleanup, that you might not want to have to happen.

    You could create a plain Win32 application, without any dependency on anything else including the CRT (much like you could create a plain NT application using NTDLL.DLL - I think smss.exe of Windows is such a process BTW).

    Having that said, I think that for most applications that doesn't matter.

    UPDATE Since people seem to get so hooked up on the difference of individual functions, in particular memcpy vs. CopyMemory, I would like to add that not all functions in CRT are wrappers around those in Win32. Naturally, some can be implemented without any help from Win32 (actually memcpy is a good example for that), while others (sensibly) can't. Something that, I believe, @Merdad hinted in his answer to.

    So, portability aside, I don't think performance is the next best argument for or against using the CRT. You should choose what fits best and that typically will be the CRT. And there is nothing speaking against using individual Win32 functions (with CRT equivalents), where you seem fit.

提交回复
热议问题