CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    SetDIBits function

    Hello all,

    I have some code using the SetDIBits function:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    and I was wondering if I could pass an stl::vector of vectors to the the const VOID *lpvBits argument.

    Currently I am passing it a:

    Code:
    DWORD bits[100][100];
    ...
    ::SetDIBits(dc.m_hDC, bitmap, 0, dyextent, &bits, &bmi_,0);
    and this works. However when I try this:

    Code:
    vector<vector<DWORD> > bits = vector<vector<DWORD> >(100, vector<DWORD>(100)); 
    ...
    ::SetDIBits(dc.m_hDC, bitmap, 0, dyextent, &bits[0], &bmi_,0);
    This does not work. Anyone know what I am doing wrong?

    Thanks!
    Ellay K.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: SetDIBits function

    A std::vector is guaranteed to be contiguous but a vector of vectors is not laid out the same in memory as the array of arrays you used in the first example.

    You should be able to pass a vector of 100*100 elements though
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: SetDIBits function

    I'm just guessing, but I believe that when you declare a 2D array like this:
    Code:
    DWORD bits[100][100];
    You're guaranteed to get a contiguous block of memory (or an out of memory error). Where as, if you declare a 2D vector, you are not guaranteed to get one contiguous chunk of memory. IIRC, SetDIBits needs to have the memory contiguous.

    Viggy

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: SetDIBits function

    Quote Originally Posted by ekhule View Post
    Currently I am passing it a:

    Code:
    DWORD bits[100][100];
    ...
    Why is this a two-dimensional array? Why not just make it one dimension, as that is what that parameter describes:
    A pointer to the DIB color data, stored as an array of bytes
    The issue is that the data has to be contiguous. A vector of vectors breaks the contiguousness of the data, as each vector could be located in different memory "banks". When you declare a two-dimenisonal array, the entire memory block is contiguous. So using a 2-d array is counter-intuitive to what that parameter is supposed to denote, even though it works.

    So what fix do you want? Do you want to make the entire data one contiguous block? If you did that, then vector will work, but only if vector is 1-dimensional.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 19th, 2012 at 03:28 PM.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: SetDIBits function

    Are you really sure that the contiguous memory of a 2D array is guaranteed Paul?

    When I thought about this again I found a tiny fragment somewhere in the back of my head of having a encountered a bug caused by a 2D array being accessed as a 1D array.

    As I interpret this http://www.open-std.org/jtc1/sc22/WG...docs/n1256.pdf p6.7.5.2:6 it says that int[50][100] is not compatible with int[100][50]. Wouldn't they be that (to some extent) if the memory is contiguous?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: SetDIBits function

    Quote Originally Posted by S_M_A View Post
    Are you really sure that the contiguous memory of a 2D array is guaranteed Paul?
    From what I remember, the layout of a bona-fide 2D array (one that is not created dynamically) should be contiguous. Don't have the chapter and verse in the '89 'C' standard, but I'm sure C++ adopted the same requirement.

    However, your concerns do occur if you're creating a 2D array dynamically, and use this approach:
    Code:
    T **ptr = new T* [m];
    for ( int i = 0; i < m; ++i )
      ptr[i] = new T [n];
    This creates an m x n 2D array, but is not contiguous. Dynamic arrays created this way is where I usually see access violation issues, due to the programmer assuming the data is contiguous.

    This approach creates a contiguous m x n 2d array:
    Code:
    T **ptr = new T* [m];
    T* ptrMem = new T [m * n ]
    for ( int i = 0; i < m; ++i, ptrMem += n )
       ptr[i] = ptrMem;
    As I interpret this http://www.open-std.org/jtc1/sc22/WG...docs/n1256.pdf p6.7.5.2:6 it says that int[50][100] is not compatible with int[100][50]. Wouldn't they be that (to some extent) if the memory is contiguous?
    Well they're not compatible in that those are two different types. I guess compatibility in this case means that you cannot simply just (for lack of a better term) "rotate" an m x n array into an n x m array by doing 'C' style casts, even if the data is contiguous.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: SetDIBits function

    Yes my memory of the bug is probably not correct.

    I did some searching on the net and failed to find anything pointing against that a static allocation will be contiguous. Among all hits I instead found this http://cplusplus.com/doc/tutorial/arrays/ that clearly states that the memory is contiguous. IMHO this is a trustworthy source so I guess the case is closed.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Join Date
    Mar 2009
    Posts
    166

    Re: SetDIBits function

    Would it be sufficient to write code to convert the two vectors to one single vector to be passed in? Anyone have any example code to do that?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: SetDIBits function

    Quote Originally Posted by ekhule View Post
    Would it be sufficient to write code to convert the two vectors to one single vector to be passed in? Anyone have any example code to do that?
    How do you convert that two-dimensional array to one dimension? So please clarify what your definition of "convert" is.

    Regards,

    Paul Mckenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured