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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:22:47+00:00 2026-06-01T19:22:47+00:00

I have this function in my program const char* Graph::toChar() { std::string str; const

  • 0

I have this function in my program

const char* Graph::toChar() {
    std::string str;
    const char* toret;
    str = "";
    for (vector<Edge*>::iterator it = pile.begin(); it != pile.end(); it++) {
        str += (*it)->toString();
    }
    toret = str.c_str();
    return toret;
}

Then I’m debugging the function everything works properly until I’m in return toret; line. I press step over and the debugger is going to std::string str; line and all string and charater variables become “”, so the final return of the function is “” (nothing).

What am I doing wrong?

*(it)->toString(); is working properly and when the debugger executes *toret = str.c_str();* the value in toret is correct.

Thx

  • 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-01T19:22:49+00:00Added an answer on June 1, 2026 at 7:22 pm

    What you’re doing here is bad: You are returning the c_str of an std::string that’s about to be deleted when it goes out of scope. regardless of debug mode or not, this means unpredictable behavior. actually rather predictable – your program will crash 🙂

    You should either return const std::string, accept std:string & as an argument and build it, or use strdup() to copy the string’s c_str to something that will stay in memory. Keep in mind that using strdup() means you’ll have to delete it somewhere later.

    Here are two forms of the function that will work:

    const std::string Graph::toChar() {
        std::string str;
        for (vector<Edge*>::iterator it = pile.begin(); it != pile.end(); it++) {
            str += (*it)->toString();
        }
        return str;
    }
    

    or

    void Graph::toChar(std::string &out) {
        out = ""
        for (vector<Edge*>::iterator it = pile.begin(); it != pile.end(); it++) {
            out += (*it)->toString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program, i have line like this: const char * str = getStr();
Ok... I have this struct and comparison function- struct Edge { char point1; char
I have this function: Program A public ICollection<ReportLocationInfo> GetAllReportsInstalled() { return _Reports; } I
I made this function: char** parse_cmd(const char* cmdline) { int i; int j =
i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
I have this: function test() { this.method = function () { $(html).mousemove(function(event) { console.log('~>
I have this: function test1() { this.count = 0; this.active = 0; this.enable =
I have this function which removes occurrences of a given element within the list
I have this function: static void Func1<T>(T x, T y) { dynamic result =
I have this function in my Javascript Code that updates html fields with their

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.