1

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?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Arif
  • 978
  • 12
  • 29

2 Answers2

2

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.

Community
  • 1
  • 1
Elemental
  • 7,365
  • 2
  • 28
  • 33
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.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005