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:
- Solution to the segmentation fault of single chain table in C language
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- The Vue project is packaged and deployed to tomcat, and an Error 404 is reported as soon as it is refreshed
- VBA “user defined type not defined” MSXML2.DOMDocument
- 【error】Segmentation fault (core dumped)
- Segmentation fault (SIGSEGV) location method
- An error is reported when passing data pages using Ajax: Vue is not defined
- Django project running service reported an error NameError: name ‘OS’ is not defined
- The sparse matrix of R language is too large to be used as.matrix
- C language string processing error warning, c4996, sprintf, predicted, c4996, strcpy, c4996, strcat
- Error: (22,57) Java: type parameter… Mapper is not in the range of type variable M
- Failed to execute ‘getComputedStyle’ on ‘Window’: parameter 1 is not of type ‘Element’.
- The C language qsort() function reports an error for overflow of – 2147483648 and 2147483648
- The springboot integration mybatis reported an error. The parameter cannot be found
- Uncaught TypeError: Failed to execute ‘removeChild’ on ‘Node’: parameter 1 is not of type ‘Node’.
- “XXXXXX is a” namespace “, but here it is used as a” type “
- Uncaught TypeError: Failed to execute ‘appendChild’ on ‘Node’: parameter 1 is not of type ‘Node How to Fix
- An error of 500 is reported when an item is assigned to a role
- Error type referred to is not an annotation type:RedisCache
- An import error is reported in the python. The solution to setting. Pylintrc is invalid