2011-11-26 70 views
4

这个问题应该很简单,也许很愚蠢,但我找不到问题。STL Vectors and the new operator

基本上,我必须用自然语言解析一些句子。我需要实现一个操纵“块”的简单算法。一个Block由2个假词构成,由20个字(字符串)组成。

下面的代码:

typedef vector<string> Pseudosentence; 
#define W 20 // A Pseudosentence is made of W words 
#define K 2 // A block is made of K Pseudosentences 

class Block { 
public: 
vector<Pseudosentence> p; 
multimap<string, int> Scoremap; 
Block() { 
    p.resize(2); 
} 
Block(Pseudosentence First, Pseudosentence Second){ 
    p.resize(2); 
    p[0] = First; 
    p[1] = Second; 
} 
void rankTerms(); // Calculates some ranking function 
void setData(Pseudosentence First, Pseudosentence Second){ 
    p[0] = First; 
    p[1] = Second; 
} 
}; 
stringstream str(final); // Final contains the (preprocessed) text. 
string t; 
vector<Pseudosentence> V; // V[j][i]. Every V[j] is a pseudosentence. Every V[j][i] is a word (string). 
vector<Block> Blocks; 
vector<int> Score; 
Pseudosentence Helper; 
int i = 0; 
int j = 0; 
while (str) { 
    str >> t; 
    Helper.push_back(t); 
    i++; 
    //cout << Helper[i]; 
    if (i == W) { // When I have a pseudosentence... 
     V.push_back(Helper); 
     j++; // This measures the j-th pseudosentence 
     Helper.clear(); 
    } 
    if (i == K*W) { 
     V.push_back(Helper); 
     j++; // This measures the j-th pseudosentence 
     Helper.clear(); 
     //for (int q=0; q < V.size(); ++q) { 
      //cout << "Cluster "<< q << ": \n"; 
      //for (int y=0; y < V[q].size(); ++y)  // This works 
       //cout << y <<": "<< V[q][y] << endl; 
     //} 
     Block* Blbl = new Block; 
     Blbl->setData(V[j-1], V[j]); // When I have K pseudosentences, I have a block. 
     cout << "B = " << Blbl->p[0][5]<< endl; 
     Blbl->rankterms(); // Assigning scores to words in a block 
     Blocks.push_back(*Blbl); 
     i = 0; 
    } 
} 

代码编译,但是当我尝试从模块使用setData(a,b)方法的XCode带我到stl_construct.h并告诉我,他收到了EXC_BAD_ACCESS信号。

到我采取的代码是这样的:

/** @file stl_construct.h 
* This is an internal header file, included by other library headers. 
* You should not attempt to use it directly. 
*/ 

#ifndef _STL_CONSTRUCT_H 
#define _STL_CONSTRUCT_H 1 

#include <bits/cpp_type_traits.h> 
#include <new> 

_GLIBCXX_BEGIN_NAMESPACE(std) 

    /** 
    * @if maint 
    * Constructs an object in existing memory by invoking an allocated 
    * object's constructor with an initializer. 
    * @endif 
    */ 
    template<typename _T1, typename _T2> 
    inline void 
    _Construct(_T1* __p, const _T2& __value) 
    { 
     // _GLIBCXX_RESOLVE_LIB_DEFECTS 
     // 402. wrong new expression in [some_]allocator::construct 
     ::new(static_cast<void*>(__p)) _T1(__value); 
    } 

(即XCode中突出了实际的线是::new(static_cast<void*>(__p)) _T1(__value);所以我认为这是由于新的运营商,但事实上,调试器显示我,我可以使用一个新的块;我不能做的是一个新的Block(a,b)(带参数构造函数)或设置数据...我觉得这很尴尬,因为每个文档都说=运算符已经为矢量重载,所以它应该没有问题...对不起,我再也找不到它了。:-(

+0

这怎么可能编译?我找不到类声明的结尾,也找不到SetData。 – Lou

+1

你可以格式化你的代码吗?不要使用标签,只能使用空格。缩进和空白将不胜感激。 –

+0

对不起,我没有粘贴setData方法,我以为我有。我现在格式化它 – Tex

回答

2

每当您将元素添加到V时,您也会增加j。这意味着j将始终等于V的长度。

这意味着下面一行将会是总是导致访问1过去了V的末尾。

Blbl->setData(V[j-1], V[j]); 

后使用该值(当它成为一个Blockp载体将导致潜在的问题,所有的方式的一部分,这可能是你的问题的根源。

而且,你有内存泄漏(你new ed,但没有delete)。在这里使用scoped_ptr,或者只是在堆栈上创建值。似乎没有理由将其分配到堆上。

+0

它的工作,太棒了!我觉得这很愚蠢,谢谢! – Tex