0

私はWindows7でMinGWを使用してCode::Blocksを使用しています。

私はうまく機能するこの関数を持っています:

Hueso* getSkel(int cual)
{
    unsigned int cont; //SkelCargados and CantSkel are global vectors
    for (cont =0; cont < SkelCargados.size();cont++) if ( CantSkel[cont] == cual) break; // EDIT: I changed <= with < before SkelCargados.size()
    if (SkelCargados.empty() || cont>SkelCargados.size())
    {
        char linea[LINEA]; //LINEA is a macro. Max value for this string.
        FILE * f = fopen("esqueletos.txt","rt");
        if (f == NULL) return NULL;
        fgets (linea,LINEA,f);

        vector<float> puntos_; // <-- please pay attention in these 4 lines
        puntos_.push_back(2.2);
        puntos_.push_back(2.2);
        puntos_.push_back(2.2);

        while (!feof(f))
        {
            //...
        }
        fclose(f);
    }
    return SkelCargados[CantSkel[cont]];
}

そしてこれは、2番目のpush_backを試行しているときにクラッシュします。(NOT)面白いのは、ベクトルとそのpush_back()をfgetsの前に置くと、正常に動作することです。

編集:ベクトルをグローバル変数として宣言すると、それも正常に機能します。

bool CargarMapa()
{
    char linea[LINEA];
    FILE * f = fopen("mapas.txt","rt");
    if (f == NULL) return false;
    fgets (linea,LINEA,f);


    vector<float> puntos_;
    puntos_.push_back(2.2);
    puntos_.push_back(3); //Here it crashes
    puntos_.push_back(4.2);


    while (!feof(f))
    {
        //...
    }
    fclose(f);
    return true;
}

これは、クラッシュしたときに発生することです。デバッガーは「プログラム受信信号SIGSEGV、セグメンテーション違反」をスローします。そして、ファイル「new_allocator.h」の「HEREITSTOPS」コメントでマークされた行に移動します。

//(I did not write the following comment)

/*
 @brief  An allocator that uses global new, as per [20.4].
 @ingroup allocators

 This is precisely the allocator defined in the C++ Standard. 
   - all allocation calls operator new
   - all deallocation calls operator delete
*/
template<typename _Tp>
class new_allocator
{
public:
  typedef size_t     size_type;
  typedef ptrdiff_t  difference_type;
  typedef _Tp*       pointer;
  typedef const _Tp* const_pointer;
  typedef _Tp&       reference;
  typedef const _Tp& const_reference;
  typedef _Tp        value_type;

  template<typename _Tp1>
    struct rebind
    { typedef new_allocator<_Tp1> other; };

  new_allocator() throw() { }

  new_allocator(const new_allocator&) throw() { }

  template<typename _Tp1>
    new_allocator(const new_allocator<_Tp1>&) throw() { }

  ~new_allocator() throw() { }

  pointer
  address(reference __x) const { return std::__addressof(__x); }

  const_pointer
  address(const_reference __x) const { return std::__addressof(__x); }

  // NB: __n is permitted to be 0.  The C++ standard says nothing
  // about what the return value is when __n == 0.
  pointer
  allocate(size_type __n, const void* = 0)
  { 
if (__n > this->max_size())
  std::__throw_bad_alloc();

return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp))); //HERE IT STOPS
  }

私を助けてください。:(

4

2 に答える 2

3
for (cont =0; cont<=SkelCargados.size();cont++) if ( CantSkel[cont] == cual) break;

あなたが望むものではありません。おそらく、ループ終了条件<の代わりに比較演算子を使用したかったでしょう。<=書かれているように、そのベクトルに 1 つの項目がある場合break、 のインデックスで ing を実行する可能性があり、その結果、位置が 1 つしかないベクトル内の1位置にインデックスを付けようとすることになります。1(ベクトル、配列、およびその他の同様の構成要素はゼロ インデックスであることに注意してください。したがって[0]、最初の要素はゼロ[1]、2 番目の要素は次のようになります。)

ほとんどの場合、バグはpush_back実際に失敗した場所ではなく、メモリを破損したコード内の別の場所にあります (未定義の動作が発生します)。

于 2012-11-02T06:15:04.080 に答える
0

正しく<=見えません

for (cont =0; cont<=SkelCargados.size();cont++) 
    if ( CantSkel[cont] == cual) break;

私はそれがあるべきだと思います:

for (cont =0; cont<SkelCargados.size();cont++) 
    if ( CantSkel[cont] == cual) break;

も確認してください

return SkelCargados[CantSkel[cont]];

それと有効な値がありますcontCantSkel[cont]

ヒント:at()代わりに使用すると、インデックスが間違っている場合[]out_of_range例外が発生し、見やすくなります..

于 2012-11-02T06:15:00.900 に答える