[英]Pointer and structure of structure in C
I have this definitions of my structures: 我对我的结构有以下定义:
typedef struct{
char name[100];//the probleme is here
char type[100];//and here
int quantity;
} product;
typedef struct{
int dim;
product produs[100];
} ceva1;
typedef struct{
int dim;
ceva1 produs[1000];
} ceva;
In main i have this code: 在主要我有此代码:
int main(){
ceva *pointer,obiect;
pointer=&obiect;
test1(pointer)
obiect.dim=0;
return 0;
}
When I try to run this program, I get an error saying "c.exe has stoped working". 当我尝试运行该程序时,出现错误消息“ c.exe停止工作”。 I've seen that, if I remove objiet the error will dissapear but I have a function and when I call that function the error appears again.
我已经看到,如果删除objiet,该错误将消失,但是我有一个函数,当我调用该函数时,该错误再次出现。 What's wrong?
怎么了?
void test1(ceva *pointer){
pointer->produs[0].produs[0].quantity=1;
} }
您可能用完了堆栈空间。
Let's note that sizeof(ceva) > 20000000
, so you're likely running out of stack space. 让我们注意,
sizeof(ceva) > 20000000
,所以您可能用完了堆栈空间。 Instead, you can allocate the data on the heap: 相反,您可以在堆上分配数据:
int main(){
ceva *data = malloc(sizeof(ceva));
test1(data);
free(data);
return 0;
}
Your ceva
structure takes up over 20MB of space. 您的
ceva
结构占用了20MB以上的空间。 That's probably a lot more than the available space you have on the stack, which commonly results in a crash at runtime (a stackoverflow) 这可能比堆栈上的可用空间大得多,这通常会导致运行时崩溃(堆栈溢出)
Allocate it dynamically instead: 而是动态分配它:
int main(){
ceva *pointer = malloc(sizeof *pointer);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.