memset question

Started by
24 comments, last by LessBread 18 years ago
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
----------------------------

http://djoubert.co.uk
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)
_____________________#define ever (;;)for ever delete (void *) rand();
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));
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++;
}
----------------------------

http://djoubert.co.uk
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
_____________________#define ever (;;)for ever delete (void *) rand();
Assuming C++:
#include <algorithm>...std::fill_n(array, size, color);
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;

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.
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).
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement