Can anybody explain to be what memset does?
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~
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~
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).
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).
hmmm... what about polymorphic classes (with virtual functions and all)? Using memset on them would probably be bad right?
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
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
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement