A: 

Given the above two variants, the first one is vastly better, since it does not repeat the "magic constant" 255. If you wanted the second variant to be competitive with first, you have to do it as

const size_t BUFFER_SIZE = 255;
char buffer[BUFFER_SIZE];
StringCchPrintf(buf, BUFFER_SIZE*sizeof(char), TEXT("%s"), X);
AndreyT
Whether this is right depends on what the function wants. If it wants the amount of __bytes__, then it's right, if it wants the amount of __objects__, then it's wrong. But even in the latter case, as it is, the `...*sizeof(char)` is quite pointless. `sizeof(char)` by definition is always 1, so you could omit it for `char`, and if the type is ever going to change, `...*sizeof(char)` doesn't really help either, since it explicitly repeats the type. Why not `BUFFER_SIZE*sizeof(buffer[0])`? (And if that buffer isn't dynamically allocated, `sizeof(buffer)*sizeof(buffer[0])` works well, too.)
sbi
+3  A: 

Neither is correct.

You are using StringCchPrintf(), which operates on the count of characters, not bytes. sizeof(buffer) returns the size of buffer in bytes, as does 255*sizeof(char). 255*sizeof(char) also has the disadvantage that you are duplicating the size of the array in two places - if you change the size of buffer but forget in the call to StringCchPrintf, you have a bug.

This happens to work since sizeof(char) is always 1.

You are also specifying buffer as char, but use TEXT() around the string - compiling with UNICODE will cause a break.

Any of the following would be correct:

char buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), "%s", X);

TCHAR buffer[255];
StringCchPrintf(buffer, ARRAYSIZE(buffer), TEXT("%s"), X);

char buffer[255];
StringCbPrintf(buffer, sizeof(buffer), "%s", X);

TCHAR buffer[255];
StringCbPrintf(buffer, sizeof(buffer), TEXT("%s"), X);
Michael
`sizeof(char)` always return 1 in C, regardless of the platform (and even if the actual size is less, like a nibble).
zneak
I should also note that `sizeof(char)` returns 1 even in cases where `char` is more than 8 bits.
zneak
The last one "sizeof(buffer)" for the TCHAR array is wrong!Either use _countof() instead of _sizeof(), or use sizeof(buffer)/sizeof(TCHAR).
Stefan
The last one is StringCbPrintf. The size parameter is count in bytes.
Michael
A: 

sizeof(buffer) will work for a statically allocated array but not for a dynamically allocated array:

char buffer[255];
cout << sizeof(buffer) << endl;   // prints 255
char *p = new char[255];
cout << sizeof(p) << endl;        // prints 8 (on a 64-bit machine)
delete[] p;
return 0;

With this in mind I would recommend always using N * sizeof(type) for the sake of consistency and to avoid subtle bugs.

Alex - Aotea Studios
A: 

You should use constants for the size, not integers like you did.

Per Microsoft, the correct form to calculate what you want is this:

sizeof array / sizeof array[0]

http://msdn.microsoft.com/en-us/library/4s7x1k91%28VS.71%29.aspx

Also, sizeof isn't perfect because in some instances it will return the size of the pointer and not the size of the array. The term SIZE OF is a little misleading in this case, because you have to ask yourself - what am I actually getting the SIZE OF?

0A0D
Somewhere buried in a Windows header, there is a preprocessor macro called `_countof` which basically expands to the above.
dreamlax