0

このコードの目的は、タブ区切りファイルのフィールドからメモリにデータをすばやく読み込んで並べ替えることです。このコードを実行すると、セグメンテーション違反が発生することがわかりました。それは私の限られたstrtokの知識と関係があると思います。文字列をトークン化するためにいくつかのC++関数を使用する方が簡単であることはわかっていますが、このコードをできるだけ高速に実行したいと思います。ほとんどのC++コードでは、新しいオブジェクトに不必要にスペースを割り当てる必要があるようです。理想的には、コードは数億行を含むファイルで実行されます。だから、それは速い必要があります。

    #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);

}

編集:segfaultはsortコマンドで発生します。sortコマンドがないと、コードは正常に実行されます。コメントを考慮して編集。デバッガーからの出力は次のとおりです。

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

1レベル上に移動して値を見ると、次のようになります。

(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
}

this.nameなどの値はファイルからのもののように見えますが、比較対象の値はすべて0またはNULLです。

4

1 に答える 1

1

でコンパイルすると、を取得g++ -Wall -gするためにインクルードする必要があり、前のステートメントのいずれも当てはまらない場合は何かを返す必要があることがわかります。その後...string.hstrtokoperator<if

  1. の戻り値をチェックしていないfopenので、最初に見つけたセグメンテーション違反は、table.txtテストするを作成していなかったときでした。

  2. どちらの戻り値もチェックしていないstrtokため、一致する列が存在しない場合は、に渡しNULLatoiそこでセグメンテーション違反を取得できます。

プログラムがクラッシュしたときにgdbのコマンドを使用して、クラッシュを引き起こした行を確認する必要があります。bt

于 2012-11-14T04:46:47.503 に答える