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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:28:59+00:00 2026-06-14T07:28:59+00:00

The goal of this code is to quickly read some data into memory from

  • 0

The goal of this code is to quickly read some data into memory from fields in a tab delimited file and sort them. I have found that when I run this code, I get a segmentation fault. I assume it’s something to do with my limited knowledge of strtok. I know it would be easier to use some c++ functions for tokenizing strings, however, I would like to have this code run as fast as possible. It seems like most c++ code would have me unnecessarily allocating space for new objects. Ideally, the code will be run on files containing 100’s of millions of lines. So, it needs to be fast.

    #include <stdlib.h>
    #include <stdio.h>

    #include <string>
    #include <iostream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    class Node
    {
      public:
      string name;
      int position1;
      int position2;
      string desc;
      float value;

      bool operator<(const Node& T) const;
    };

    bool Node::operator<(const Node &T) const
    {
      int result;
      result = name.compare(T.name);
      if (result !=0) return(result);

      if (position1 != T.position1) return(position1 < T.position1);

      if (position2 != T.position2) return(position2 < T.position2);

      return(false);

    }

    class NodeList
    {
      public:
      vector<Node> nodes;
    };


    int main(void)
    {
      string filename = "table.txt";
      FILE* infile = fopen(filename.c_str(), "r");

      int buflen = 1000;
      char buffer[buflen];

      NodeList K;
      Node T;


      while(fgets(buffer,buflen,infile) != NULL)
      {
         cout<< buffer << endl;

         T.name      = string(strtok(buffer, "\t\n"));
         T.position1 = atoi  (strtok(NULL  , "\t\n"));
         T.position2 = atoi  (strtok(NULL  , "\t\n"));
         T.desc      = string(strtok(NULL  , "\t\n"));
         T.value = atof  (strtok(NULL  , "\t\n"));

         K.nodes.push_back(T);
      }

      sort(K.nodes.begin(),K.nodes.end());

      return(0);

}

EDIT: The segfault occurs in the sort command. Without the sort command the code runs normally. Edited to take comments into account. Here is the output from the debugger:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0xffffffffffffffe8 0x00007fff83a078bb in std::string::compare ()
(gdb) bt
#0  0x00007fff83a078bb in std::string::compare ()
#1  0x0000000100001333 in Node::operator< (this=0x7fff5fbfeef0, T=@0x1001fffe0) at test.cpp:27
#2  0x000000010000274e in std::__unguarded_linear_insert<__gnu_cxx::__normal_iterator<Node*, std::vector<Node, std::allocator<Node> > >, Node> (__last={_M_current = 0x100200000}, __val=@0x7fff5fbfeef0) at stl_algo.h:2309
#3  0x0000000100003f28 in std::__unguarded_insertion_sort<__gnu_cxx::__normal_iterator<Node*, std::vector<Node, std::allocator<Node> > > > (__first={_M_current = 0x100200200}, __last={_M_current = 0x1002581e0}) at stl_algo.h:2406
#4  0x000000010000437b in std::__final_insertion_sort<__gnu_cxx::__normal_iterator<Node*, std::vector<Node, std::allocator<Node> > > > (__first={_M_current = 0x100200000}, __last={_M_current = 0x1002581e0}) at stl_algo.h:2439
#5  0x0000000100004422 in std::sort<__gnu_cxx::__normal_iterator<Node*, std::vector<Node, std::allocator<Node> > > > (__first={_M_current = 0x100200000}, __last={_M_current = 0x1002581e0}) at stl_algo.h:2831
#6  0x00000001000019e8 in main () at test.cpp:76

If I go up one level and look at the values, I get this:

(gdb) print T
$1 = (const Node &) @0x1001fffe0: {
  name = {
    _M_dataplus = {
      <std::allocator<char>> = {
        <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
      members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider:
      _M_p = 0x0
    }
  },
  position1 = 0,
  position2 = 0,
  desc = {
    _M_dataplus = {
      <std::allocator<char>> = {
        <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
      members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider:
      _M_p = 0x0
    }
  },
  value = 0
}

The values for this.name etc look like they come from the file, but whatever it is being compared to has values that are all 0’s or NULL.

  • 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-14T07:29:00+00:00Added an answer on June 14, 2026 at 7:29 am

    Compiling with g++ -Wall -g, I see that you need to include string.h to get strtok, and your operator< needs to return something if none of the earlier if statements were true. After that…

    1. You aren’t checking the return value of fopen, so the first segfault I found was when I hadn’t created a table.txt to test with.

    2. You aren’t checking the return value of strtok either, so if no matching column exists, then you can pass NULL to atoi, and get a segfault there.

    You need to use gdb‘s bt command when your program crashes to find out what line triggered the crash.

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

Sidebar

Related Questions

There are some questions on Code Metrics here, especially this one on goal values.
This is the goal: to remove Navigation Bar in some of the editors (no
My goal is to get a Time instance from a DateTime instance This has
I have data like this: Team | goal a | 5 b | 8
I got this code to return a shortest path between 2 cities: goal(Z,Path,Cost) :-
My goal is to be able to quickly type this: <%= %> Can anyone
I have this code : int flags = some integer value; compte.actif = !Convert.ToBoolean(flags
The goal of this code is to run four threads, which will open four
In this code when I call the cleanGrid() function my goal was to get
im using this code $output = preg_replace('/[^(\x20-\x7F)]*/','', $output); my goal is to remove this

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.