Programming language: C
I have two DWORDs: A low and a high one. I want to convert them both into one variable of type size_t. I have the following code:
size_t fileSize = fileSizeHigh;
size_t *pfileSize = &fileSize;
pfileSize[4] = fileSizeLow;
is it right that way? I guess not since the first command probably stores the high-byte in the wrong position, right? Please help me on how to do this. Thank You :)
So, on a 32-bit Microsoft system, a
DWORD
is the same size as asize_t
. This means that you cannot fit twoDWORD
s into the space occupied by asize_t
.Your code will simply write into memory beyond the end of
fileSize
and has undefined behaviour. It will not do what you want.What is it you're actually trying to accomplish? What is the task you are trying to complete? Tell us that and we may be able to offer you an alternate solution.
Edit: As @nos explains, on a 64-bit system a
size_t
can hold twoDWORD
s. If you're using a 64-bit system, you can use bit shifting to combine the two:However, this is still a bit of a code smell, because operations on 64-bit Windows targets are meant to be very similar to those on 32-bit targets, so it still looks like you're doing something for an odd reason.