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

  • Home
  • SEARCH
  • 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 9173651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:37:15+00:00 2026-06-17T16:37:15+00:00

Consider the following method which reads a line from a text file and tokenizes

  • 0

Consider the following method which reads a line from a text file and tokenizes it:

std::pair<int, int> METISParser::getHeader() {

    // handle header line
    int n;  // number of nodes
    int m;  // number of edges

    std::string line = "";
    assert (this->graphFile);
    if (std::getline(this->graphFile, line)) {
        std::vector<node> tokens = parseLine(line);
        n = tokens[0];
        m = tokens[1];
        return std::make_pair(n, m);
    } else {
        ERROR("getline not successful");
    }

}

A crash happens in std::getline (pointer being freed was not allocated – won’t go into the details here). The crash does not happen if I compile my code on other systems and is very likely not an error in my own code. For the moment I am unable to fix this, and I don’t have the time, so I’ll just try to bypass it with your help:

Can you suggest an alternative implementation that does not use std::getline?

EDIT: I am on Mac OS X 10.8 with gcc-4.7.2. I tried in on SuSE Linux 12.2 with gcc-4.7, where the crash does not happen.

EDIT: One guess was that parseLine corrupts the string. Here is the code for completeness:

static std::vector<node> parseLine(std::string line) {

    std::stringstream stream(line);
    std::string token;
    char delim = ' ';
    std::vector<node> adjacencies;

    // split string and push adjacent nodes
    while (std::getline(stream, token, delim)) {
        node v = atoi(token.c_str());
        adjacencies.push_back(v);
    }

    return adjacencies;
}
  • 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-17T16:37:17+00:00Added an answer on June 17, 2026 at 4:37 pm

    You can always write your own slower and simpler getline, just to make it work:

    istream &diy_getline(istream &is, std::string &s, char delim = '\n')
    {
        s.clear();
        int ch;
        while((ch = is.get()) != EOF && ch != delim)
            s.push_back(ch);
        return is;
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following main() method which you would find most iPhone applications: int main(int
Consider the following method in Java: public static boolean expensiveComputation() { for (int i
Consider the following Java method: public static void f(int n) { if (n<=1) {
Let's consider the following example. Writing API which has public method which returns Collection
I'm struggling trying to teach myself Mockito. Consider the following method, hasInventory() , which
Consider the following scenario. I have a method which returns ISomething , but it
Out of the following queries, which method would you consider the better one? What
Consider the following method, where I build a string and return it. I would
Consider the following extension method in c#, Traverse: IEnumerable<T> Traverse<T>( this IEnumerable<T> source, Func<T,
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields

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.