Skip to content

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

Open
@dzalf

Description

@dzalf

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions