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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:47:08+00:00 2026-06-04T00:47:08+00:00

I am writing a compiler and use deque to store methods labels of a

  • 0

I am writing a compiler and use deque to store methods labels of a class and here’s the sample code:

#include <deque>
#include <iostream>
#include <string>

using std::cout;
using std::deque;
using std::endl;
using std::string;

int main()
{
  deque<const char *> names;

  string prefix = "___";
  const char *classname = "Point";

  const char *methodname[] = {"Init", "PrintBoth", "PrintSelf", "equals"};

  for (int i = 0; i < 4; i++)
    {
      string label = prefix + classname + "." + methodname[i];
      names.push_back(label.c_str());
    }

  for (int i = 0; i < 4; i++)
    cout << names[i] << endl;

  return 0;
}

However, the result is not what I’ve expected:

___Point
___Point.PrintSelf
___Point.PrintSelf
___Point.equals

Also, I noticed if I simply push back the methodname

names.push_back(methodname[i])

I get all the methodnames in order.

What have I done wrong here?

  • 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-04T00:47:09+00:00Added an answer on June 4, 2026 at 12:47 am
    for (int i = 0; i < 4; i++)
    {
      string label = prefix + classname + "." + methodname[i];
      names.push_back(label.c_str()); //what you're pushing? a temporary!
    
    } //<--- `label` is destroyed here and it's memory is freed.
    

    Here label is a variable which gets destroyed at the closing brace and gets created again, in each iteration.

    That means, what you’re pushing to names is a temporary value. That is causing the problem.

    I would suggest you to use this:

    std::deque<std::string> names;
    

    then do this:

    names.push_back(label); //a copy is pushed to the deque!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing some code i came across this issue: #include <iostream> class random {
I'm writing a C++ Package for later use using Code::Blocks. The project structure looks
When writing Common Lisp code, I use SLIME. In particular, I compile the buffer
I'm writing a compiler which uses C as an intermediate code which is (Currently)
I am writing a compiler in Python using Eclipse with PyDev. I've come to
I am writing an abstract matrix class (and some concrete subclasses) for use on
I'm writing a compiler for a small language, and my Parser class is currently
Here is some code I'm writing in C++. There's a call to an addAVP()
I'm writing C++ using the MinGW GNU compiler and the problem occurs when I
I am writing a markdown compiler in Erlang for server-side use. Because it will

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.