Learn more about the use of memset of function in C language

  • 2020-04-02 03:16:41
  • OfStack

The header file:


#include <string.h>

The memset() function is used to set the first n bytes of specified memory to a specific value. The prototype is:
   


 void * memset( void * ptr, int value, size_t num );

Parameter description:
PTR is a pointer to the memory to be manipulated.
Value is the value to set. You can pass both int and char values to value, and int and char can be converted to each other based on ASCII code.
Num is the first num byte of PTR, size_t is unsigned int.

Memset () sets the value of the first num bytes of the memory region referred to by PTR to value, and then returns a pointer to PTR.

Memset () can set an entire chunk of memory to a specific value, so it is often used to initialize an array of characters. Such as:


char str[20];
memset(str, '0', sizeof(str)-1);

[return value] returns a pointer to PTR.

Note: the parameter value, though declared as int, must be an unsigned char, so it ranges from 0 to 255.

Example:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
 //Cannot be declared as char * STR = "http: arbitration c.biancheng.net";
 char str[] = "http://c.biancheng.net";
 memset(str, '-', 7);
 puts(str);
 system("pause");
 return EXIT_SUCCESS;
}

Execution results:


-------c.biancheng.net

Optimization: try to use memset to set an array to zero (except for virtual classes) instead of going through a for loop
   
      Here's an example: 3D game programming master tips. In fact, the focus of this article is memset, because in the work, used more.
 
      For example, to empty a float f[10000], use memset(f,0,sizeof(float) * 10000);
      For (int I =0; i. < 10000; + + I) [I] = 0 f;
      Of course, you can also use inline assembly:
     


 _asm
  {
  mov edi, f;  //Edi points to the beginning of the target memory in the array
  mov ecx, 1000/4; //Number of cycles or moves
  mov eax, 0;  //Every time I move 32 digits, I put a 0
  rep stosd;  //Mobile data
  } 

 
      Ha, this criterion has a premise, that is to take virtual class exception, the reason is that memset empty the class, may also put virtual table 0.
Possible because: class creation: stack and heap.
    If on the stack, the virtual function call to the stack object might be determined statically, bypassing the virtual table. So you can't go wrong.
    However, there must be an error on the heap. Here is the test code:


class CMemsetVirtualTest //Test with virtual class, memset as 0, virtual table is invalid
{
public:
 CMemsetVirtualTest()
 {
  memset(this,0,sizeof(CMemsetVirtualTest));
 }
 
 virtual void NormalFun()
 {
  cout<<"test :  NormalFun()  Virtual table pointer is valid "<<endl;
 }
 virtual ~CMemsetVirtualTest()
 {
 cout<<"test :  ~CMemsetVirtualTest()  Virtual table pointer is valid "<<endl;
 }
}; 
void testFun1() //Tests: objects created on the stack
{
 CMemsetVirtualTest Ctest;
 Ctest.NormalFun();
}
void testFun2() //Tests: objects built on the heap
{
 CMemsetVirtualTest* Ptest = new CMemsetVirtualTest();
 Ptest->NormalFun(); //It's going to explode here
 delete Ptest;  //If the last sentence is blocked, it will also burst here
}
 
int main()
{
 CMemsetVirtualTest Ctest;//Tests: objects created on the stack
 Ctest.NormalFun();//Normal test:
 
 CMemsetVirtualTest* Ptest = new CMemsetVirtualTest(); //Tests: objects built on the heap
 Ptest->NormalFun(); //It's going to explode here
 delete Ptest;  //If the last sentence is blocked, it will also burst here
}

 
    That is:
    In c + +, involving the class of virtual technology, his object in the memory block is not only the user to define the data structure of this class looks, the compiler will in place of some data or code, used to implement the response of the virtual technology. And when you use memset function put these compilers plant hedge, program execution results become unknown. If use c + + object will copy memberwise copy, the compiler is copy user defined data structure, also for support of virtual technology related facilities for the proper modification.
    If the object does not use virtual techniques, you can use memset and, as normal, copy bit by bit.


Related articles: