结构体内部的动态内存
我正在编辑一段代码,这是一个大项目的一部分,它使用“const”来初始化一堆数组。 因为我想参数化这些常量,所以我必须调整代码以使用“malloc”来分配内存。 不幸的是,结构存在一个问题:我无法在结构本身中分配动态内存。在外部执行此操作会导致对原始代码进行大量修改。
这是一个小例子:
int globalx,globaly;
struct bigStruct{
struct subStruct{
double info1;
double info2;
bool valid;
};
double data;
//subStruct bar[globalx][globaly];
subStruct ** bar=(subStruct**)malloc(globalx*sizeof(subStruct*));
for(int i=0;i<globalx;i++)
bar[i]=(*subStruct)malloc(globaly*sizeof(subStruct));
};
int main(){
globalx=2;
globaly=3;
bigStruct foo;
for(int i=0;i<globalx;i++)
for(int j=0;j<globaly;j++){
foo.bar[i][j].info1=i+j;
foo.bar[i][j].info2=i*j;
foo.bar[i][j].valid=(i==j);
}
return 0;
}
注意:在我正在编辑的程序代码中,globalx 和 globaly 是指定名称空间中的 const。现在我删除了“const”,以便它们可以充当仅设置一次的参数。
总结:如何为结构内的子结构正确分配内存? 非常感谢!
最大限度
I'm editing a piece of code, that is part of a big project, that uses "const's" to initialize a bunch of arrays.
Because I want to parametrize these const's I have to adapt the code to use "malloc" in order to allocate the memory.
Unfortunately there is a problem with structs: I'm not able to allocate dynamic memory in the struct itself. Doing it outside would cause to much modification of the original code.
Here's a small example:
int globalx,globaly;
struct bigStruct{
struct subStruct{
double info1;
double info2;
bool valid;
};
double data;
//subStruct bar[globalx][globaly];
subStruct ** bar=(subStruct**)malloc(globalx*sizeof(subStruct*));
for(int i=0;i<globalx;i++)
bar[i]=(*subStruct)malloc(globaly*sizeof(subStruct));
};
int main(){
globalx=2;
globaly=3;
bigStruct foo;
for(int i=0;i<globalx;i++)
for(int j=0;j<globaly;j++){
foo.bar[i][j].info1=i+j;
foo.bar[i][j].info2=i*j;
foo.bar[i][j].valid=(i==j);
}
return 0;
}
Note: in the program code I'm editing globalx and globaly were const's in a specified namespace. Now I removed the "const" so they can act as parameters that are set exactly once.
Summarized: How can I properly allocate memory for the substruct inside the struct?
Thank you very much!
Max
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑你对 C++ 缺乏经验。合乎逻辑的解决方案是在构造函数中分配内存。从这个级别开始教授 C++ 会相当复杂。
I suspect you've got little experience with C++. The logical solution is to allocate the memory in the constructor. It would be rather complex to start teaching C++ from that level here.
这是 C 或 C++ 代码。标签上写着 C++,但代码看起来就像 C。为什么使用
malloc
而不是new
?来回答你的问题。为该结构提供一个构造函数来分配内存,并为该结构提供一个析构函数来删除它。
请记住,在 C++ 中,类和结构之间的唯一区别是,默认情况下,类中的成员是私有的,而结构中的成员默认是公共的。
Is this C or C++ code. The tags say C++ but the code looks just like C. Why are you using
malloc
instead ofnew
?To answer your question. Give the struct a constructor to allocate the memory and a destructor to delete it.
Remember, in C++ the only difference between classes and structs is that members are private by default in a class and public by default in a struct.
使用构造函数进行所有初始化(包括内存分配),并使用析构函数释放内存。并且不要使用
malloc
因为您已使用C++
标记标记您的问题。malloc
只是分配内存,不会初始化对象。以下示例展示了它在 C++ 中的外观:Use constructors to do all initialization (including memory allocation), and destructors to free memory. And do not use
malloc
since you have tagged your question withC++
tag.malloc
is only allocates the memory, it will not initialize objects. The following sample shows how it could look in C++:您可以创建一个函数initialize_bigStruct(),并在每次定义bigStruct 后使用它。您将需要通过简单的查找/替换来修改代码。
C 中不允许添加函数,但是如果您使用 C++,那就完全不同了。
You can make a function initialize_bigStruct() and use it after every definition of bigStruct. You will need to modify your code with simple find/replace.
Adding functions is not allowed in C, however if you are using C++ its a different story altogether.
只是 C 中的一个建议,您需要调用一个函数,因为您不能像您尝试那样在 struc 中使用 malloc。
Just a suggestion in C where you need to call a function since you cant use malloc inside a struc like you where trying to.