Push_back() doesn't work with Arduino-compatible Vector library (c++)

59 Views Asked by At

I'm trying to convert a char (containing only 0/1) into a vector where each single char is stored as an integer. He use Vector library for Arduino, which is not as well documented as basic c++ STL. here is the function :

void reception_emission(char* message) {
    Vector <int> integers;
    Serial.println(message);
    Serial.println("suite");
    // Convertir la chaîne de caractères en un vecteur d'entiers
    for (int i = 0; i<strlen(message); ++i) {
        
        if (message[i] == '0' || message[i] == '1') {
            //integers.push_back(message[i] - '0')
             Serial.println(message[i]); 
              Serial.println("!!"); 
              const int nb=message[i] - '0';
              Serial.println(nb);
              Serial.println("////");
              
              integers.push_back(nb);
            } // Convertir le caractère en entier
        }
    
    Serial.print("size: "); 
    Serial.println(integers.size()); 
    // Appeler la fonction emission pour chaque entier dans le vecteur
    for (int i = 0; i < integers.size(); ++i) {
        //Serial.println(char(integers[i]));
        emission(laserPin, micros(), integers[i]);
        void_blank(laserPin, micros());
    }
} 

I call it using :

reception_emission("01001010101"); 

I print many outputs to see what is exactly the problem. On the serial monitor, I have :

0
!!
0
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
0
!!
0
////
1
!!
1
////
size: 0

it means that : every char is well converted in integer and stored in "nb".

But, at the end, you can see that the vector interger is still empty.

So, what is the problem here ? did not find anything yet.

thank you.

1

There are 1 best solutions below

0
Remy Lebeau On

Unlike std::vector, Vector does not provide its own memory storage. You have to give it storage yourself.

According to the readme at https://github.com/janelia-arduino/Vector/tree/master:

A sequence container similar to the C++ std::vector, but instead of allocating memory dynamically, this container points to an external, statically allocated c style array. The maximum size is fixed at compile time, but the size can change by pushing and popping elements from the vector. Static memory allocation is used to avoid dynamic allocation problems on very small embedded processors. Care must be taken not to dereference an empty vector, access elements beyond bounds, or use without setting the storage array.

As such, your integers vector is always empty because it has nothing to store its values into.

Try something more like this instead:

void reception_emission(char* message) {

    const int ELEMENT_COUNT_MAX = ...; // whatever max size is reasonable for your neeeds...
    int storage_array[ELEMENT_COUNT_MAX];
    Vector <int> integers(storage_array);

    int msglen = strlen(message);
    for (int i = 0; i < msglen; ++i) {
        if (message[i] == '0' || message[i] == '1') {
            Serial.println(message[i]); 
            Serial.println("!!"); 
            const int nb = message[i] - '0';
            Serial.println(nb);
            Serial.println("////");
              
            integers.push_back(nb);
        }
    }
    
    Serial.print("size: "); 
    Serial.println(integers.size()); 

    for (int i = 0; i < integers.size(); ++i) {
        //Serial.println(char(integers[i]));
        emission(laserPin, micros(), integers[i]);
        void_blank(laserPin, micros());
    }
} 

That being said, if you don't mind tweaking your logging a little then you can just get rid of the Vector altogether as you don't actually need it in this code:

void reception_emission(char* message) {

    Serial.println(message);
    Serial.println("suite");

    int msglen = strlen(message);
    int count = 0;

    for (int i = 0; i < msglen; ++i) {        
        if (message[i] == '0' || message[i] == '1') {
            Serial.println(message[i]); 
            Serial.println("!!"); 
            const int nb = message[i] - '0';
            Serial.println(nb);
            Serial.println("////");
              
            //Serial.println(char(nb));
            emission(laserPin, micros(), nb);
            void_blank(laserPin, micros());

            ++count;
        }
    }
    
    Serial.print("count: "); 
    Serial.println(count);
}