Jump to content
  • Advertisement
Sign in to follow this  
dawidjoubert

memset question

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does memset cpy 8bits at a time.. Is there a memset equivalent for an unsigned int (32bit) This is what i am doing memset(start,color,(x2-x1) << 2); obviously it is not working because it is copying over bytes not longwords

Share this post


Link to post
Share on other sites
Advertisement
void *memset(void *s, int c, size_t n);

int == 32-bits on just about all most modern systems (not sure about 64-bit systems)

Share this post


Link to post
Share on other sites
memset() will set one byte at a time. If yor datatype is more than a byte long then you can use sizeof().


int sum_ints[25];

/* Set every byte in this array of ints to 0 */
memset(sum_ints, 0, 25 * sizeof(int));

Share this post


Link to post
Share on other sites
Thanks but memset won't work because i am blitting a colour, that is 4 different bytes and then 4 different bytes again.

But my solution, behold:
int x = x1;
while (x < x2)
{
(*start) = color;
start++;
x++;
}

Share this post


Link to post
Share on other sites
not to nitpick but

int x;
for (x = x1; x < x2; ++x)
{
start[x] = color;
}



looks alot cleaner

in C/C++ pointer notation and array notation are pretty much interchangable most of the time, but at times its just cleaner to use array notation. Then again we also have times were using pointer notation makes things easier and cleaner, but now isnt one of thoughs times ;)

<3 Thetan

Share this post


Link to post
Share on other sites
you could try using inline assembly

You must use this with multiple of 4 bytes. There are tricks so that you can do this with any number of bytes.

This will be nice and fast.

push es
push edi

les edi, buffer
mov eax, color
mov ecx, count
rep stosd

pop edi
pop es


Or you could do this :)

You can use pointers as the counting variable in for loops.


for (int* s=&start[x1]; s<&start[x2]; s++)
*s = color;


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Thetan
void *memset(void *s, int c, size_t n);

int == 32-bits on just about all most modern systems (not sure about 64-bit systems)


Quote:
Final draft of C99
The memset function copies the value of c (converted to an unsigned char) into
each of the first n characters of the object pointed to by s.


memset() copies ((unsigned char)c) into each byte of s. int may be 32 bits on almost all modern systems, but memset() uses only 8 bits of that on almost all modern systems.

Really, memset() and calloc() aren't very useful in a strictly conforming program where you can't assume "all bits 0" is a valid representation of floating point 0 and/or a null pointer. For similar reasons, the most common value passed as the second parameter to memset() is 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
push es
push edi

les edi, buffer
mov eax, color
mov ecx, count
rep stosd

pop edi
pop es


There's no need to fiddle with es in a flat memory model (as used for Windows).

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
You can use pointers as the counting variable in for loops.


for (int* s=&start[x1]; s<&start[x2]; s++)
*s = color;

Yes, this is what I would do too. It usually saves a register or two, allowing for better optimisation as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!