3

While using some of the Microsoft safe versions of many of the standard C library functions I've noticed that some of these functions seem capable of determining at compile time if a passed in buffer is allocated statically or dynamically. If the input buffer is statically allocated the function can determine the size of it auto-magically but if dynamic the size must be given as another parameter.

For example this segment of code works when the buffer is statically allocated:

char buffer[1024];
sprintf_s(buffer, "Hello World\n");
printf_s(buffer);

However this one does not:

char *buffer = new char[1024];
sprintf_s(buffer, "Hello World\n");
printf_s(buffer);

I've tried looking at the function definitions for these but the code is mostly preprocessor defines that are very confusing to try to follow.

So my question is: how is this being determined and is this a standard C / C++ language feature or some kind of Microsoft specific feature?

Also some of these functions seem pointless like printf_s() has the exact same function definition as printf(), so why do they even have this?

If anyone can shine some light on this I'd appreciate it.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Matt
  • 113
  • 4
  • C++ version for C functions? – Iharob Al Asimi Jul 06 '15 at 21:06
  • @iharob - These functions are in stdio.h which is a C library and are specified with extern "C". – Matt Jul 06 '15 at 21:11
  • 1
    `printf_s()` invokes the [invalid parameter handler](https://msdn.microsoft.com/en-us/library/a9yf33zb.aspx) when the format string is invalid. `printf()` doesn't. – cremno Jul 06 '15 at 21:23
  • @iharob: Microsoft took the freedom to overload some of the CRT functions, and additionally provide template overloads. Both of which are C++ features. So yes, those are C++ versions of C functions. Anyone who has done even the least tiny bit of software development for Windows in this millennium would be familiar with those. – IInspectable Jul 06 '15 at 21:43
  • Well I have compiled my c code on windows, but never wrote any software for it. – Iharob Al Asimi Jul 06 '15 at 21:44
  • @iharob: So why do you feel the need to comment on Windows specific questions then? Not the first time that you're doing this, when you have **nothing** to add. – IInspectable Jul 06 '15 at 21:47
  • I was curious why C++ versions for C functions, but I was also a little bit confused too because I didn't understand what that meant. I am sorry, it's hard to hide how I hate Windows so much. But this comment had nothing to do with that, see that I didn't even mention Windows or Microsoft in my comment. – Iharob Al Asimi Jul 06 '15 at 21:49

3 Answers3

6

If you take a look at the reference documentation for sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l, you'll find the following declarations:

int sprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format [,
   argument] ... 
);

template <size_t size>
int sprintf_s(
   char (&buffer)[size],
   const char *format [,
   argument] ... 
); // C++ only

With the overload the compiler can determine the array size, when passed a statically sized array. This is standard C++, nothing Microsoft specific here (except for the overload and naming).


Additional information is available under Secure Template Overloads as well as Parameter Validation.
IInspectable
  • 46,945
  • 8
  • 85
  • 181
0

There is a Microsoft macro called _countof which will give the size of static arrays. This is probably being used in the implementation of the printf_s-related safe functions. According to this answer, there is a C++11 non-macro way to do this (get the size of a static array) as well.

Community
  • 1
  • 1
tsandy
  • 911
  • 4
  • 10
0

these functions seem capable of determining at compile time if a passed in buffer is allocated statically or dynamically

They're not.

This is a simple use case for the type system; in one case you're passing an array (and, remember, array types include the dimension!). In the other, you're passing a pointer.

That pointer could be to a piece of cheese allocated using stellar radiation, for all the compiler knows.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055