This statement is illegal:
char buffer[]; error C2133: 'buffer' : unknown size
That's a pretty convincing argument for me; How can you define an array without a known size?
This is what gets me: This is a legal statement:
char buffer[] = {0};
When ran in the debugger, it runs fine, and it has allocated 7 bytes for itself.
As you tell it to modify values, it allocates itself? 3 bytes (EG: buffer[8] = 'a';).
These both return a "size of 1":
sizeof(buffer); (sizeof(buffer) / sizeof(buffer[0]))
However, the strlen() reflects what you'd expect sizeof operator to do!
Initially, strlen() returns 7 (why 7...?), then if you exceed 7, it becomes 11!
If anybody could provide an explanation, it'd be appreciated!
As far as I know, this is considered 'undefined behavior,' in the C/C++ language.
This is the program I am compiling, if anybody would like to try it.
#include <windows.h>
#include <iostream> // For sprintf()
using namespace std;
int main() {
char buffer[] = {0}; // How is this a legal declaration?!
char message[50] = {0}; // For sprintf'ing the data out
// Insert 8 values (all 0x65)
for (unsigned char x = 0; x < 8; x++) {
buffer[x] = 'A';
}
// Returns a size of "1" and a strlen of "11" for me (MSVC 6.0)
sprintf(message, "SIZE OF: %d\nSTRLEN: %d", (sizeof(buffer) / sizeof(buffer[0])), strlen(buffer));
MessageBox(NULL, message, "", MB_OK);
return 0;
}












