【问题标题】:How to dynamically fill the structure which is a pointer to pointer of arrays in C++ implementing xfs如何动态填充结构,该结构是指向 C++ 中实现 xfs 的数组指针的指针
【发布时间】:2015-05-12 07:16:41
【问题描述】:

结构1:

typedef struct _wfs_cdm_cu_info
{
    USHORT usTellerID;
    USHORT usCount;
    LPWFSCDMCASHUNIT * lppList;
} WFSCDMCUINFO, * LPWFSCDMCUINFO; 

结构2:

typedef struct _wfs_cdm_cashunit
{
    USHORT usNumber;
    USHORT usType;
    LPSTR lpszCashUnitName;
    CHAR cUnitID[5];
    CHAR cCurrencyID[3];
    ULONG ulValues;
    ULONG ulInitialCount;
    ULONG ulCount;
    ULONG ulRejectCount;
    ULONG ulMinimum;
    ULONG ulMaximum;
    BOOL bAppLock;
    USHORT usStatus;
    USHORT usNumPhysicalCUs;
    LPWFSCDMPHCU * lppPhysical;
} WFSCDMCASHUNIT, * LPWFSCDMCASHUNIT;

结构 3:

typedef struct _wfs_cdm_physicalcu
{
    LPSTR lpPhysicalPositionName;
    CHAR cUnitID[5];
    ULONG ulInitialCount;
    ULONG ulCount;
    ULONG ulRejectCount;
    ULONG ulMaximum;
    USHORT usPStatus;
    BOOL bHardwareSensor;
} WFSCDMPHCU, * LPWFSCDMPHCU;      

代码:

 LPWFSCDMCUINFO lpWFSCDMCuinf = NULL;   
