【问题标题】:How to copy a structure(which is a structure within structure) and fill it in the array of structure in C++如何复制结构(这是结构中的结构)并将其填充到C ++中的结构数组中
【发布时间】:2015-05-26 13:15:50
【问题描述】:

我有一个结构,其中结构内的结构为 显示在以下问题中: How to dynamically fill the structure which is a pointer to pointer of arrays in C++ implementing xfs

我需要将上述结构的值提取到我创建的另一个结构中。这个结构需要被视为结构数组。

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

这个结构是一个结构数组,因为我需要以 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;
}

以上代码是写在类库中的,需要在类库中显示。

但是由于内存分配问题,我被困在我创建的结构中获取垃圾值。 我已经成功填充了主要结构((即)结构内的结构),我只需要这个结构中的特定成员

【问题讨论】:

  • 有什么原因不能使用标准容器吗?
  • @shuttle87 : 什么是标准容器??
  • @shuttle87:所以你说要使用这些结构与 std::array 或 std::vector 对吗? .是的,我确实有问题,因为原始结构是预先定义的。因此我创建了一个用户定义的
  • @shuttle87:我遇到了内存问题,因为它没有正确地将数据存储在创建的结构中

标签: c++ arrays pointers structure cen-xfs


【解决方案1】:

你有这个结构:

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

假设LPSTR 来自windows types,那么它是大多数现代系统上char * 的类型定义。如果是这种情况,那么您需要为该数组分配内存以及结构的空间。当你为这个结构创建空间时,你留出足够的内存来存储指针和其他 2 个数据成员,但是指针还没有指向任何有效的东西,你所做的就是留出足够的空间来存储指针。在代码 sn-p 中,看起来这里的 char 数组实际上从未分配过任何内存,因此是垃圾值。

不过,我会将此结构更改为更惯用的 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)
        {}
};

由于这种方法的内存管理更容易处理。 然后,您可以将这些结构存储在std::vector 中,并创建一个实用函数来转换为原始数组如果需要

将所有数据存储在容器中,然后在调用现有库的代码边界进行转换,这是管理此类情况复杂性的更好方法。

【讨论】:

  • @TechBrkTru,只需使用构造函数。
  • 错误 2 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int ....我收到此错误,还有什么选择,只需将它们声明为单位??? @shuttle87
  • 如果要使用 c++ 方法,结构的名称会有所不同,但其余代码基本保持不变,然后将 struct Sp_cashinfo 更改为 struct SP_CASHUNITINFO
  • 我把uint32_t改成unsigned int就成功了,其实不用改结构名,设置完这个结构的内存空间后再查询,我应该怎么填数据??? @shuttle87
  • 这是因为您没有使用最新的 c++ 标准,您可以通过编译器标志启用它以获得 c++11 支持,或者如果您不能这样做,您可能需要使用诸如#include &lt;cstdint&gt; 之类的头文件来获取这些类型。至于填充数据,大部分和以前一样。如果您在创建结构时拥有所有数据,那么您可以使用构造函数。
猜你喜欢
  • 2012-02-25
  • 2016-12-09
  • 2018-06-30
  • 2015-01-06
  • 2014-10-12
  • 2017-04-02
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多