Handmade Network»Forums
Adrian McCarthy
8 posts
Clarifications for Ep 435 (Removing the CRT)
Edited by Adrian McCarthy on Reason: Initial post
1. The implementation for memset is subtly wrong. The return value should be the original destination pointer, not the end of the range. This likely won't matter, because the compiler still considers memset an intrinsic and will, in most cases, generate code for it directly inline. And even if it does decide to call the function, the likelihood of it using the return value is low. So if it ever does break, it would be a bear to debug.

Also note that you may eventually need to implement memcpy as well, as that's a common intrinsic for a compiler to depend on.

Putting the implementation in a separate file is good, because it means the compiler will use the intrinsic implementation as much as it can in your main code. If you had put `#pragma function(memeset)` at the top of your main code, then the compiler would always treat it like a regular function, which may or may not be inlined but won't benefit from any deep knowledge about the operation that the compiler might normally exploit.

2. Casey was confused about why the linker required both /ENTRY and /SUBSYSTEM:WINDOWS, claiming that WinMainCRTStartup is the startup function and that WINDOWS is the default subsystem. Those claims aren't exactly true.

When the compiler sees a "main" function, like `main`, `WinMain`, `wWinMain`, `_tmain`, etc., it recognizes that the user wants a hosted environment for a particular subsystem. In whatever translation unit it's building, it'll add annotations to the object file to tell the linker which entry point into the CRT to use and which subsystem the program is targeting, both of which are deduced from the type of main function. Use `main`, and the compiler will select the appropriate CRT entry point (which is _not_ `WinMainCRTStartup`) and it will flag the object file as being targeted for the console subsystem (rather than the Windows subsystem).

The moment Casey removed the `wWinMain` function, the compiler no longer saw any kind of "main" function, so it didn't pass any info along to the linker, so the linker required both the entry point and the subsystem.
Mārtiņš Možeiko
2562 posts / 2 projects
Clarifications for Ep 435 (Removing the CRT)
memset return value was fixed at end of the stream.
Adrian McCarthy
8 posts
Clarifications for Ep 435 (Removing the CRT)
Glad to hear it was fixed. I didn't have a chance to watch that episode all the way through.