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

发布于 2025-01-10 22:38:01 字数 2609 浏览 0 评论 0原文

我第一次在 C++ 中使用 std::vector 。

我的元素是 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;

接下来,我有一个函数 FindVar,它基于比较结构的 3 个字段来搜索 VarData 结构。如果找到,我返回找到的元素的地址(至少,这是我的意图)并返回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;
}

FindVar 是从另一个函数 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
}

起始条件是我的 Vars 向量填充了 3 个 VarData 类型的元素。在程序初始化期间,我打印了这3个元素的地址,似乎是0x11a90、0x12036和0x126dc。

之后,我使用第一个元素的参数调用“RegisterVar”。我希望它能找到0x11a90。但在 FindVar 函数中,fprintf 调用显示 0x12270 (使用 fprintf 函数中最后一个 %p 格式说明符)。但如果它找到了我的第一个元素(意味着 sVar、cVarType 和 cValType 匹配),那么为什么我看不到 0x11a90?事实上,值 0x12270 从哪里来,因为它与向量中的任何元素都不匹配?

甚至变得更奇怪了。在调用 FindVar 之后,因为它返回 true,所以我立即进入函数 RegisterVar 的 fprintf。在我看来,它应该打印完全相同的(奇怪的)值 0x12270,因为这是由 FindVar 返回的。但这似乎完全不正确。该值现在显示0x126dc,但这是第三个元素,不是我找到的!

事实上我不是在做梦,如下面的屏幕截图所示。您可以看到在 FindVar 中,它打印出名称“(A:COM ACTIVE FREQ TYPE:1 STRING)”。但是当它返回时,它会打印名称“(A:TITLE, STRING)”,这是(我确认)存储在 0x126dc 上的第三个元素的名称。

输入图片此处描述

底线:如何正确编程 FindVar,使其返回匹配元素的指针?我确信我一定做了一些完全错误的事情,没有以正确的方式使用指针和/或向量。

I'm using std::vector in C++ for the first time.

My elements are of struct VarData and are stored in a 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. If found, I return the address of the element I found (at least, that is my intention) and return 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.

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. During the initialization of the program, I printed the addresses of these 3 elements which seem to be 0x11a90, 0x12036 and 0x126dc.

After that, I'm calling "RegisterVar" with the parameters of the first element. I would expect that it will find 0x11a90. But in the FindVar function, the fprintf call shows 0x12270 (using the last %p format specifier in the fprintf function). But if it has found my first element (means that sVar, cVarType and cValType are matching), then why don't I see 0x11a90? In fact, where can the value 0x12270 come from, as it doesn't match with any of my elements in the vector?

It even gets weirder. Immediately after the call to FindVar, because it returns true, I get into the fprintf of the function RegisterVar. In my opinion, it should print exactly the same (weird) value 0x12270, as this is returned by 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!

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)". 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.

enter image description here

Bottomline: How can I correctly program FindVar so that it returns the pointer of the element that is matching? I'm sure I must be doing something completely wrong, not using pointers and/or vector in the correct way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2025-01-17 22:38:01

您需要通过指针传递 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))
    {

或通过引用:

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))
    {

You need to pass the pVar pointer by pointer:

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))
    {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文