Hey everybody,
I am new to this forum as I am new to Handmade Hero. Just watching the Intro to C series and stumbled across something I even could remember from they days back when I was still coding in C(++). Nowadays I am more used to .NET (guess that's changing somehow atm... lol).
So what am I talking about?
First things first: If there was anyone already talking about SAL in this forum, please skip this post. I searched throughout the forum and didn't find anything in regards to Microsofts idea of a Sourcecode Annotation Language (SAL). So I decided to drop a little post about it.
Right at the beginning of the Intro to C series (as well as the beginning of the Main series), Casey copies a WinMain entry function right from MSDN and then deletes the _In_ macros. When I remember correctly, he doesn't go into details about what these macros really are (and are good for).
Basically it's about making code more "readable". Well, of course you could argue about whether or not annotating your code with somewhat obscure macros makes it more readable or not. However, here is a little example taken directly from MSDN:
| void * memcpy(
void *dest,
const void *src,
size_t count
);
|
To make is reasonable short: Since passing pointers could result in something happening to the variable only within the function as well as it could modify the original value (making it basically a return value), you couldn't really say what this function really does (only from reading this header).
This is where SAL drops in:
| void * memcpy(
_Out_writes_bytes_all_(count) void *dest,
_In_reads_bytes_(count) const void *src,
size_t count
);
|
In this example, you can better tell what this function really does. Also, Microsofts compiler does "understand" this annotation language as well. That basically means that typical mistakes could be found at the compile time if SAL is used correctly.
So _In_ does really mean: "Data is passed to the called function, and is treated as read-only."
The fun part then is to use the Visual Studio Code Analysis Tool to possibly find code defects.
More on this can be found here:
https://msdn.microsoft.com/en-us/library/ms182032.aspx
Again: If this has been discussed elsewhere, just ignore this post. If the common perception of followers of this Tutorial series is to avoid proprietary techniques such as SAL, this is perfectly alright. I myself have no real experience in using SAL. :) However, it is a means of helping developers write quality code.