LPWFSCDMCASHUNIT lpWFSCDMCashUnit =  NULL;   
LPWFSCDMPHCU   lpWFSCDMPhcu = NULL;   
int i=0;
try
 {
    hResult = WFMAllocateBuffer(sizeof(WFSCDMCUINFO),WFS_MEM_ZEROINIT|WFS_MEM_SHARE,(void**)&lpWFSCDMCuinf); 
    lpWFSCDMCuinf->usCount =7;   
    lpWFSCDMCuinf->usTellerID = 0;          
    hResult = WFMAllocateMore(7*sizeof(LPWFSCDMCASHUNIT),lpWFSCDMCuinf,(void**)&lpWFSCDMCuinf->lppList);   
    for(i=0;i<7;i++)
    {
        LPWFSCDMCASHUNIT   lpWFSCDMCashUnit = NULL; 
         hResult = WFMAllocateMore(sizeof(WFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&lpWFSCDMCashUnit);
        lpWFSCDMCuinf->lppList[i] = lpWFSCDMCashUnit;//store the pointer
        //FILLING CASH UNIT
        -----------------------------
         lpWFSCDMCashUnit->ulValues =50;
        -----------------------------
        WFMAllocateMore(1* sizeof(LPWFSCDMPHCU), lpWFSCDMCuinf, (void**)&lpWFSCDMCashUnit->lppPhysical);// Allocate Physical Unit structure
        for(int j=0;j<1;j++)
        {
            LPWFSCDMPHCU   lpWFSCDMPhcu = NULL;  
            hResult = WFMAllocateMore(sizeof(WFSCDMPHCU), lpWFSCDMCuinf, (void**)&lpWFSCDMPhcu);
            lpWFSCDMCashUnit->lppPhysical[j] = lpWFSCDMPhcu;

            //FILLING Phy CASHUNIT
            -------------------------------------------------------
            lpWFSCDMPhcu->ulMaximum = 2000; 
             -----------------------------
        }

    }

    //lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit;
    hResult =WFSExecute (hService,WFS_CMD_CDM_END_EXCHANGE,(LPVOID)&lpWFSCDMCuinf,60000,&lppResult);
    return (int)hResult;

我在检索结构 1 中的所有值时卡住了。 我需要将值动态添加到这些结构中并显示 Structure1 作为输出。为此需要分配内存。我尝试使用上面的代码来分配内存,但尽管分配了值,但没有正确存储在结构。

usCount 的值会根据面额设置而变化。基于此设置usNumPhysicalCUs。 此外,当我在 WFSExecute 方法中发送 &amp;lpWFSCDMCuinf 时,lppPhysical 似乎是空的。

我不知道我哪里错了。

【问题讨论】:

  • 你希望有人阅读那堵混淆代码吗?请将其简化为能说明您的问题的内容,并使用易读的名称。
  • 嗯,这是一个符合 XFS 的代码,因为它听起来很模糊。我会尽量减少它。
  • 我不知道 XFS 但你的 c++ 代码无效... for(int i=0;i
  • 是的 int i 已经在上面声明了。我需要有 7 个物理现金单元,因此考虑了循环,那么有什么不同的方式呢?

标签: pointers structure xfs cen-xfs


【解决方案1】:

首先你必须为每个块分配内存。 对于指针数组,您将分配内存来存储指针的计数,而不是对于分配的内存中的每个指针,您必须为结构本身分配内存。 我以更简短的形式重写您的代码。没有错误检查,此代码仅为示例。

LPWFSCDMCUINFO lpWFSCDMCuinf = NULL;
HRESULT hr = WFMAllocateBuffer(sizeof(WFSCDMCUINFO), WFS_MEM_ZEROINIT|WFS_MEM_SHARE, (void**)&lpWFSCDMCuinf);
// Allocate 7 times of WFSCDMCASHUNIT
const int cuCount = 7;
lpWFSCDMCuinf->usCount = cuCount;
hr = WFMAllocateMore(cuCount * sizeof(LPWFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&lpWFSCDMCuinf->lppList);
for (int i=0; i < cuCount; i++) 
{
    // for one entry
    LPWFSCDMCASHUNIT currentCU = NULL;
    hr = WFMAllocateMore(sizeof(WFSCDMCASHUNIT), lpWFSCDMCuinf, (void**)&currentCU);
    // Store pinter
    lpWFSCDMCuinf->lppList[i] = currentCU;
    // Fill current CU data here
    // ....

    // Allocate Phisical Unit Pointers
    const int phuCount = 1;
    currentCU->usNumPhysicalCUs = phuCount;
    WFMAllocateMore(phuCount * sizeof(LPWFSCDMPHCU), lpWFSCDMCuinf, (void**)&currentCU->lppPhysical);
    // Allocate Phisical Unit structure
    for (int j=0; j < phuCount; j++)
    {
        LPWFSCDMPHCU phuCurrent = NULL;
        // Allocate Phisical Unit structure
        WFMAllocateMore(sizeof(WFSCDMPHCU), lpWFSCDMCuinf, (void**)&phuCurrent);
        currentCU->lppPhysical[j] = phuCurrent;
        // Fill Phisical Unit here
        // ..
        // ..
    }
}

除此示例外,我建议您编写一些辅助函数来分配 XFS 结构,例如 WFSCDMCUINFO。在我自己的项目中,我使用了一些宏来使用 WFMAllocate 和 WFMAllocateMore 函数序列化内存中的 XFS 结构。 XFS 结构非常复杂,并且因类而异。我编写了一些宏来序列化和反序列化内存流和 XFS 内存缓冲区中的结构。在应用程序中,我使用堆分配将 XFS 结构存储在内存中,但是当我需要在另一个 XFS 消息中返回结构时,我需要使用 WFMAllocate 和 WFMAllocateMore 将内存缓冲区传输到 XFS 内存。

【讨论】:

  • 1) 存在混淆。为什么您两次声明“LPWFSCDMPHCU”。 LPWFSCDMPHCU 当前CU = NULL; LPWFSCDMPHCU phuCurrent = NULL;2) 我不知道什么是辅助函数。我正在尝试使用 WFMAllocateBuffer 和 WFMAllocateMore ,但是在为现金单元分配 7 次时,我得到一个对象未​​设置到实例的错误 3) 我得到一个错误如:-- 错误 C2664: 'WFMAllocateBuffer' : 无法将参数 3 从 'LPWFSCDMCUINFO *' 转换为 'LPVOID *' 4) 数据填充完成但未在最后一个结构体中传递:lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit; 5) 我可以看到 currentCU 是一个未定义的值
  • 示例代码中有一些错别字。现在结构分配正确。
  • 所以最后我不需要添加 lpWFSCDMCuinf->lppList=&lpWFSCDMCashUnit;
  • lpWFSCDMCuinf->lppList 的值设置在 WFMAllocateBuffer
  • 当我将结构 lpWFSCDMCuinf.lppList 整体传递时,我可以看到它是空的。只是因为 LPWFSCDMCASHUNIT currentCU = NULL;代码已编辑供您参考-
猜你喜欢
  • 1970-01-01
  • 2012-07-16
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 2011-01-02
  • 2020-04-07
相关资源
最近更新 更多