|
-
March 19th, 2012, 03:06 PM
#1
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.
-
March 19th, 2012, 03:16 PM
#2
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
-
March 19th, 2012, 03:17 PM
#3
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
-
March 19th, 2012, 03:17 PM
#4
Re: SetDIBits function
 Originally Posted by ekhule
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.
-
March 19th, 2012, 03:42 PM
#5
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?
-
March 19th, 2012, 04:53 PM
#6
Re: SetDIBits function
 Originally Posted by S_M_A
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;
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
-
March 20th, 2012, 01:25 AM
#7
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.
-
March 20th, 2012, 06:38 AM
#8
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?
-
March 20th, 2012, 09:48 AM
#9
Re: SetDIBits function
 Originally Posted by ekhule
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|