[英]C struct program crash
Hi im trying to learn data structures in CI write a program but it crash when i run it 嗨我试图学习CI中的数据结构写一个程序,但它运行时崩溃
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int x;
int y;
}structure;
typedef struct{
structure test1;
}test;
void function(test *trying){
trying->test1.x = 5;
printf("%d\n", trying->test1.x);
}
int main(){
test *mystruct;
function(mystruct);
system("pause");
return 0;
}
Thank you! 谢谢!
test *mystruct;
function(mystruct);
mystruct
pointer is not initialized and has an indeterminate value. mystruct
指针未初始化并具有不确定的值。
This statement invokes undefined behavior because of the absence of initialization: 由于缺少初始化,此语句调用未定义的行为:
trying->test1.x = 5;
Do that instead: 这样做:
test mystruct;
function(&mystruct);
When you write test *mystruct;
当你编写
test *mystruct;
, mystruct
contains garbage value . ,
mystruct
包含垃圾值 。 If this value lies outside the address space for your program, you'll get the error. 如果此值位于程序的地址空间之外,则会出现错误。 Why crash?
为何崩溃? Because it tries to overwrite protected areas of memory like the OS.
因为它试图覆盖OS等受保护的内存区域。 So either you allocate memory for
mystruct
and use it (You'll have to free the memory in this case). 因此要么为
mystruct
分配内存并使用它(在这种情况下你必须释放内存)。 Or plain declare as normal variable and pass the address to the function. 或者普通声明为普通变量并将地址传递给函数。
test mystruct;
function(&mystruct);
You can use the second half of this answer but as other commenters have noted, it's much better to simply pass the address of mystruct
to the function. 您可以使用此答案的后半部分,但正如其他评论者所指出的那样,将
mystruct
的地址简单地传递给函数会mystruct
。
test mystruct; // = (test *) malloc(sizeof(test));
function(&mystruct);
This means it's allocated on the stack, which is different than the heap, and you don't have to free the memory after you use it because it's done for you. 这意味着它在堆栈上分配,这与堆不同,并且您在使用它之后不必释放内存,因为它已经为您完成了。
You can to allocate memory on the heap for test
if you're going to declare it as a pointer. 如果要将其声明为指针,则可以在堆上分配内存以进行
test
。
Change your first line in main
to: 更改你的第一线
main
以:
test *mystruct = malloc(sizeof(test));
And free the memory that was allocated after you're done using it: 并释放使用它后分配的内存:
free(mystruct);
In function main
, variable mystruct
is not initialized to point to a valid memory address. 在函数
main
,变量mystruct
未初始化为指向有效的内存地址。 Therefore, it is most likely pointing to an invalid memory address, and when used in function function
, it eventually yields a memory access violation. 因此,它很可能指向无效的内存地址,并且当在函数
function
使用时,它最终会产生内存访问冲突。
Fix suggestion #1: 修正建议#1:
test mystruct;
function(&mystruct);
Fix suggestion #2: 修正建议#2:
test* mystruct = malloc(sizeof(test));
function(mystruct);
free(mystruct);
if you don't want to use dynamic memory then try this: 如果你不想使用动态内存,那么试试这个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int x;
int y;
}structure;
typedef struct{
structure test1;
}test;
void function(test *trying){
trying->test1.x = 5;
printf("%d\n", trying->test1.x);
}
int main(){
test mystruct;
memset(&mystruct, 0, sizeof(mystruct));
function(&mystruct);
system("pause");
return 0;
}
This creates a stack based variable and initializes its contents using the memset()
function. 这将创建一个基于堆栈的变量,并使用
memset()
函数初始化其内容。 You have an odd intermediate typedef and member that you might eliminate entirely. 你有一个奇怪的中间typedef和成员,你可以完全消除。 eg
例如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct my_s{
int x;
int y;
} my_t;
void function(my_t *t){
if(!t) return;
t->x = 5;
printf("%d\n", t->x);
}
int main(){
my_t mystruct;
memset(&mystruct, 0, sizeof(mystruct));
function(&mystruct);
return 0;
}
Lastly, Im not sure what you're trying to accomplish with the system("pause") but as for the structure, the above examples will work. 最后,我不确定你要用系统完成什么(“暂停”)但是对于结构,上面的例子将起作用。
Try these simple changes to your original code: (builds and runs without using malloc()
or calloc()
) 尝试对原始代码进行这些简单的更改:( 不使用
malloc()
或calloc()
构建和运行)
(comments in-line to explain) (在线评论解释)
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int x;
int y;
}structure;
typedef struct{
structure test1;
}TEST; //use CAPITALS to distinguish typedefed struct for readability
TEST test; //use typedef to create instance of TEST
void function(TEST *trying){
trying->test1.x = 5;
printf("%d\n", trying->test1.x);
}
int main(){
TEST *mystruct; // create local instance of TEST
mystruct = &test;//initialize mystruct pointer to point to beginning of physical struct
function(mystruct);
system("pause");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.