There are some problems with strings in C++ (strlen, sizeof, strcpy, etc.)

The difference between strlen and sizeof

  • sizeof is an operator, not a function, and the result is inwhen compilingGet; parameters can be any data type or data;
  • strlen is a library function for character processing; parameters can only be == character pointers and strings ending with '\0'

The memory size occupied by a pointer is related to the compilation environment, but has nothing to do with the number of machine bits.

The difference between string and char

string inherits from basic_string, which actually encapsulates char, including char array, capacity, length and other attributes. The string can be dynamically expanded, and each time it is expanded, another space twice the size of the original space (2*n) is applied, and then the original string is copied and the new content is added.

Relationship conversion between string and const char*, char*

a)  string转const char* 

string s = “abc”; 

const char* c_s = s.c_str(); 

b)  const char* 转string,直接赋值即可 

const char* c_s = “abc”; 
 string s(c_s); 

c)  string 转char* 
 string s = “abc”; 
 char* c; 
 const int len = s.length(); 
 c = new char[len+1]; 
 strcpy(c,s.c_str()); 

d)  char* 转string 
 char* c = “abc”; 
 string s(c); 

e)  const char*char* 
 const char* cpc = “abc”; 
 char* pc = new char[strlen(cpc)+1]; 
 strcpy(pc,cpc);

f)  char*const char*,直接赋值即可 
 char* pc = “abc”; 
 const char* cpc = pc;

The difference between strcpy, sprintf and memcpy

copy content

strcpy can only copy strings; sprintf source objects can be of various data types, and target operation objects are strings; memcpy can copy anything

copy method

strcpy does not need to specify the length,It ends when the copied string end character "\0" is encountered, so it is easy to overflow;memcpy determines the length of the copy according to the third parameter

use

When copying strings, strcpy is usually used; sprintf mainly realizes the conversion of other data types to strings; when copying other types, memcpy is generally used

effectiveness

memcpy is the highest; strcpy is the second; sprintf is the lowest

The difference between strcpy and strncpy functions? Which function is safer?

function prototype
char* strcpy(char* strDest, const char* strSrc)
char *strncpy(char *dest, const char *src, size_t n)
Security Question
  • strcpy function: If the memory space indicated by the parameter dest is not large enough, it may cause a buffer overflow (buffer Overflow) error, please pay special attention when writing the program, or replace it with strncpy().
  • strncpy function: used to copy the first n characters of the source string, the memory areas pointed to by src and dest cannot overlap, and dest must have enough space for n characters.
The relationship between strncpy target length, specified length and original length
  • If the target length > the specified length > the source length, then copy all the source length to the target length and automatically add '\0'
  • If the specified length < source length, copy the source length to the target string by the specified length, excluding '\0'
  • Runtime error if specified length > target length;

memset

Used to fill a specific value into a block of memory.

statement

void *memset(void *str, int c, size_t n)

  • str: points to the memory block to be filled
  • c: The value to be set.
  • n: the number of characters to be set to the value

What will happen to memset(this,0,sizeof(*this)) inside the class member function?

The class contains a virtual function table

Doing so will destroy the virtual function table, and subsequent calls to virtual functions will be abnormal

The class contains objects of type C++

Since the object is initialized before the code in the constructor body is executed, assuming that the object allocates memory in its constructor, then using memset willThe memory space of the object is set to 0, corrupting the object memory.

Supongo que te gusta

Origin blog.csdn.net/qaaaaaaz/article/details/130734546
Recomendado
Clasificación