简体   繁体   English

如何复制结构(结构中的结构)并将其填充到C ++中的结构数组中

[英]How to copy a structure(which is a structure within structure) and fill it in the array of structure in C++

I have a structure which a structure within structure as shown in this following question : How to dynamically fill the structure which is a pointer to pointer of arrays in C++ implementing xfs 我有一个结构,如下面的问题所示,该结构位于结构内部: 如何动态填充该结构,该结构是在实现xfs的C ++中指向数组的指针

I need to fetch the values of the above structure to another structure that I have created.This structure needs to be considered as array of structure. 我需要将上述结构的值提取到我创建的另一个结构中。该结构应视为结构数组。

typedef struct Sp_cashinfo
{
    LPSTR lpPhysicalPositionName;
    ULONG ulInitialCount;
    ULONG ulCount;  
}SP_CASHUNITINFO;

This structure is an array of structure since I need to store in a 2D form(ie 7 times ) 该结构是结构的数组,因为我需要以2D形式存储(即7次)

int CashUnitInfo(SP_CASHUNITINFO *Sp_cdm_cashinfo)
 {
     try
    {
        -----assigned the values----------------
        hResult = WFSGetInfo (hService,dwCategory,lpQueryDetails,dwTimeOut,&lppResult); //assigned the values ,got the response 0 ie success    
        fwCashUnitInfo = (LPWFSCDMCUINFO)lppResult->lpBuffer;               
        USHORT NumPhysicalCUs;
        USHORT count =(USHORT)fwCashUnitInfo->usCount;
        Sp_cdm_cashinfo = (SP_CASHUNITINFO*)malloc(7*sizeof(SP_CASHUNITINFO));      
        for(int i=0;i<(int)count;i++)
        {
    NumPhysicalCUs =fwCashUnitInfo->lppList[i]->usNumPhysicalCUs;
    for(int j=0;j<NumPhysicalCUs;j++)//storing the values of structure
    {
        Sp_cdm_cashinfo[i].lpPhysicalPositionName   =fwCashUnitInfo->lppList[i]->lppPhysical[j]->lpPhysicalPositionName;
        Sp_cdm_cashinfo[i].ulInitialCount           =fwCashUnitInfo->lppList[i]->lppPhysical[j]->ulInitialCount;
    }
    }
 return (int)hResult;
}

The above code is been written in a class library needs to be displayed in a class library. 以上代码是在类库中编写的,需要在类库中显示。

But due to memory allocation problem ,I'm stuck to get garbage value to the structure that I have created. 但是由于内存分配问题,我不得不为我创建的结构获取垃圾值。 I have successfully filled the Main Structure( (ie)Structure within structure) and I require just specific members from this structures 我已经成功填写了主结构(即结构内的结构),并且只需要该结构中的特定成员

You have this struct: 您具有以下结构:

typedef struct Sp_cashinfo
{
    LPSTR lpPhysicalPositionName;
    ULONG ulInitialCount;
    ULONG ulCount;  
}SP_CASHUNITINFO;

Assuming that LPSTR is from the windows types then it is a typedef for char * on most modern systems. 假设LPSTR来自Windows类型,那么在大多数现代系统上,它都是char *的typedef。 If that is the case then you need to allocate memory for that array along with the space for the struct. 如果是这种情况,则需要为该数组分配内存,并为结构分配空间。 When you create space for this struct you set aside enough memory for storing the pointer and the other 2 data members, however the pointer doesn't yet point to anything that's valid, all you have done is put aside enough space to store the poiner. 当您为该结构创建空间时,您会留出足够的内存来存储指针和其他2个数据成员,但是指针尚未指向有效的任何内容,您所要做的就是留出足够的空间来存储指针。 In the code snippet it looks like the char array here was never actually allocated any memory hence the garbage values. 在代码片段中,看起来这里的char数组从未真正分配过任何内存,因此没有垃圾值。

I would however change this struct to a more idiomatic c++ design like the following: 但是,我会将此结构更改为更惯用的c ++设计,如下所示:

#include <string>
struct Sp_cashinfo
{
    std::string lpPhysicalPositionName;
    uint32_t ulInitialCount;
    uint32_t ulCount;   

    Sp_cashinfo(std::string name, uint32_t initialCount, uint32_t count):
        lpPhysicalPositionName(name),
        ulInitialCount(initialCount),
        ulCount(count)
        {}
};

As the memory management with this approach is a lot easier to deal with. 由于使用这种方法进行内存管理要容易得多。 You can then store these structs in a std::vector and make a utility function to convert to a raw array if need be . 然后,您可以将这些结构存储在一个std::vector ,使效用函数转换为原始阵列如果需要的话

Keeping all your data stored in containers then converting at the boundaries of your code where you call the existing libraries is a better way of managing the complexity of a situation like this. 将所有数据保存在容器中,然后在调用现有库的代码边界处进行转换,是管理此类情况复杂性的更好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM