Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

lpbitmapinfo

Status
Not open for further replies.

MarcoMB

Programmer
Oct 24, 2006
61
IT
can someone explain me the following line of code
LPBITMAPINFO lpbi;

Code:
lpbi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];
i don't understand the use of new BYTE to inizialize the LPBITMAINFO variable...i expected something like this...
Code:
lpbi =  new LPBITMAINFO[sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))];

i don't understand the use of BYTE to initialize lpbi...it's probably a pointer to BITMAPINFOHEADER struct, but why use BYTE as type?
 
LPBITMAPINFO is a pointer --> sizeof (LPBITMAPINFO) = 4 (bytes).
sizeof (BYTE ) = 1.

SO :

new LPBITMAPINFO[10] takes 40 bytes
new BYTE[10] takes 10 bytes.

Because the calculation of the size is in bytes, you will allocate 4 * the memory you actually need when using new LPBITMAPINFO[...]



Marcel
 
Natural expectation would be in no case what you wrote - your example would allocate array of pointers to BITMAPINFO. What you actually need is allocation of one BITMAPINFO structure and assigning a pointer to it to a pointer variable:

lpbi = new BITMAINFO;

But look at the definition of BITMAINFO:

typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;


It contains initial definition only for very first element of bmiColors[] array. If you want more - first your example is the only way to allocate it. What the statement actually does - it allocates amount of BYTEs suitable for bmiHeader and bmiColors[256] and casts it to pointer to BITMAPINFO.
 
By the way, the example will work correctly only if compiler is set to allocate memory aligned to DWORD boundary (because the first element of BITMAPINFOHEADER is of type DWORD). To be sure the more suitable example would be the following:

lpbi = (LPBITMAPINFO) new DWORD[(sizeof(BITMAPINFOHEADER) + (256 * sizeof(RGBQUAD))) / sizeof(DWORD) + 1];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top