0

所以我的主要功能是这样的:

void main {
    uint32 errmsg;
    uint32 mydata;
    if (LOG) {
        std::ofstream file;
        file.open(fileName,ios_base::app);
        file << "!!!mydata: " << mydata << ",&mydata: " << &mydata << endl;
        file.close();
    }
    errmsg = ReadReg32(0, 0, &mydata);
    if (LOG) {
        std::ofstream file;
        file.open(fileName,ios_base::app);
        file << "!!! returned mydata: " << mydata << ",&mydata: " << &mydata << endl;
        file.close();
    }
}

并在下面调用 ReadReg32:

static uint32 ReadReg32(uint32 brdNum, uint32 address, uint32 *data)
{
    std::ofstream file;
    if (LOG) {
        file.open(fileName,ios_base::app);
        file << "   ReadReg32: brdNum =" << brdNum << ", address =" << address << endl;
        file << "   ReadReg32 ... Data WAS:  " << *data << endl;
        file.close();
    }

    /* AD:TW Check if we need to do init */
    bool initstatus = checkDeferredInit();

    ADMXRC3_HANDLE phCard;
    ADMXRC3_STATUS status;

    //open card
    if((status = ADMXRC3_Open(brdNum, &phCard)) != ADMXRC3_SUCCESS)
        return status;

    //read data
    if((status = ADMXRC3_Read(phCard, NON_PREFETCHABLE_SPACE, 0, address, 4, &data)) != ADMXRC3_SUCCESS){
        ADMXRC3_Close(phCard);
        return status;
    }

    if (LOG) {
        file.open(fileName,ios_base::app);
        file << "   ReadReg32 ... Data IS:  " << data << ", &data: " << &data << ", Read status =" << status << endl;
        file.close();
    }

    //close card
    status = ADMXRC3_Close(phCard);

    if (LOG) {
        file.open(fileName,ios_base::app);
        file << "   ReadReg32 ... Close card status =" << status << ", Returning ..." << endl;
        file.close();
    }
    return status;
}

相关的输出是这样的:

!!!mydata: 348,&mydata: 000000000012F050
   ReadReg32: brdNum =0, address =0
   ReadReg32 ... Data WAS:  348
checkDeferredInit
   ReadReg32 ... Data IS:  0000000081040102, &data: 000000000012EF30, Read status =0
   ReadReg32 ... Close card status =0, Returning ...
!!! returned mydata: 348,&mydata: 000000000012F050

其中 0000000081040102 是我希望看到的正确数字,但它永远不会将其返回给调用函数,即 mydata 永远不会更新。

有趣的笔记:

  1. 在 ADMXRC3_Read 中,如果我不使用 &data,我会得到“不正确”的数据值
  2. 在 ADMXRC3_Read 中,我坚持 &data 的类型应该是 void *pbuffer?

任何想法都像一箱啤酒或一瓶中规中矩的盒装葡萄酒一样值钱。;)

我很感激你的任何想法,这让我很难过......

4

1 回答 1

2

乍一看,您似乎使用&了太多。

static uint32 ReadReg32(uint32 brdNum, uint32 address, uint32 *data)
{
 ....
//read data
    if((status = ADMXRC3_Read(phCard, NON_PREFETCHABLE_SPACE, 0, address, 4, &data)) != ADMXRC3_SUCCESS){
        ADMXRC3_Close(phCard);
        return status;
    }

在这里,您部分地使用参数read data调用。但已经是指针了!因此,您实际上传递了函数本地变量的地址。ADMXRC3_Read&datadata

要解决此问题,请仅调用ADMXRC3_Readwith data

重要提示:在 C 和 C++ 中,一切都是按值传递的!所以这data只是一个保存 32 位数字的变量(如果你有 32 位地址),仅此而已。

编辑:在我的笔记中,我指的是这个常见问题解答:http ://c-faq.com/ptrs/passbyref.html我写错了。有关更多信息,请参阅常见问题解答。

于 2013-09-23T09:18:05.780 回答