STL vectors and the new operator

This question should be pretty simple, 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". The block consists of 2 pseudo-cases, which consist of 20 words (lines).

Here is 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 a block, Xcode takes me to stl_construct.h and tells me that it received an EXC_BAD_ACCESS signal.

The code I accept 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 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 ::new(static_cast<void*>(__p)) _T1(__value); allocates is ::new(static_cast<void*>(__p)) _T1(__value); so I thought this was due to the new statement, but the debugger actually showed me that I can use a new block, which I cannot do is a new Block(a,b) (with a parameter constructor) or setting data ... I find this inconvenient because in every documentation it says that the = operator was overloaded for vectors, therefore this should not be a problem ... Sorry again for the stupidity of the question, but I can not find it. :-(

+4
source share
1 answer

Each time you add an element to V , you also increase j . This means that j will always be equal to the length of V

This means that the line below will always have access 1 to the end of V

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

Using this value later (when it is part of the Block p vector will lead to all possible problems. This is probably the source of your problem.

In addition, you have a memory leak (you are new ed, but not delete ). Use scoped_ptr here or just create a value on the stack. There seems to be no reason to allocate it in a heap.

+2
source

Source: https://habr.com/ru/post/1383143/


All Articles