r/programmingquestions • • Jun 15 '23

Am I missing something?

I am writing code in C++ to fill a region to a certain color in an Image. For some unknown reason the color value that I give to memset doesn't give expected results. I gave the function the color, 0xff000000 and it sets the memory to 0x00000000. I have no idea why memset just gets rid of the alpha value. Any help will be appreciated:

void Image::FillQuadrant(Quadrant quad, u32 color)
{
const size_t bytes = sizeof(u32) * quad.Width;
const u32 yEnd = quad.y + quad.Height;
printf("Quadrant{ %hu, %hu, %hu, %hu }\n", quad.x, quad.y, quad.Width, quad.Height);
printf("Size: %lu bytes\n", bytes);
printf("Color: 0x%x\n", color);
u32* offset = &m_Data[quad.x];
for (u32 y = quad.y; y < yEnd; y++)
{
memset(offset + y * m_Width, color, bytes);
}
}

1 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jun 16 '23

No one here could have known that, given that you have not shared the Quadrant structure or Image class.

If passing by value is correct, then I don't see anything wrong with this piece of code at the moment. A broader context (like the rest of the code) is required to debug the issue.