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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:20:21+00:00 2026-06-17T10:20:21+00:00

I have a function that iterates through a const char * and uses the

  • 0

I have a function that iterates through a const char * and uses the character to add objects to an instance of std::map if it is one of series of recognized characters.

#define CHARSEQ const char*

void compile(CHARSEQ s) throw (BFCompilationError)
{
     std::cout << "@Receive call " << s << std::endl;

     for(int i = 0; s[i] != '\0'; i++)
     {
         if (std::string("<>-+.,[]").find_first_of(s[i]) == std::string::npos)
         {
             throw BFCompilationError("Unknown operator",*s,i);
         }
         std::cout << "@Compiling: " << s[i] << std::endl;
         std::cout << "@address s " << (void*)s << std::endl;
         std::cout << "@var s " << s << std::endl;
         controlstack.top().push_back(opmap[s[i]]);
     }
 }

The character sequence passed is "++++++++++."
For the first three iterations, the print statements display the expected values of ‘+’, ‘+’, and ‘+’, and the value of s continues to be "+++++++++++.". However, on the fourth iteration, s becomes mangled, producing bizarre values such as 'Ð', 'öê', 'cR ', 'œk' and many other character sequences. If the line that throws the exception is removed and the loop is allowed to continue, the value of s does not change after again.

Other functions have access to s but since this is not a multithreaded program I don’t see why that would matter. I am not so much confused about why s is changing but why it only changes on the fourth iteration.

I have searched SO and the only post that seems at all relevant is this one but it still doesn’t answer my question. (Research has been difficult because searching "const char* changing value" or similar terms just comes up with hundreds of posts about what part of is is const).

Lastly, I know I should probably be using std::string, which I will if no answers come forth, but I would still like to understand this behavior.

EDIT:

Here is the code that calls this function.

CHARSEQ text = load(s);

std::cout << "@Receive load " << text << std::endl;

try
{
    compile(text);
}
catch(BFCompilationError& err)
{
    std::cerr << "\nError in bf code: caught BFCompilationError @" << err.getIndex() << " in file " << s << ":\n";
    std::cerr << text << '\n';
    for(int i = 0; i < err.getIndex(); i++)
    {
        std::cerr << " ";
    }
    std::cerr << "^\n";
    std::cerr << err.what() << err.getProblemChar() << std::endl;
    return 1;
}

Where load is:

CHARSEQ load(CHARSEQ fname)
{
    std::ifstream infile (fname);
    std::string data(""), line;
    if (infile.is_open())
    {
        while(infile.good())
        {
            std::getline(infile,line);
            std::cout << "@loading: "<< line << '\n';
            data += line;
        }
        infile.close();
    }
    else
    {
        std::cerr << "Error: unable to open file: " << fname << std::endl;
    }

    return std::trim(data).c_str();
}

and the file fname is ++++++++++. spread such that there is one character per line.

EDIT 2:

Here is an example of console output:

@loading: +
@loading: +
@loading: +
@loading: +
@loading: + 
@loading: +
@loading: +
@loading: +
@loading: +
@loading: +
@loading: .
@Receive load ++++++++++.
@Receive call ++++++++++.
@Compiling: +
@address s 0x7513e4
@var s ++++++++++.
@Compiling: +
@address s 0x7513e4
@var s ++++++++++.
@Compiling: +
@address s 0x7513e4
@var s ++++++++++.
@Compiling: 
@address s 0x7513e4
@var s ßu

Error in bf code: caught BFCompilationError @4 in file bf_src/Hello.txt:
ßu
^
Unknown operatorß
  • 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-17T10:20:23+00:00Added an answer on June 17, 2026 at 10:20 am

    Your load function is flawed. The const char* pointer returned by c_str() is valid only until the underlying std::string object exists. But data is a local variable in load and is cleared after return. Its buffer is not overwritten by zeroes but left as it were as free memory. Therefore printing out the value immediately after returning is likely to work but your program may put new values there and the value pointed by your pointer will change.

    I suggest to use std::string as the return value of load as a workaround.

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

Sidebar

Related Questions

I have a function that iterates through all HDD's on a computer, and returns
I have tried to make a function that iterates through the following array to
I have this function that iterates through a directory and is supposed to read
If I have the following function that iterates through a json response object (val2):
I have an really simple example of array of const char's and one function
So I have a script that grabs a <tr> and iterates through it to
I have this piece of Boost code that iterates through files in a directory:
Does anyone have a function that will iterate through ever masterpage/sub masterpage that a
I have some code that pulls database primary keys and iterates through them by
I have a for loop that iterates through an XML document and finds a

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.