Sign in to follow this  
Si0n

Can anybody explain to be what memset does?

Recommended Posts

zix99    205
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
Monder    993
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
Si0n    136
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
Guimo    463
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
Guimo    463
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
Si0n    136
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
Ra    1062
memset accepts an int as its second argument, not a double, so the double would have to be converted at compile time and wouldn't give you any advantage.

Share this post


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


Yes you can use it with classes, but that doesn't mean it's a good idea to, you're relying on the fact that the class contains nothing but your data. If you accidentaly overwrite some internal field that you're not aware of you can screw things up.

Share this post


Link to post
Share on other sites
izhbq412    204
Quote:
Original post by zix99
memset(test, "-", 200);
That will take the variable "test", replace it with the character "-" for 200 bytes in memory.

Not really, "-" is expanded to '-''\0', so this wouldn't work. You should use memset( test, '-', 200 ) instead.

It can be used with classes, even with classes that have member functions ( functions and data members are located at different places in memory and all the instances of a class use only one copy of the functions ). It's not a good idea because it allows you to write over private data, which shouldn't be accessable that way ;)

You can use it in classes (in their member functions) just as if you were using it outside a class.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this