how use memset()?

how i use memset()?
heres my actual code:
1
2
3
4
if(Pixels==NULL)VirtualFree(Pixels, 0,MEM_RELEASE);;
            unsigned int PixelsCount =SizeWidth*SizeHeight*sizeof(unsigned int);
            Pixels = (unsigned int*) VirtualAlloc(0, PixelsCount,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memset(Pixels,NewColor, PixelsCount );

on these sample the 'NewColor' is 'RGB(0,255,0)'...
but why all arrays elements don't recive the same value?
Last edited on
> on these sample the 'NewColor' is 'RGB(0,255,0)'...
¿how do you represent that with only one byte?
man wrote:
void *memset(void *s, int c, size_t n);
The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.


also, keep in mind that the function will fill the array byte by byte (it seems that you are storing integers, which may be a little bigger than one byte)
Last edited on
Use std::fill (in <algorithm>) instead.

 
    std::fill(Pixels, Pixels + SizeWidth * SizeHeight, NewColor);

Is there a good reason you're using VirtualAlloc instead of new?
Last edited on
In the previous topic http://www.cplusplus.com/forum/general/283026/ Cambalinho did show intent to use malloc() and free() ... and had no destructor to clean up ...

We did offer std::vector too (which would handle memory management automatically). In vain.
1
2
3
4
if(Pixels==NULL)VirtualFree(Pixels, 0,MEM_RELEASE);;
            unsigned int PixelsCount =SizeWidth*SizeHeight*sizeof(unsigned int);
            Pixels = (unsigned int*) VirtualAlloc(0, PixelsCount,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
memset(Pixels,NewColor, PixelsCount );
Dude, you've been posting here for more than nine years. How is it possible you're writing code like this after using C++ for so long?
1st i'm sorry something.. nothing was in vain... i'm learning what i can for what i need.. .sorry something.

"Dude, you've been posting here for more than nine years. How is it possible you're writing code like this after using C++ for so long?"
i'm sorry, but what you mean?
memset is a crude tool.
but I think your issue is that it tells you the second parameter is an int. OK, it is, but its 0-255. You can't use it with an RGB.
like if you want a black image:
memset(pixels, 0, height*width*rgb_is_3_or_rgba_is_4_etc);
or a white image.. (pixels, 255,
or a sickly grey: (pixels, 128,
and so on. Most of the time with images those 3 constants are all you want.
all the mem*** family functions came from C and are a wee bit dangerous. Like serialization, they can't handle internal pointers in objects, and like anything C about arrays/pointers you can go out of bounds and all. I would avoid it unless you need the extra speed, they are fast, if nothing else.

As for your code, you are trying to deal with microsoftisms while also struggling with a lot of pointer basics. This is a recipe for disaster; if you are going down this road you need to go brush up on pointers before trying to use that pointer heavy, messy library.
Last edited on
i'm sorry, but what you mean?

*sigh*
the best shot was realy:
std::fill(Pixels, Pixels + SizeWidth * SizeHeight, BackColor);

thank you so much for all to all.
Topic archived. No new replies allowed.