-3

I have an issue doing the following:

Class "A":

    const uint8_t arr1[] = {0x00, 0x00, 0x03, 0x00, ...};
    const uint8_t arr2[] = {0xA1, 0x00, 0xFF, 0x00, ...};    

    struct s1 {
         String name;
         uint8_t const * arr;
    };
    std::vector<s1, std::allocator<alloc1>> vect1;
    std::vector<s1>::iterator it = vect1.begin();

   privateMethod1(uint8_t const * ptr) {
      // this method receives pointer correctly, so I can doPrint(ptr)
      s1 myStruct;
      myStruct.name = "name";
      myStruct.arr = ptr;
      vect1.push_back(myStruct);
   }

    myPublicMethod(String arrName) {
        uint8_t const * ptr;
        if(arrName == "A") {
              ptr = arr1;
        } else if(arrName == "B") {
              ptr = arr2;
        }
        privateMethod1(ptr);
    }

Few moments later...

  myPublicMethod2() {
      for(; it < vect1.end(); it++) {
         doPrint(it->arr);
         // here the link between pointers broken, 
         // doPrint() shows me random characters
      }
   }

Apparently the problem is to store arr1 or arr2 correctly in myStruct. Anyone has an idea where I am wrong using pointers? Thanks

Sergio
  • 5
  • 5
  • The problem is first you give us a non valid C++ code ... – bruno Dec 26 '18 at 09:41
  • `std::vector> vect1; std::vector::iterator it = vect1.begin();` -- This compiled? The iterator is for a different `vector` type. That's one reason why `auto` was introduced in C++ 11. – PaulMcKenzie Dec 26 '18 at 09:42
  • What is _String_ definition ? What is doPrint definition ? What do you execute before to call `myPublicMethod2()` ? – bruno Dec 26 '18 at 09:43
  • Hi @bruno, thanks for being curious ! The stack trace is the following: 1. myPublicMethod(). 2. myPublicMethod2(). doPrint() - that is a kind of printBMP(), as far as arr1 & arr2 are encoded bitmaps. – Sergio Dec 26 '18 at 10:01
  • @paulmckenzie, thanks! Could you give an example what is wrong and how do I use auto here. The code is compiled with iterator – Sergio Dec 26 '18 at 10:09
  • @Sergio `std::vector> vect1; auto it = vect1.begin();` – PaulMcKenzie Dec 26 '18 at 15:58

2 Answers2

1

You give us a non valid C++ code, it contains plenty of errors.

You miss to give us the definition of String and alloc1

You miss to give us the execution you did, typically the argument of myPublicMethod

So to answer you we need to use a magic crystal ball

One possibility is (arrName == "A") and (arrName == "B") are false so you do not initialize ptr. These tests returns false for instance when the definition of String is typedef const char * String;

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1. myPublicMethod(“A”); 2. myPublicMethod2(); this is how it’s executed – Sergio Dec 26 '18 at 10:13
  • **what is the definition of String** ? We use (better to say _lost_) time for you, can you have the politeness to answer to our question ? – bruno Dec 26 '18 at 10:15
  • dude, honestly I never require your precious time, neither someones else. I guess is a free will to answer and spend your time helping someone when his/her stack overflowed. So if this is not your job to answer on noobs' questions I have had my politeness. Mery Christmas anyways. – Sergio yesterday – Sergio Dec 28 '18 at 06:18
0

So, I do figured this out. The following code is close to reality example which I was posting earlier.

#include <iostream>
#include <string.h>
#include <vector>

const uint8_t arr1[] = {0x00, 0x00, 0x03, 0x00};
const uint8_t arr2[] = {0xA1, 0x00, 0xFF, 0x00};

class A {
public:
    struct s1 {
        std::string name;
        uint8_t const * arr;
    };
    std::vector<s1, std::allocator<s1>> vect1;

    void myPublicMethod(std::string arrName, std::string name) {
        uint8_t const * ptr;
        if(arrName == "A") {
            ptr = arr1;
        } else if(arrName == "B") {
            ptr = arr2;
        }
        privateMethod1(ptr, name);
    }

    void myPublicMethod2() {
        auto i = vect1.begin();
        for(; i < vect1.end(); i++) {
            doPrint(i->arr, i->name);
            // here the link between pointers broken,
            // doPrint() shows me random characters
        }
    }

private:
    void privateMethod1(uint8_t const * ptr, std::string name) {
        // this method receives pointer correctly, so I can doPrint(ptr)
        s1 myStruct;
        myStruct.name = name;
        myStruct.arr = ptr;
        vect1.push_back(myStruct);
    }

    void doPrint(const uint8_t arr[], std::string name) {
        for(uint16_t i = 0; i < sizeof(*arr); i++) {
            if(name.length() > 0) {
                std::cout << arr[i] << '\n';
            } else {
                std::cout << "No name\n";
            }
        }
    }
};


int main() {
    A ca;
    ca.myPublicMethod("A", "");
    ca.myPublicMethod("B", "image");
    ca.myPublicMethod2();
    return 0;
}

This code is just a small part of a project (main code). I found imposible / unnecessary to post whole project because there are lots of unnecessary stuffs that definitely will require a bunch of time to understand.

Initially I thought there were a problem with a pointers, particularly in A::myPublicMethod, that creates *ptr, that then being transferred to A::privateMethod1 to store in vect1. I was wrong thinking there is a mistake while saving / transferring to vect1. Lately I didn't find any changes for vect1 in a main code. Vector vas created well, the problem was in method doPrint, particularly in if(name.length() > 0)... So again, I had to stick to the logic of main code without inventing an example which was a lil far from reality.

In conclusion thanks to @paulmckenzie who indirectly gave me a reason to stop thinking in pointers. It would help me a lot if someone just confirmed that following my first example, pointers are logically correct.

Cheers,

Sergio
  • 5
  • 5