Thread: sizeof struct with variable length array?

  1. #1
    Registered User
    Join Date
    May 2014
    Posts
    121

    sizeof struct with variable length array?

    The WinAPI has a struct like this for raw input:
    Code:
    typedef struct tagRAWINPUT {  RAWINPUTHEADER header;
      union {
        RAWMOUSE    mouse;
        RAWKEYBOARD keyboard;
        RAWHID      hid;
      } data;
    } RAWINPUT, *PRAWINPUT, *LPRAWINPUT;]
    There's nothing special about that, but the RAWHID struct supposely has a variable length array:
    Code:
    typedef struct tagRAWHID {  DWORD dwSizeHid;
      DWORD dwCount;
      BYTE  bRawData[1];
    } RAWHID, *PRAWHID, *LPRAWHID;
    The definition of the struct doesn't show it but the documentation says that bRawData is variable length. sizeof(RAWINPUT) will not be the correct size when the data field is of RAWHID type so how do you allocate a variable with automatic storage type that has the right size for the entire struct? You can get a header that has the size for the entire struct but how do you actually allocate storage space for the data without using malloc? I've seen some example code that used a char array but that violates aliasing rules and there are also alignment issues with that approach.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what is the value of n for the size, and when do you know this size (compile time or run time)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2014
    Posts
    121
    Quote Originally Posted by Salem View Post
    So what is the value of n for the size, and when do you know this size (compile time or run time)
    You don't know the size until at run time. It actually varies depending on the data that GetRawInputData sends you. You can obtain a header from GetRawInputData that has the size of the entire data packet but I'm not sure how you're supposed to declare a variable with automatic storage size for that packet. As you maybe have noticed, bRawData isn't actually defined to be a variable size array for obvious reasons so a static code analyzer will complain if you read past its boundaries (it's defined to be one byte long in the header).

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    I'll give you a hint, it involves math starting with subtraction.

    Edit: The allocation however I only know of malloc so someone else will have to help you there.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there no documentation on how to do this, say with some interface provided? I suggest that you read this C FAQ on the struct hack if you have not already done so.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2014
    Posts
    121
    Quote Originally Posted by laserlight View Post
    Is there no documentation on how to do this, say with some interface provided? I suggest that you read this C FAQ on the struct hack if you have not already done so.
    I'm aware of the technique and it's exactly what the sample code I found on MSDN uses. I don't want to call malloc/realloc every time I get a WM_INPUT message though. Other code I've seen used a char array with automatic storage to hold the entire struct including the variable length array but that's even worse than malloc since you could end up with alignment issues and it also violates the aliasing rules. I guess I could use _malloca instead of malloc to allocate the buffer on the stack instead of on the heap but that introduces another point of of failure that I'd rather not have (especially since I have no good way of signaling errors that happen in the WNDPROC callback): Handling WNDPROC errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers in a function, sizeof doesn't get array length
    By ulillillia in forum C Programming
    Replies: 4
    Last Post: 01-27-2011, 11:36 AM
  2. variable length array
    By fatsrir in forum C Programming
    Replies: 18
    Last Post: 06-04-2010, 10:52 AM
  3. how do I find the length of and array using sizeof()
    By raymond1234 in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 02:38 AM
  4. Variable length array
    By 671 in forum C Programming
    Replies: 2
    Last Post: 09-13-2006, 01:05 PM
  5. sizeof() function on a pointer to an array of a struct
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 05-30-2004, 02:30 PM