Header file:
The memset () function is used to set the first n bytes of the specified memory to a specific value, which is a prototype:
void * memset (void * ptr, int value, size_t num);
Parameter description:
A pointer to the memory to be manipulated by PTR.
Value is the values to set. You can either pass values of type int to value, or you can pass values of char types, int and char can be converted to each other based on ASCII code.
NUM is the first num byte of PTR, and size_t is the unsigned int.
The function Description memset () sets the value of the first num byte of the memory area that the PTR refers to, and then returns a pointer to PTR.
memset () You can set all the memory space to a specific value, so it is often used to initialize the character array. For example:
Char str[20];
memset (str, ' C ', sizeof (str)-1);
Return value returns a pointer to PTR.
Note: The parameter value is declared as int, but must be unsigned char, so the range is between 0 and 255.
Example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
// Can not be declared as char *str = "http://c.biancheng.net";
Char str[] = "http://c.biancheng.net";
memset (str, '-', 7);
Puts (str);
System ("pause");
return exit_success;
}
Execution results:
Optimization: Try to use memset to set an array to clear 0 (except with virtual classes) instead of 0 by the For loop
The following example, you can refer to: 3D Game Programming Master skills. This article is in fact the focus is memset, because in the work, with more.
For example: To empty a float f[10000], you should use Memset (f,0,sizeof (float) * 10000);
Instead: for (int i=0; i<10000; ++i) f[i] = 0;
Of course, it can also be in the form of inline assembly:
_asm
{
mov edi, F; Each time move 32 digits, all place 0
Rep stosd; Move Data
}
Well, this rule has a premise, that is, with the exception of the virtual class, because, Memset will class empty, it is possible to set the virtual table 0.
It is possible because: the creation of the class: on the stack and on the heap.
If on the stack, then the virtual function call of the stack object may be determined statically, bypassing the virtual table. So there's no mistake.
But there's going to be an error on the heap, and here's the test code:
class Cmemsetvirtualtest//test with virtual class, after being memset to 0, does the virtual table fail {public:cmemsetvirtualtest () {
memset (this,0,sizeof (cmemsetvirtualtest));
virtual void Normalfun () {cout<< "Test:normalfun () virtual table pointer valid" <<endl;
Virtual ~cmemsetvirtualtest () {cout<< "Test: ~cmemsetvirtualtest () virtual table pointer valid" <<endl;
}
};
void TestFun1 ()//test: The object built on the stack {cmemsetvirtualtest ctest;
Ctest.normalfun ();
void testFun2 ()//test: The object established on the heap {cmemsetvirtualtest* ptest = new Cmemsetvirtualtest (); Ptest->normalfun (); There's going to be a sudden drop in the delete ptest; If you block the sentence, you'll get out of here.} int Main () {cmemsetvirtualtest ctest;//Test: Object created on stack ctest.normalfun ();//Normal test: cmemsetvirtual test* ptest = new Cmemsetvirtualtest (); Test: The object built on the heap Ptest->normalfun (); There's going to be a sudden drop in the delete ptest; If you block the sentence, you'll get out of here.}
&NBSP
that is:
in C + +, a class involving virtual technology, his object memory block is not simply the data structure that the user defines in this class, the compiler will insert some data or code in it, The virtual technique used to implement the response. So when you use the Memset function, you'll flush out the compiler, and the program execution becomes unknown. At this point, if the Copy object C + + uses the memberwise copy, the compiler copies both the user-defined data structure and appropriate modifications to the associated facilities that support the virtual technology.
If the object is not using virtual technology, then you can use memset, just as normal, can be a bit-by-byte copy.