c++arrayspointersstructurecen-xfs

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

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(i.e 7 times )

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( (i.e)Structure within structure) and I require just specific members from this structures


Solution

  • 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. 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. In the code snippet it looks like the char array here was never actually allocated any memory hence the garbage values.

    I would however change this struct to a more idiomatic c++ design like the following:

    #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.

    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.