Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a vector object by reference to a function loses values #8

Open
dzalf opened this issue Mar 10, 2024 · 1 comment
Open

Passing a vector object by reference to a function loses values #8

dzalf opened this issue Mar 10, 2024 · 1 comment

Comments

@dzalf
Copy link

dzalf commented Mar 10, 2024

Hi there

I am trying to implement an SDCard reader of the filenames inside of it by using Vector of const char* as follows.

When I pass the vector by reference, I can retrieve the total number of elements found, HOWEVER, the const char* elements with the filenames are lost.

I have tried reassigning a storage array inside the function, passing pointers, returning another vector by copy, returning pointers, etc. No luck

#include "Vector.h"
#include "SD.h"

#define DEBUG_PRINT(x) (debug->print(x))
#define DEBUG_PRINTLN(x) (debug->println(x))

const int FILES_IN_CARD_VECTOR_SIZE = 50;
char debugBuffer[100];

typedef Vector<const char*> SDCardFiles;

SDCardFiles  filesInCard;
const char* filesInCardStorage[FILES_IN_CARD_VECTOR_SIZE];

void setup(){

    filesInCard.begin();
    filesInCard.setStorage(filesInCardStorage);

    File root = SDCard.open("/");
    retrieveSDCardFilenames(root, filesInCard);

    DEBUG_PRINT(F("Files in card: "));
    DEBUG_PRINTLN(filesInCard.size());

        for (size_t ptr = 1; ptr <= filesInCard.size(); ptr++)
        {
            // here only rubbish gets printed
            sprintf(debugBuffer, "%d. %s\n", ptr, filesInCard.at(ptr - 1));
            DEBUG_PRINTLN(debugBuffer);
        }
}

where the skimmed version of the function retrieveSDCardFilenames is:

void retrieveSDCardFilenames(File folder, SDCardFiles &filesVector)
{
    int fileCount = 0;

    while (true)
    {
        File entry = folder.openNextFile();

        if (!entry)
        {
            folder.rewindDirectory();
            break;
        }
        else
        {
            fileCount++;
        }
        entry.close();
    }

    for (int i = 0; i < fileCount; i++)
    {

        File document = folder.openNextFile();

        const char *fn = (const char *const)document.name();

        DEBUG_PRINT(i + 1);
        DEBUG_PRINT(". Fn >> ");
        DEBUG_PRINTLN(fn);

        // printing names here is OK!

        filesVector.push_back((const char *const)document.name());

         sprintf(debugBuffer, "%d. %s\n", i+1, filesVector.at(i));
          DEBUG_PRINTLN(debugBuffer);

        // this printout is OK too!

        document.close();
    }

}

This is a section of the serial console with the result that I am getting. I can see the elements from the vector passed by reference inside of the functions, however, they are lost out of its scope, however, I am passing it by reference.

Please ignore the two lines that read:

Files created:
MIS1_003.txt
MIS2_006.txt

as these belong to another section of my code that DOES work fine using vectors from this library.

Any ideas?

image

@dzalf
Copy link
Author

dzalf commented Mar 11, 2024

On a strange turn of events, I can confirm that my code fails ONLY on Arduino (ATMEGA2560). I have created a near-identical copy compatible with C++11 and the code works FLAWLESSLY.

I will describe in another comment my test setup to confirm that it only fails on the Arduino-based board.

To give you a brief preview, here is another screenshot of my serial console:

image

where the Vector filesCreated is exclusively used inside the main .cpp source file while the Vector filesInCard is passed by reference to a function. Inside it, the values are correctly retrieved and printed, HOWEVER, once the code goes back to the main source, the Vector is EMPTY.

Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant