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

  • Home
  • SEARCH
  • 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 6245579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:31:51+00:00 2026-05-24T12:31:51+00:00

As a C++ noob, I can’t seem to get this right even though I

  • 0

As a C++ noob, I can’t seem to get this right even though I know it should be simple :{ – I need to create an array of LPWSTR and then populate it with unique strings. The idea was to do something simple like this:

LPWSTR *wszArray = new LPWSTR[5];

for(int x = 0; x < 5; x++)
{
    swprintf(wszArray[x], "somestring_%d", x);
}

I know that I haven’t allocated memory for the LPWSTR, but after trying a few things I am not having much luck. Also I’m not sure if the array should be free’d later once i’m done with the strings.

Any advice would be great.

  • 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-24T12:31:52+00:00Added an answer on May 24, 2026 at 12:31 pm

    What you have right now is a single pointer to pointers to wide char. You’re then initializing that with the address of an array of 5 dynamically allocated pointers to wide char’s. That’s fine as far as it goes, but does not allocate any space for the strings themselves, only for pointers to 5 strings. You then need to allocate space for the strings themselves:

    for (int i=0; i<5; i++) {
        array[i] = new char[13];
        swprintf(array[i], L"somestring_%d", i);
    }
    

    Yes, you should delete the space you allocated after you’re done using it. That would look something like:

    for (int i=0; i<5; i++)
        delete [] array[i];
    delete [] array;
    

    OTOH, you probably shouldn’t do any of this, and instead use something like:

    std::vector<std::wstring> array;
    
    for (int i=0; i<5; i++) {
         std::wostringstream temp;
         temp << L"somestring_" << i;
         array.push_back(temp.str());
    }
    

    In this case, you can retrieve a “LPWSTR” using array[i].c_str(). You don’t have to allocate or free any of the memory explicitly at all though.

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

Sidebar

Related Questions

This should be simple but I'm a noob and I can't for the life
Yep - another noob regex query, which I can't seem to get. I'm trying
This is a noob question, but I can't get Valum's Uploader to work no
This is probably a noob question, but I can't seem to find the answer.
This is probably a noob question but can't get my head around it. How
I feel like a real noob posting this, but I can't seem to find
General noob questions: (1) How can I create an NSMutable array in a buttonClicked
I feel like this is a very noob question.. but I just can't get
I apologize if this is a noob question, but I can't seem to figure
I am hoping this is a simple stupid noob mistake that can be fixed

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.