Today, when I write a program to operate the cstringarray variable:
ASSERT_VALID fails with illegal vtable pointer.
Assertion Failed: <unknown application>: File array_s.cpp, Line 236
The cstringarray virtual table in the defined struct has an error.
When I checked the program carefully, I found that the struct pointer I applied for was initialized with memest. The original struct was not added with the cstringarray variable. It was all char type, so memset was used for initialization. Www.2cto.com
The cstringarray variable cannot be initialized with memset !! (Why is it unclear ......)
Instead of memset initialization, how can I initialize my struct?
Google finds three methods to complete initialization:
1. Initialization in the constructor of the struct
In C ++, struct and class are no longer essentially different in use, so you can use constructors to initialize them.
The following code is used:
Struct Stu
{
Int nNum;
Bool bSex;
Char szName [20];
Char szEmail [100];
// Constructor Initialization
Stu ()
{
NNum = 0;
BSex = false;
Memset (szName, 0, sizeof (szName ));
Memset (szEmail, 0, sizeof (szEmail ));
}
};
You may have discovered that it is quite troublesome to assign values one by one if the struct contains a large number of members. You can write as follows:
Struct Stu
{
Int nNum;
Bool bSex;
Char szName [20];
Char szEmail [100];
// Constructor Initialization
Stu ()
{
Memset (this, 0, sizeof (Stu ));
// Or the following format
// Memset (& nNum, 0, sizeof (Stu ));
}
};
If a pointer is allocated to the struct and the pointer points to a heap memory, it is released in the destructor. The above is initialized in the constructor.
2. inherit template class initialization
First, define a template base class:
Template <typename T>
Class ZeroStruct
{
Public:
ZeroStruct ()
{
Memset (this, 0, sizeof (T ));
}
};
The structure defined later inherits from this template class.
Struct Stu: ZeroStruct <Stu>
{
Int nNum;
Bool bSex;
Char szName [20];
Char szEmail [100];
};
In this way, initialization can also be implemented.
3. Initialization during definition
Struct Stu
{
Int nNum;
Bool bSex;
Char szName [20];
Char szEmail [100];
};
// Initialization during definition
Stu stu1 = {0 };
In some structs, the first member represents the size of the struct, so you can initialize it like this:
Struct Stu
{
Int nSize; // struct size
Int nNum;
Bool bSex;
Char szName [20];
Char szEmail [100];
};
Stu stu1 = {sizeof (Stu), 0 };
The following 0 can be omitted and directly written as: Stu stu1 = {sizeof (Stu)}; it is automatically filled with 0.
Summary:
The first two types have actually converted the struct into a class, which is similar to the class usage. The third is the writing of pure struct.
If the purpose is only limited to struct, we recommend that you do not add constructors or inherit from the template class, Because struct is actually a class at this time. The third method cannot be used for initialization when defining struct. Of course, Initialization is not required at this time.