简体   繁体   English

返回 C++ 中 std::vector 的指针

[英]Returning pointer of std::vector in C++

I'm using std::vector in C++ for the first time.我第一次在 C++ 中使用 std::vector 。

My elements are of struct VarData and are stored in a std::vector Vars .我的元素是struct VarData并存储在 std::vector Vars中。

#pragma pack(push, 1) // packing is now 1
struct VarData {
    char VarType;
    UINT16 lvID;
    UINT16 DefineID;
    UINT16 Bank;
    UINT16 Offset;
    char Name[MESSAGE_SIZE];
    char Event[EVENT_NAME_SIZE];
    char ValType;
    FLOAT64 f64;
    INT32 i32;
    char s256[256];
};
#pragma pack(pop) // packing is 8 again

vector<VarData> Vars;

Next to that, I have a function FindVar that searches for a VarData structure, based on comparing 3 fields of the structure.接下来,我有一个 function FindVar ,它基于比较结构的 3 个字段来搜索 VarData 结构。 If found, I return the address of the element I found (at least, that is my intention) and return true .如果找到,我返回我找到的元素的地址(至少,这是我的意图)并返回true

bool FindVar(char* sVar, char cVarType, char cValType, VarData* pVar)
{
    for (VarData &v : Vars)
    {
        if ((v.VarType == cVarType) && (v.ValType == cValType) && (strncmp(v.Name, sVar, 255) == 0))
        {
            pVar = &v;
            fprintf(stderr, "%s: FindVar --> %s - %f, %i, %s, %p", WASM_Name, pVar->Name, pVar->f64, pVar->i32, pVar->s256, (void*)pVar);
            return true;
        }
    }

    return false;
}

The FindVar is called from within another function RegisterVar . FindVar 是从另一个 function RegisterVar中调用的。

void RegisterVar(char* sVar, char cVarType, char cValType)
{
    VarData *pVar;

    // Search if LVar is already registered
    if (!FindVar(sVar, cVarType, cValType, pVar))
    {
         // Do something if the element is not found - irrelevant for my question
    }

    fprintf(stderr, "%s: RegisterVar 1--> %s - %f, %i, %s, %p", WASM_Name, pVar->Name, pVar->f64, pVar->i32, pVar->s256, (void*)pVar);

    // Do some more stuff
}

The starting condition is that my Vars vector is filled with 3 elements of type VarData.起始条件是我的 Vars 向量填充了 3 个 VarData 类型的元素。 During the initialization of the program, I printed the addresses of these 3 elements which seem to be 0x11a90, 0x12036 and 0x126dc.在程序的初始化过程中,我打印了这 3 个元素的地址,它们似乎是 0x11a90、0x12036 和 0x126dc。

After that, I'm calling "RegisterVar" with the parameters of the first element.之后,我用第一个元素的参数调用“RegisterVar”。 I would expect that it will find 0x11a90 .我希望它会找到0x11a90 But in the FindVar function, the fprintf call shows 0x12270 (using the last %p format specifier in the fprintf function).但在 FindVar function 中,fprintf 调用显示0x12270 (使用 fprintf 函数中的最后一个 %p 格式说明符)。 But if it has found my first element (means that sVar, cVarType and cValType are matching), then why don't I see 0x11a90 ?但如果它找到了我的第一个元素(意味着 sVar、cVarType 和 cValType 匹配),那么为什么我看不到0x11a90 In fact, where can the value 0x12270 come from, as it doesn't match with any of my elements in the vector?事实上,值 0x12270 从何而来,因为它与向量中的任何元素都不匹配?

It even gets weirder.它甚至变得更奇怪。 Immediately after the call to FindVar, because it returns true, I get into the fprintf of the function RegisterVar.在调用 FindVar 之后,因为它返回 true,我立即进入 function RegisterVar 的 fprintf。 In my opinion, it should print exactly the same (weird) value 0x12270 , as this is returned by FindVar.在我看来,它应该打印完全相同的(奇怪的)值0x12270 ,因为这是由 FindVar 返回的。 But that seems completely not true.但这似乎完全不是真的。 The value is now showing 0x126dc , but that is the third element, which is not the one I found!该值现在显示0x126dc ,但那是第三个元素,不是我找到的那个!

And the fact that I'm not dreaming is shown in the screenshot below.下面的屏幕截图显示了我不是在做梦的事实。 You can see that within FindVar, it prints out the name "(A:COM ACTIVE FREQ TYPE:1 STRING)".您可以看到在 FindVar 中,它打印出名称“(A:COM ACTIVE FREQ TYPE:1 STRING)”。 But when it returns, it prints the name "(A:TITLE, STRING)" which is (I confirm) the name of the 3rd element which is stored on 0x126dc.但是当它返回时,它会打印名称“(A:TITLE, STRING)”,这是(我确认)存储在 0x126dc 上的第三个元素的名称。

在此处输入图像描述

Bottomline : How can I correctly program FindVar so that it returns the pointer of the element that is matching?底线:如何正确编程 FindVar 以便它返回匹配元素的指针? I'm sure I must be doing something completely wrong, not using pointers and/or vector in the correct way.我确定我一定是做错了什么,没有以正确的方式使用指针和/或向量。

You need to pass the pVar pointer by pointer:您需要通过指针传递pVar指针:

bool FindVar(char* sVar, char cVarType, char cValType, VarData ** pVar){
    ....
    *pVar = &v;
void RegisterVar(char* sVar, char cVarType, char cValType)
{
    VarData *pVar;
    
    // Search if LVar is already registered
    if (!FindVar(sVar, cVarType, cValType, &pVar))
    {

Or by reference:或参考:

bool FindVar(char* sVar, char cVarType, char cValType, VarData* &pVar){
    ....
    pVar = &v;
void RegisterVar(char* sVar, char cVarType, char cValType)
{
    VarData *pVar;
    
    // Search if LVar is already registered
    if (!FindVar(sVar, cVarType, cValType, pVar))
    {

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

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