Reputation: 950
This question should be quite straightforward, maybe stupid but I just can't find the problem.
Basically, I have to parse some sentences in natural language. I need to implement a simple algorithm that manipulates "Blocks". A Block is made of 2 Pseudosentences, which are made of 20 words (strings).
Here's the code:
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;
}
}
The code compiles, but when I try to use the setData(a,b)
method from Block, XCode takes me to stl_construct.h
and tells me that he received a EXC_BAD_ACCESS
signal.
The code to which I am taken is this:
/** @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);
}
(The actual line that XCode highlights is the ::new(static_cast<void*>(__p)) _T1(__value);
so I thought it was due to the new operator, but in fact the debugger showed me that I can use a new Block; What I can't do is a new Block(a,b)
(with the parameter constructor) or setting data... I find this awkward, because every documentation says that the =
operator has been overloaded for vectors, so it should be no problem... Sorry again for the stupid question, but I can't find it. :-(
Upvotes: 4
Views: 635
Reputation: 20609
Every time you add an element to V
you also increment j
. This means that j
will always equal the length of V
.
That means that the below line will always result in access 1 past the end of V
.
Blbl->setData(V[j-1], V[j]);
Using that value later (when it it part of a Block
's p
vector will result in all manner of potential problems. This is likely the source of your issue.
Also, you have a memory leak (you new
ed but didn't delete
). Use a scoped_ptr
here, or just create the value on the stack. There doesn't seem to be a reason to allocate it on the heap.
Upvotes: 2