memset question

Started by
24 comments, last by LessBread 18 years ago
Quote:Original post by Colin Jeanne
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));


In fact, memset (and memcpy BTW) will set n bytes at a time (where n is 4 on 32 bit machine). memset is a rather clever routine that will write memory as fast as it can (you won't be able to beat it; Adam Hamilton's version is the core of memset, but memset first align variable and try to pair asm instructions in order to achieve the best speed). Only the lowest byte of the memset argument is used (the other bytes are discarded).

You can use std::fill or std::fill_n to copy more than one byte to a destination variable.

(edit:)

Quote:Original post by AP
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.


AFAIK, floating point 0 is 0 (IEEE standard), and NULL pointer is 0 too (of course, if you don't like your co-workers, you can define NULL to be 42; the NULL value is implementation defined, but is supposed to be the null pointer constant (which is defined as an integral constant expression rvalue of integer type that evaluates to zero (4.10 of the holy C++ standard))). Did I miss something?

HTH,
Advertisement
Quote:Original post by bakery2k1
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).


Sorry... but I am going to have to slam you down on this one. If you knew anything about assembler you would know that stosb, stosw, etc.. uses ES:DI or ES:EDI
Yes, but in Windows the base of es is the same as that of ds (they're both zero). That's the meaning of a "flat memory model", as stated in my post.

If _you_ knew "anything about assembler", you'd know this. You'd also know that your "les" instruction is loading the es register with a random selector (whichever Word follows "buffer" in memory), which is likely to cause a GPF and hence crash your program.

Consider _yourself_ "slammed down".
I am sorry... I guess I am slammed down. I thought a flat memory model still doesn't guarantee that ES and DS are the same.


BTW. Thanks a bunch for the neg comment... Maybe I should take back that sorry... I would only neg comment someone if they were consistantly unhelpful.
Under a flat memory model you shouldn't have to fuss with segment registers at all - except for fs with regards to exception handling - and even then it's best to let the compiler work that for you.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by bakery2k1
If _you_ knew "anything about assembler", you'd know this. You'd also know that your "les" instruction is loading the es register with a random selector (whichever Word follows "buffer" in memory), which is likely to cause a GPF and hence crash your program.

Consider _yourself_ "slammed down".


I'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.

And no... I didn't know that les would load with a random selector - I have used this instruction before with windows with no GPF and my program worked exactly the way I wanted it to.

Once again, I am sorry for my previous statement.

Did you not expect me to apologise???
Quote:Original post by Adam Hamilton
I'm sure this was an edit cause some of it wasn't there when you first posted... Seems kinda childish to go back an edit a post and add insult to injury after I had apoligised.


*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1� *cough*
Quote:Original post by Anonymous Poster
*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1?? *cough*


err... um...

*cough* http://www.gamedev.net/community/forums/topic.asp?topic_id=383613&whichpage=1&#2534085 *cough*
I get what your saying AP (I had changed a post - forgot to put EDIT:) but...

If you had continued reading through the thread, I mentioned in my next post I had edited the original post because I thought I sounded a bit mean... (I Didn't proof read before I posted)

Bakery2k1 on the other hand basically read my apology and decided to edit his post and make it sound much more nasty

Who is the childish one?

Someone who realises they have maybe sounded rash, edited their post sounding not mean and owned up to editing their post

or

Someone who posts something slamming them (which was warranted) reading an apology and then edits their post filling it with nastyness.

Come on... I think the latter is more childish.

This topic is closed to new replies.

Advertisement