Jump to content
  • Advertisement
Sign in to follow this  
Si0n

Can anybody explain to be what memset does?

This topic is 4930 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

Advertisement
According to my brower that page doesnt even work. (but my ISP has been buggy lately, so it could just be me)

EDIT: Ok.. it was just my buggy ISP, it works fine.



As for memset, from what i know it basically sets information in the memory (where all your variables are stored).

Let's say you have the character array:
char test[200];
And we want to set every single character in that array to "-" all you have to do is call
memset(test, "-", 200);
That will take the variable "test", replace it with the character "-" for 200 bytes in memory.

note: Make sure it doesnt set it more than the actual size of the array.. that could cause memory leaks/failures.

Hope that helped
~zix~

Share this post


Link to post
Share on other sites
It depends entirely on the implementation of memset and the loop you're comparing it with [razz]. Though memset will have to use some form of loop to clear the memory.

memset itself just fills a certain area of memory with a single value, in C you'd use it to do things like zero out a structure. However in C++ this is not such a good idea. Classes (and structures are just classes by a different name) won't always just contain their data, zeroing out the memory occupied by a class would give you an undefined behaviour (i.e. you don't know what it's going to, most likely it'll cause a crash of some sort).

Share this post


Link to post
Share on other sites
hmmm... what about polymorphic classes (with virtual functions and all)? Using memset on them would probably be bad right?

Share this post


Link to post
Share on other sites
memset is a function that sets all bytes in an ammount of memory to a specified value starting at a specified position. All bytes in the specified memory should be contiguous.

The function uses an assemlbly call to do that work. It is done in a sigle pass so it will be faster than a for.


Luck!
Guimo

Share this post


Link to post
Share on other sites
And yes, it can be used in clases:

class VECTOR
{ public:
float x, y, z;
}

...

VECTOR v;
memset(&v, sizeof(vector), 0);

//is that call right... dont remember

memset dont care where it writes... it only does the job.

Luck!
Guimo


Share this post


Link to post
Share on other sites
cool

by teh way I see a lot of people using a double (0.0) for tehseonc argument.
It wouldnt' matter right? it would just take it as a 0 correct?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!