内存分配
我有如下声明的 STRUCT1 结构
typedef struct struct1 {
short int nbr_fe;
[size_is(nbr_fe)] STRUCT2 ptr_fe[*];
} STRUCT1;
STRUCT2 也是 STRUCT1 内的另一个结构
,然后我有一个指向它的指针,如下所示
typedef [ptr] STRUCT1 * ptr;
我必须在 nbrRequested 上为 STRUCT1 基数组分配内存 到目前为止,我已经做到了,
STRUCT1 obj1;
memset((void*)&obj1, '\0' , sizeof(STRUCT1));
for(int i1=0;i1<(int)nbrRequested;i1++) {
STRUCT2 obj2;
memset((void*)&obj2, '\0' , sizeof(STRUCT2));
obj1.ptr_fe[i1] = obj2;
}
ptr ptr2;
ptr2 = &obj1;
但如果 nbrRequested 大于 500,则循环将无限进行,并且应用程序将挂起。
有没有更好的方法来分配内存而不使用for循环
I have the STRUCT1 Structure declared as below
typedef struct struct1 {
short int nbr_fe;
[size_is(nbr_fe)] STRUCT2 ptr_fe[*];
} STRUCT1;
STRUCT2 is also another structure inside STRUCT1
and then I have a pointer declared to it as below
typedef [ptr] STRUCT1 * ptr;
And I have to allocate a memory to an array of STRUCT1 base on the nbrRequested
And so far I have
STRUCT1 obj1;
memset((void*)&obj1, '\0' , sizeof(STRUCT1));
for(int i1=0;i1<(int)nbrRequested;i1++) {
STRUCT2 obj2;
memset((void*)&obj2, '\0' , sizeof(STRUCT2));
obj1.ptr_fe[i1] = obj2;
}
ptr ptr2;
ptr2 = &obj1;
but if the nbrRequested is greater than 500, the loop goes in infinite and the application hangs.
Is there any better way to allocate a memory without using for loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码片段应该清楚您需要 malloc 内存,清除它,然后您可以将指针分配给结构成员。
哦,不要忘记,当您完成此结构后,您需要释放此结构中
malloc
的所有指针,否则将出现内存泄漏。This snippet should clear up that you need to malloc the memory, clear it, then you can assign the pointer to the structure member.
Oh, and don't forget when you're done with this structure, you need to free up all the pointers that were
malloc
'd in this structure or you'll have a memory leak.您没有分配任何内容,而是覆盖堆栈上的相同数据(当循环退出时,这些数据就会超出范围。
在 C++ 中,您使用 new 运算符分配内存。在 C 中,您将使用 malloc。所以代码的最小重写将是这样的(在 C 中,因为这似乎就是您正在编写的内容)
当然,这只是将数组中的每个结构设置为 0,而不是更有意义的初始化,如果这是的话你要。
You're not allocating anything, you're overwriting the same data on the stack (which then goes out of scope when the loop exits.
In C++, you allocate memory with the new operator. In C you'd use malloc. So a minimal rewrite of your code would be something like this (in C, since that seems to be what you're writing)
Of course, this just sets every struct in the array to 0, rather than a more meaningful initialization, if that is what you want.
由于这是 C++ 代码,
我将使用如下 new 运算符
WF_LIST_WORKUNIT_P_FE *listWU = new WF_STRUCT_WORKUNIT_FE [nbrRequested + 1];
问题是结构 WF_STRUCT_WORKUNIT_FE 本身是结构 WF_LIST_WORKUNIT_FE 的嵌套结构
那是
typedef 结构 wf_list_workunit_fe
{
短整型 nbr_Workunits_fe;
[size_is(nbr_Workunits_fe)] WF_STRUCT_WORKUNIT_FE lst_Workunit_fe[*];
WF_LIST_WORKUNIT_FE;
为 WF_LIST_WORKUNIT_FE 分配内存不会起作用吗?
Since this is a C++ code
I would use new operator as below
WF_LIST_WORKUNIT_P_FE *listWU = new WF_STRUCT_WORKUNIT_FE [nbrRequested + 1];
The catch is structure WF_STRUCT_WORKUNIT_FE itself is a nested structure of Structure WF_LIST_WORKUNIT_FE
that is
typedef struct wf_list_workunit_fe
{
short int nbr_Workunits_fe;
[size_is(nbr_Workunits_fe)] WF_STRUCT_WORKUNIT_FE lst_Workunit_fe[*];
} WF_LIST_WORKUNIT_FE;
wouldnt allocating a memory to WF_LIST_WORKUNIT_FE work ?