簡體   English   中英

如何將指針傳遞給指針向量?

[英]How to pass a pointer to vector of pointers?

這是一個重復問題的 C++ 版本。 我在下文中對向量的指針有所了解,但我這樣做是為了復制一個更大的項目。 info_a 的成員沒有從 print_b 中正確打印,並且我無法正確傳遞指針向量。

根據我的嘗試,info_a 的成員無法從 print_b 中正確打印。 第一個元素很好,但接下來的兩個不是。

結構和 print_b 來自第三方 api,我試圖傳遞他們所期望的。

這是工作代碼...

http://coliru.stacked-crooked.com/a/c3ad6af6da9409a5

有誰知道我哪里出錯了?

typedef struct {
    uint16_t a1;
    uint8_t a2;
    uint8_t a3;
} info_a;

typedef struct {
    uint16_t id;
    unsigned int arr_sz;
    info_a *arr;
} info_b;

void print_a(const info_a* a)
{
    using namespace std;

    cout <<
        "a->a1 0x" << hex << setfill('0') << setw(4) << a->a1 << std::endl <<  
        "a->a2 0x" << hex << setfill('0') << setw(2) << a->a2 << std::endl <<  
        "a->a3 0x" << hex << setfill('0') << setw(2) << a->a3 << std::endl;    
}

void print_b(const info_b* b)
{ 
    using namespace std;

    cout << "b->id 0x" << hex << setfill('0') << setw(4) << b->id << endl      
        << "b->arr_sz " << hex << setfill('0') << setw(2) << b->arr_sz << endl;

    for (unsigned int i = 0; i < b->arr_sz; ++i) {                             
        const info_a *elem = &(b->arr[i]);                                     

        print_a(elem);
    }
}

int main(int argc, char **argv)
{
    std::shared_ptr<std::vector<std::shared_ptr<info_a>>> sp_info_a =
        std::make_shared<std::vector<std::shared_ptr<info_a>>>();              

    std::shared_ptr<std::vector<std::shared_ptr<info_b>>> sp_info_b =          
        std::make_shared<std::vector<std::shared_ptr<info_b>>>();              

    int offset = 0;

    for (uint16_t i = 1; i <= 1; ++i) {
        for (uint16_t j = 1; j <= 3; ++j) {                                    
            std::shared_ptr<info_a> a_info =                                   
                std::make_shared<info_a>(info_a { j, 0x31, 0x32 } );           

            sp_info_a->push_back(a_info);                                      
        }

        std::shared_ptr<info_b> b_info = std::make_shared<info_b>(             
            info_b {
                static_cast<uint16_t>(i),                                      
                static_cast<unsigned int>(sp_info_a->size()),                  
                (*sp_info_a)[offset].get()                                     
            });

        sp_info_b->push_back(b_info);

        print_b(b_info.get());

    }

    return 0;
}

我一直無法正確傳遞指針向量

這是問題的核心。 您正在調用的 C API 不需要指針向量,它需要結構向量。

特別是這點...

    unsigned int arr_sz;
    info_a *arr;

...期望從arr開始,一個接一個地在 memory 中找到arr_sz結構。

您的代碼分別分配所有結構,然后將第一個結構的地址存儲在arr中。 然后打印代碼離開arr[0]的末尾,期望找到下一個結構,但是由於您已經分別分配了它們,所以它們被存儲在 memory 中的位置。

為了解決這個問題,無論你做什么,你都必須創建一個std::vector<info_a>的等價物(然后你可以將一個info_b包裹起來,作為一種數組視圖)。 這就是保證結構在 memory 中實際上是相鄰的。

它不起作用,因為您將std::shared_ptr<info_a>數組解釋為info_a數組。 這將起作用:

int main(int argc, char **argv)
{
    std::shared_ptr<std::vector<info_a>> sp_info_a =
        std::make_shared<std::vector<info_a>>();

    std::shared_ptr<std::vector<std::shared_ptr<info_b>>> sp_info_b =
        std::make_shared<std::vector<std::shared_ptr<info_b>>>();

    int offset = 0;

    for (uint16_t i = 1; i <= 1; ++i) {
        for (uint16_t j = 1; j <= 3; ++j) {
            sp_info_a->push_back(info_a { j, 0x31, 0x32 });
        }

        std::shared_ptr<info_b> b_info = std::make_shared<info_b>(
            info_b {
                static_cast<uint16_t>(i),
                static_cast<unsigned int>(sp_info_a->size()),
                sp_info_a->data() + offset
            });

        sp_info_b->push_back(b_info);

        print_b(b_info.get());

    }

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM