c++ - 如何为结构体数组分配内存?

标签 c++ c struct malloc wrapper

我有一个 C++ 库(实际上是另一个 C++ 库的包装器),我需要将一些 struct 传递给我的 C 应用程序。

我不知道如何动态分配内存。

我遇到段错误。

库.h

struct my_substruct {
    unsigned char                   id                    ;
    time_t                          date                  ;
    char                            *info                 ;
};
typedef struct my_substruct         My_substruct          ;

struct my_struct {
    char                            *description          ;
    unsigned char                   value                 ;
    My_substruct                    *substruct            ;
};
typedef my_struct                   My_struct             ;

库.cpp

unsigned char getStructs(My_struct *structs)
{
    vector <structCPPLibrary> structsCPPLibrary = getFromCPPLibrary();
    unsigned char numStructs                    = structsCPPLibrary.size();
    structs                                     = (My_struct *)malloc(numStructs*sizeof(My_struct));
    unsigned char indexStruct                   = 0;
    for (auto s : structsCPPLibrary)
    {
        structs[indexStruct].description        = (char *)malloc(s.description.size() + 1);
        strcpy(structs[indexStruct].description, s.description.c_str()); // In 's' is a std::string
        structs[indexStruct].value              = s.value;               // In 's' is an unsigned char too
        unsigned char numSubstructs             = s.substructs.size();   // In 's' is a vector of Substructs
        structs[indexStruct].substruct          = (My_substruct *)malloc(numSubstructs*sizeof(My_substruct));
        unsigned char indexSubstruct            = 0;
        for (auto sub : s.substruct) {
            structs[indexStruct].substruct[indexSubstruct].id   = sub.id;    // In 'sub' is an unsigned char too
            structs[indexStruct].substruct[indexSubstruct].date = sub.date;  // In 'sub' is a time_t too
            structs[indexStruct].substruct[indexSubstruct].info = (char *)malloc(sub.info.size() + 1);
            strcpy(structs[indexStruct].substruct[indexSubstruct].info, sub.info.c_str()); // In 'sub' is a std::string
            indexSubstruct++;
        }
        indexStruct++;
    }
    return indexStruct;
}

ma​​in.c

void getStructFromWrapper(void)
{
    My_struct *structs;
    unsigned char numStruct = getStructs(structs);
    show_content(structs);
}

最佳答案

改变

unsigned char getStructs(My_struct *structs) {
    ...
}
getStructs(structs);

unsigned char getStructs(My_struct **p_structs) {
    // C function can't be pass by reference, so convert to a reference here
    auto& struct = *p_structs;  
    ...
}
...
getStructs(&structs);

您的问题是您的 struct = ... 行没有修改 getStructFromWrapperstructs 的值。

关于c++ - 如何为结构体数组分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244063/

相关文章:

c++ - 在QT应用程序中制作一个透明孔

c++ - Boost tribool 在 C++ 中导致从右到左的条件评估

c++ - 需要基本示例代码编译帮助 - 我无法让任何 SDK 示例发挥作用

c - Solac 单线程 C 应用程序

c++ - 重用后 DLL 使应用程序崩溃

arrays - Go 中 []Foo(nil) 和 []Foo{} 的区别

java - 在java中实现patricia trie

C++ 程序调用 C 函数在 g++ 链接期间获取 undefined reference

C++ 无法将 1 写入结构成员

java - 如何将结构传递给 JCuda 中的内核