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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:08:51+00:00 2026-06-13T22:08:51+00:00

hi im doing string tokenization similarly to the example below. however, within the while

  • 0

hi im doing string tokenization similarly to the example below. however, within the while loop, i will be changing letter ‘a’ to ‘hellow’ for example. im getting segmentation fault when trying to change pch before assigning to myVar[i]. how should i go about doing it?

    map <int, char*> myVar;
    char str[] ="- This, a sample string.";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str," ,.-");
    int i = 0;

    while (pch != NULL)
    {
        printf ("%s\n",pch);

        //modify token value
        stringstream strStream;
        strStream << "hello_world";

        char newStr[7] = {0};
        memcpy(newStr, strStream, 7);

        myVar[i] = (char*)newStr;
        pch = strtok (NULL, " ,.-");
        i++;
     }
  • 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-13T22:08:52+00:00Added an answer on June 13, 2026 at 10:08 pm

    I see two bugs inside of your while loop:

    1) you are passing the stringstream itself, not the data it contains, to memcpy(). You are relying on the stringstream::operator void*() conversion operator. You are not supposed to deference that pointer, as it does not point at actual data. It is merely a flag to indicate whether the stringstream is valid or not. To pass the stringstream data to memcpy(), you have to call its str() method first to get a std::string containing the data, and then call its c_str() method to pass that data to memcpy().

    2) when you are inserting values into your std::map, you are inserting a local char[] variable each time. That char[] goes out of scope immediately afterwards, leaving the std::map containing pointers to random locations on the stack. Given the code you have shown, the char[] buffers are likely to reuse the same stack space each time.

    Since you are using C++, you really should be using more C++-oriented things, like std::string, std::cout, etc.

    Try this:

    std::map <int, std::string> myVar;
    std::string str = "- This, a sample string.";
    std::cout << "Splitting string \"" << str << "\" into tokens:" << std::endl;
    size_t start = 0;
    int i = 0;
    
    do
    {
        std::string token;
    
        size_t pos = str.find_first_of(" ,.-", start);
        if (pos != std::string::npos)
        { 
            token = str.substr(start, pos-start);
            start = pos + 1;
        }
        else
        {
            token = str.substr(start);
            start = std::string::npos;
        }
    
        std::cout << token << std::endl;
    
        //modify token value
        myVar[i] = "hello_world";
    
        ++i;
     }
     while (start != std::string::npos);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to do some simple math while I'm doing string formatting. For
I was wondering if there is any way of preserving indentation while doing string
For doing string concatenation, I've been doing basic strcpy , strncpy of char* buffers.
Python is doing string multiplication where I would expect it to do numeric multiplication,
I have some code which is doing this: String transactionId = incoming.getJMSCorrelationID(); when I
In another question I came across two different ways of doing a string replacement.
In PHP, I'm frequently doing lots of string manipulation. Is it alright to split
I'm doing some attribute parsing as a string, so I need to know how
I am doing a test to see if the connection string user ID is
Something I find myself doing more and more is checking a string for empty

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.