Problem Description:
In the data structure operation, a function to initialize the table is written:
#define InitSize 100
#define ElemType int
typedef struct {
ElemType* list;
int len;
int size;
} sql_;
int sql_init(sql_ *head) {
(*head).list = (ElemType*)malloc(InitSize*sizeof(ElemType));
(*head).len = 0;
(*head).size = InitSize;
return 1;
}
int main() {
sql_ L1;
sql_init(&L1);
sql_*L2;
sql_init(L2);
system("pause");
}
However, an error is reported at runtime: segmentation fault
After entering the debugging interface, it is found that L1 has been initialized successfully, but there is a problem when it comes to L2. What is the difference between L1 and L2 initialization codes?
Analyze the problem:
sql_ L1;
sql_init(&L1);
sql_*L2;
sql_init(L2);
It can be seen that L1 is declared as SQL_ Type; L2 is declared as SQL*_ Type.
There is no warning in vscode, but there is a warning and error in vs2019, and it can not even be compiled. So what’s the reason?
Through network retrieval, we get such an article: int * a in C + +; int & a; int & * a; int * & a_ Tianya Mingyue Dao blog – CSDN blog
Inspired by the quoted concepts mentioned in the article, we made the following attempts:
sql_ L;
sql_*L2 = &L;
sql_init(L2);
After such processing, the initialization can be completed successfully. Therefore, the following guess is made:
When defining a macro type, only SQL is declared_ Type, but for SQL_* The type is not declared, so it is declared in main sql_ * L1; The compiler can’t find the prototype, so it can’t reference SQL when declaring parameters_* Type, so vs the more stringent compiler found this problem and told L2 that memory could not be allocated.
resolvent:
Using predefined SQL_ Type declaration, use its reference when parameters need to be passed & amp; L as SQL_* Arguments of type, that is, the method of initializing L1 mentioned earlier:
sql_ L1;
sql_init(&L1);
Read More:
- 206. Reverse Linked List [easy] (Python)
- 21. Merge Two Sorted Lists [easy] (Python)
- Code::Blocks 12.11 error: ‘nullptr’ was not declared in this scope&GNU GCC -std=gnu++0x
- Array initializer is not allowed here
- Detailed explanation of Python__ new__() method
- Bad default revision ‘head’
- Concat error caused by tensorflow version
- Error: LTO wrapper failed collect2: error: LD returned 1 exit status
- Error c2064: term does not evaluate to a function in VC
- Analysis of compilation errors of “error conflicting types for function”
- C++ error: jump to case label crosses initialization
- PCL Programming Notes — assertion ` PX! = 0 ‘failed
- Git pull error: pull is not possible because you have unmerged files
- Solution of off grid pin in Ad
- Pull is not possible because you have unmerged files. Please, fix them up in the work tree, and then
- Eclipse software Java solution: editor does not contain a main type error box
- Git pull undo misoperation
- Configuration of OpenGL under CodeBlocks and solutions to problems encountered
- g++ c++ error: ‘malloc’ was not declared in this scope
- TypeError: Failed to execute ‘fetch‘ on ‘Window‘: Request with GET/HEAD method cannot have body.