结构体内部的动态内存

发布于 2024-08-26 06:30:49 字数 1005 浏览 12 评论 0原文

我正在编辑一段代码,这是一个大项目的一部分,它使用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤君无依 2024-09-02 06:30:49

我怀疑你对 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.

花间憩 2024-09-02 06:30:49

这是 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 of new?

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.

单身情人 2024-09-02 06:30:49

使用构造函数进行所有初始化(包括内存分配),并使用析构函数释放内存。并且不要使用 malloc 因为您已使用 C++ 标记标记您的问题。 malloc只是分配内存,不会初始化对象。以下示例展示了它在 C++ 中的外观:

struct bigStruct{
    struct subStruct{
            double info1;
            double info2;
            bool valid;
    };

    // constructor
    bigStruct( size_t num_of_subs ) : bar( num_of_subs )
    {
    }
    // destructor
    ~bigStruct()
    {
    }        


protected:
    double data;    
    std::vector<subStruct> bar;
};

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 with C++ tag. malloc is only allocates the memory, it will not initialize objects. The following sample shows how it could look in C++:

struct bigStruct{
    struct subStruct{
            double info1;
            double info2;
            bool valid;
    };

    // constructor
    bigStruct( size_t num_of_subs ) : bar( num_of_subs )
    {
    }
    // destructor
    ~bigStruct()
    {
    }        


protected:
    double data;    
    std::vector<subStruct> bar;
};
安穩 2024-09-02 06:30:49

您可以创建一个函数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.

谎言月老 2024-09-02 06:30:49
int globalx,globaly;
typedef struct subStruct{
        double info1;
        double info2;
        char valid;
}subStruct;
struct bigStruct{
    struct subStruct ** bar;
    double data;
};
/*Don't bother sending gl.. var since they are global*/
void alloc_struct(struct bigStruct *foo)
{
    int i;
    foo->bar=(subStruct**)malloc(globalx*sizeof(subStruct*));
    for(i=0; i<globalx; i++)
    {
        foo->bar[i]=(subStruct*)malloc(globaly*sizeof(subStruct));
    }
}
int main(){
    int i,j;
    globalx=2;
    globaly=3;
    struct bigStruct foo;
    alloc_struct(&foo);
    for(i=0;i<globalx;i++)
            for(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;
}

只是 C 中的一个建议,您需要调用一个函数,因为您不能像您尝试那样在 struc 中使用 malloc。

int globalx,globaly;
typedef struct subStruct{
        double info1;
        double info2;
        char valid;
}subStruct;
struct bigStruct{
    struct subStruct ** bar;
    double data;
};
/*Don't bother sending gl.. var since they are global*/
void alloc_struct(struct bigStruct *foo)
{
    int i;
    foo->bar=(subStruct**)malloc(globalx*sizeof(subStruct*));
    for(i=0; i<globalx; i++)
    {
        foo->bar[i]=(subStruct*)malloc(globaly*sizeof(subStruct));
    }
}
int main(){
    int i,j;
    globalx=2;
    globaly=3;
    struct bigStruct foo;
    alloc_struct(&foo);
    for(i=0;i<globalx;i++)
            for(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;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文