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 8532147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:38:50+00:00 2026-06-11T09:38:50+00:00

hi im loking for some advice im working in a command interpreter for class

  • 0

hi im loking for some advice
im working in a command interpreter for class and i have this “command” (that is a class) that get some c strings from internal variables and make an std::wstring, then i cast it to wchar_t * but when i return it i get just garbage on the variable,p.e

content of variable before return:

comandos disponibles: ayuda salir

content of variable after return:

����������������������������������������

i tried to make the function return an const wchar_t * but it also dont work, but if i put a string in the return it just work fine pe.

return L"test"

any idea?

–edit–

this is the code i´m using

wchar_t * ayuda::run(std::list<char* const> * lista){

    std::wstring out;

    out += L"comandos disponibles:\n"; //static header
    commandMap map = loadMap();//get a map whit all the function names

    commandMap::iterator it;
    for(it = map.begin(); it != map.end();it++){
        out+=std::wstring(it->first.begin(),it->first.end())+L"\n";// append the command name to the string
    }
    wchar_t * _out = const_cast<wchar_t*>( out.c_str() ); //cast to wchar *
    return _out;
}
  • 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-06-11T09:38:52+00:00Added an answer on June 11, 2026 at 9:38 am

    Are you trying to return a wchar_t * allocated on the stack ?

    wchar_t *MyFunction()
    {
        wchar_t myString[] = L"This will be destroyed from the stack on returned";
        return myString;
    }
    

    In that case, the string is removed from the stack, and then garbage is returned. Which would explain what you see.

    In C++, use std::string or std::wstring for strings, it prevents memory leaks and also provides useful functions. Avoid arrays as much as you can.

    #include <string>
    
    std::wstring MyFunction()
    {
        std::wstring myString = L"This will be copied, since this is not a pointer, but an instance of an object.";
        return myString;
    }
    

    The other way would be to allocate the string on the heap, be then you would need to ensure to delete it somewhere otherwise there will be a memory leak.

    wchar_t *MyFunction()
    {
        wchar_t myString[] = L"This will be destroyed from the stack on returned";
        size_t myStringLen = wcslen(myString);
    
        wchar_t *outString = new wchar_t[myStringLen+1]; //allocate heap memory
        wcscpy(outString, myString); //copy the string to heap memory
    
        return outString; //return a string that will not be destroyed.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice with the Java Print API. I have a program that
Background info: I have some WCF services that are hosted on an internal server
Being a self-confessed newbie I'm looking for some advice and guidance :) I have
I'm looking for a little sage advice from some helpful HTML/CSS masters. I am
I am working on a programming exercise that has interested me for some time.
I am working with a Class that contains constants or parameter values that all
I have read some tips that multithread implementation largely depends on the target OS
Ok so I have looked at many posts on how to get DI working
I'm working on an application that has a software watchdog. If some thread locks
I'm working on a page that will have a series of images on it.

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.