Difference between WIN32 and other c string

2020-04-21 04:54发布

问题:

I got this code inside a small program to read a file:

#ifdef WIN32
    unsigned char *buffer = (unsigned char *)alloca((unsigned int)ui.length);
#else
    unsigned char buffer[ui.length];
#endif

Why is a pointer used for Win32 platform and character array for other platforms?

回答1:

It seems previously to C99 defining a variable length array on the stack was not supported. alloca essentially does this. Seems this programmer had a WIN32 compiler that didn't support VLA's so was using (the well-supported but non-standard) alloca.

More on this in stack overflow: Why is the use of alloca() not considered good practice? and this rather useful array summary http://www.programmersheaven.com/2/Pointers-and-Arrays-page-2 mentioned by Arthur on the stack overflow post.



回答2:

There is nothing special in Windows. The difference is Microsoft Visual C++ does not support Variable-length array (VLA) (a C99 feature), and the author probably thinks MSVC == WIN32, thus that condition was created.