Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:23:22+00:00 2026-05-27T22:23:22+00:00

I’m having a problem with vectors which I tried to solve for quite some

  • 0

I’m having a problem with vectors which I tried to solve for quite some time now. I will just post the code right away and explain it then.

#include "filesystem.h"
#include <vector>
using namespace Filesystem;

wchar_t** Path::ListDir()
{
    /*struct dirEntry
    {
        wchar_t entry[MAX_PATH];
    };*/
    vector<wchar_t*> pathList = vector<wchar_t*>();
    WIN32_FIND_DATA findData;
    HANDLE dirHandle = new HANDLE;
    wchar_t* path = L"C:\\*";

    dirHandle = FindFirstFile(path, &findData);
    if (dirHandle == INVALID_HANDLE_VALUE)
        return NULL;

    while (FindNextFile(dirHandle, &findData) != 0)
    {
        pathList.push_back(findData.cFileName);
    }

    FindClose(dirHandle);

    return NULL;
}

This is part of a filesystem class I am coding right now. It’s supposed to list all the files in a directory and return them as a 2 dimensional array of strings. If I do it like this, it fills up the list with the last file times the count of files in the folder. Which is not what I want it to do. I thought that it could be since I deliver a pointer to the push_back, but I haven’t found a way to fix it. I’m coding on windows, btw.

It would be nice if someone on here has any idea on how to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:23:22+00:00Added an answer on May 27, 2026 at 10:23 pm

    You are storing the address of findData.cFileName in the vector N times.

    You need to copy this. Possibly using:

    vector<std::wstring> pathList;
    

    EDIT: Other minor points:

    vector<wchar_t*> pathList = vector<wchar_t*>();
    HANDLE dirHandle = new HANDLE;
    

    can be replaced with:

    vector<wchar_t*> pathList;
    HANDLE dirHandle;
    

    If you must return a wchar_t** then (for example):

    // '+1' for NULL terminating string as the callers needs
    // know where the array ends.
    wchar_t** result = new wchar_t*[pathList.size() + 1];
    *(result + pathList.size()) = 0;
    
    for (size_t i = 0; i < pathList.size(); i++)
    {
        wchar_t* name = new wchar_t[pathList[i].length() + 1];
        std::copy(pathList[i].begin(), pathList[i].end(), name);
        *(name + pathList[i].length()) = L'\0';
        *(result + i) = name;
    }
    
    return result;
    

    The caller must remember to delete[] returned array:

    Path p;
    wchar_t** list = p.ListDir();
    ...
    for (size_t i = 0; 0 != *(list + i); i++)
    {
        delete[] *(list + i);
    }
    delete[] list;
    

    Though as this is C++ you should use std::vector<std::wstring>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.