8 Replies - 255 Views - Last Post: 23 September 2019 - 01:30 PM Rate Topic: -----

#1 rrlangly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-May 15

help accessing array via pointer in struct

Posted 23 September 2019 - 08:24 AM

I have the following code posted here which compiles fine, but isn't outputting the values that I'm expecting.

http://coliru.stacke...c923fa0b2ccdb18

I'm expecting to see output like below ...
a->a1 0x0001
a->a2 0x02
a->a3 0x03
a->a1 0x0002
a->a2 0x02
a->a3 0x03
a->a1 0x0003
a->a2 0x02
a->a3 0x03
b->id 0x0001
b->arr_sz 03
b->arr follows:
    elem->a1 0x0001
    elem->a2 0x02
    elem->a3 0x03
    elem->a1 0x0002
    elem->a2 0x2
    elem->a3 0x3
    elem->a1 0x0003
    elem->a2 0x0
    elem->a3 0x0


but instead, you can see what the output looks like. Struct info_a and info_b are actually third-part structs that I cannot change. I'm just trying to set the fields and access an array of info_a via info_b::arr.

Thanks for any help.

Is This A Good Question/Topic? 0
  • +

Replies To: help accessing array via pointer in struct

#2 jimblumberg   User is online

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,658
  • Joined: 25-December 09

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 10:22 AM

Your code inside code tags:

#include <iostream>
#include <iomanip>
#include <memory>
#include <vector>

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_b(const info_b* B)/>
{
    using namespace std;

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

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

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

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;    
}

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;
    int idx = 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, 0x02, 0x03 } );           

            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_a((*sp_info_a)[idx].get());                                      
        print_a((*sp_info_a)[++idx].get());                                    
        print_a((*sp_info_a)[++idx].get());                                    

        print_b(b_info.get());

    }

    return 0;
}



You sure have complicated things beyond recognition, perhaps you need to simplify? For example why the shared_pointer to a std::vector? Totally not needed, just use a "normal" std::vector.

The problem is that you are trying to print non-printable characters not numbers when trying to print your "a" structure.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 rrlangly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-May 15

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 11:43 AM

View Postjimblumberg, on 23 September 2019 - 10:22 AM, said:

The problem is that you are trying to print non-printable characters not numbers when trying to print your "a" structure.


The complication stems from a much larger code base that I'm trying to fix, hence, my duplication to make it as close as possible.

Why does elem->a1 from print_b() not print the hex value of a1? It does the first time, but not the second or third.

I.e.,

    
    elem->a1 0x0001 <-- correct
    elem->a2 0x0
    elem->a3 0x0
    elem->a1 0x0000 <-- wrong
    elem->a2 0x0
    elem->a3 0x0
    elem->a1 0x0021 <-- wrong
    elem->a2 0x0
    elem->a3 0x0

Was This Post Helpful? 0
  • +
  • -

#4 rrlangly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-May 15

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 11:48 AM

I also have this question. I don't think I'm getting how I'm trying to print a non-printable character. Isn't 0x02 a printable decimal 2? And shouldn't this line print it as hex anyways?

    "a->a2 0x" << hex << setfill('0') << setw(2) << a->a2 << 


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is online

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,658
  • Joined: 25-December 09

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 12:06 PM

Don't you realize that a uint8_t is basically an unsigned char, and it is treated this way by cout? Anything less than 0x20 (decimal 32) (the space character) is considered a non-printing control character.
By the way look what happens when I cut and paste the "output" from the link above:

a->a1 0x0001

a->a2 0x0   // The editor window shows a "box" after the 0x0 that is one of the control characters.

a->a3 0x0

a->a1 0x0002

a->a2 0x0

a->a3 0x0

a->a1 0x0003

a->a2 0x0

a->a3 0x0

b->id 0x0001

b->arr_sz 03

b->arr follows:

    elem->a1 0x0001

    elem->a2 0x0

    elem->a3 0x0

    elem->a1 0x0000

    elem->a2 0x0

    elem->a3 0x0

    elem->a1 0x0021

    elem->a2 0x0

    elem->a3 0x0



Quote

Why does elem->a1 from print_b() not print the hex value of a1?

You need to run the program with your debugger and look at the variable being printed. But I would say you're probably not setting elem properly on line 28. By the way why aren't you calling print_a() instead of duplicating that printout code in print_b()?

Jim

This post has been edited by jimblumberg: 23 September 2019 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 rrlangly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-May 15

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 12:40 PM

Here's another pass at it. If anyone as thoughts as to why the 2nd and 3rd elements are not printing correctly, thanks for any help.

http://coliru.stacke...3ad6af6da9409a5
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,658
  • Joined: 25-December 09

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 01:03 PM

Please cut and paste the code into your post inside code tags.

Did you run the program with your debugger?

You need to insure that you actually have an array in "b" and not just a pointer to a single element.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 rrlangly   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-May 15

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 01:15 PM

I did run it inside a debugger and believe the line
    const info_a *elem = &b->arr[i]


is correct, besides, that's what the API I'm using is doing so I have to make sure it can iterate using that. Also, I thought vector storage was contiguous, I can't see the difference between giving it the first element and an array.

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;
}


Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is online

  • member icon

Reputation: 5767
  • View blog
  • Posts: 17,658
  • Joined: 25-December 09

Re: help accessing array via pointer in struct

Posted 23 September 2019 - 01:30 PM

The difference is that there is only one element in that vector so anything other that i = 0 and you are accessing the vector out of bounds.

You really really need to start to learn to use your debugger.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1