Don't know C++ (1) (explicit, new and malloc, union)

Chong Wang Lv6

Here is a summary of the problems that often arise in C++ syntax. . .

update:2017-05-16 15:22:22

1.explicit keyword

The function is to indicate that the constructor is explicit rather than implicit.

Reprinted link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class CxString  // No explicit keyword: implicit conversion is allowed by default.
{
public:
char *_pstr;
int _size;
CxString(int size)
{
_size = size; // Preset string length.
_pstr = malloc(size + 1); // Allocate string storage.
memset(_pstr, 0, size + 1);
}
CxString(const char *p)
{
int size = strlen(p);
_pstr = malloc(size + 1); // Allocate string storage.
strcpy(_pstr, p); // Copy the string.
_size = strlen(_pstr);
}
// The destructor is omitted here.
};

// Example calls:

CxString string1(24); // Valid: reserves 24 bytes for CxString.
CxString string2 = 10; // Valid: reserves 10 bytes for CxString.
CxString string3; // Invalid: there is no default constructor.
CxString string4("aaaa"); // Valid.
CxString string5 = "bbb"; // Valid: calls CxString(const char *p).
CxString string6 = 'c'; // Valid: calls CxString(int); size is the ASCII value of 'c'.
string1 = 2; // Valid: reserves 2 bytes for CxString.
string2 = 3; // Valid: reserves 3 bytes for CxString.
string3 = string1; // Compiles, but a real implementation must overload '=' to manage memory safely.

In the above code, why is the sentence “CxString string2 = 10;” OK? In C++, if the constructor has only one parameter, then there will be a default conversion operation during compilation: convert the data of the data type corresponding to the constructor into an object of this class. In other words, in the code “CxString string2 = 10;”, the compiler automatically converts the integer type into a CxString class object, which is actually equivalent to the following operation:

1
2
3
4
CxString string2(10);  
or
CxString temp(10);
CxString string2 = temp;

2.error: assigning to ‘char *‘ from incompatible type ‘void *‘

The error appears in _pstr = malloc(size + 1); in the above code. The main reason is that C is very broad for type conversion, but C++ is not good. Malloc returns void *, and assignment to char * here requires type conversion.

1
s->ptr = static_cast<char*>(malloc(s->len+1));

3.malloc and new

a.malloc is a library function in C, and new is an operator in C++.

b.malloc just allocates a piece of memory instead of generating an object. It returns a void* type pointer.

For objects of non-internal data types, malloc/free alone cannot meet the requirements of dynamic objects. The object must automatically execute the constructor when it is created, and the object must automatically execute the destructor before it dies. Since malloc/free is a library function rather than an operator, it is not within the control of the compiler. The task of executing constructors and destructors cannot be imposed on malloc/free.

So the C++ solution is to combine all the actions required to create an object in an operator called new. When you create an object with new, it allocates memory for the object on the heap and calls the constructor for this memory.

The c.delete expression first calls the destructor and then frees the memory (free is often called).

Just as the new expression returns a pointer to an object, the delete expression requires the address of an object.

d.What is the difference between int a[10] and int a=new int[10] in C++

The former has a fixed size and the latter is dynamically allocated. The former is on the stack and the latter is on the heap.

If it is not a[10], but a[1000000000] or larger, then under normal circumstances, you can only use int a=new. This involves the issue of memory storage location. In the int a[] method, the memory is stored on the stack; int a=new int[] method, the memory is stored on the heap. can be non-contiguous memory, so larger memory can be allocated. Therefore, if you need to allocate larger memory, you need to allocate it on the heap**

The difference between e.new int[10] and new int[10]()

int *pia = new int[10]; // 10 uninitialized ints

int *pia2 = new int[10](); // 10 ints initialized to 0

int *p2 = new int[10](); applied for space and initialized it. int *p1 = new int[10]; only applies for space and does not initialize Reason: For some structures, we can see that () often represents the constructor, and int is the basic type initialization. Reason: You can test the output of two values ​​and you will find that the value of p1 is unknown, while p2 is cleared.

The new operator only applies to allocate a memory space. Because the name of the allocated object is not known, what is returned after allocation is only a pointer to the object and is not initialized. After adding a (), it is equivalent to calling the default constructor, which will be initialized by default and initialized with 0.

4.union

All members of the union refer to the same location in memory. When you want to store different things in the same location at different times, you can use a union.

1
2
3
4
5
6
7
union
{
float f;
int i;
}fi;
fi.f = 3.14159;
printf("%d\n",fi.i);