I'm trying to populate a tree as below.
m is the number of inputs and s is the data for the head node.
c is the head node and the inputs that follow are parent-child nodes. Each node has a list of children.
Now, calling addNode to always start from the top of the tree is slow for what I'm doing. Instead, I make addNode return a reference to the to node that it adds to the tree. I maintain a map iNode of to - TreeN so that I can pass a reference to the from node to addNode instead of passing it the head node.
I think what happens is when a TreeN's vector resizes, the objects in it are copied to a new vector in memory and the reference to the nodes I had in iNode are to now deallocated memory. I'm not sure though, fairly new to Cpp.
How would I get this to work the way I want it to?
class TreeN {
public:
int d;
vector<TreeN> chld;
bool operator==(const TreeN &o) {
return o.d == d;
}
};
TreeN& addNode(TreeN &root, int &from, int &to) {
cout << "f: " << from << " t: " << to << " p: " << root.d << endl;
if (root.d == from) {
root.chld.push_back(TreeN{to});
return root.chld[root.chld.size() - 1];
}
else {
for(auto &c : root.chld) {
return addNode(c, from, to);
}
}
}
int main(int argc, const char** argv) {
int m, s;
cin >> m >> s;
TreeN c = {s, vector<TreeN>{}};
map<int, TreeN&> iNode;
iNode.insert(pair<int, TreeN&>(c.d, c));
while (m--) {
int a, b;
cin >> a >> b;
map<int, TreeN&>::iterator itA = iNode.find(a);
map<int, TreeN&>::iterator itB = iNode.find(b);
if (itA != iNode.end()) {
cout << "here " << itA->first << endl;
TreeN &n = addNode(itA->second, a, b);
cout << n.d << endl;
if (itB == iNode.end()) iNode.insert(pair<int, TreeN&>(b, n));
} else {
iNode.insert(pair<int, TreeN&>(b, addNode(c, a, b)));
}
}
printTree(c);
}
Input:
4 1
1 2
1 3
2 3
3 2
This code fails and gives the following output:
here 1
f: 1 t: 2 p: 1
2
here 1
f: 1 t: 3 p: 1
3
here 2
f: 2 t: 3 p: 0 (should say 2)
Segmentation fault (core dumped)
I will not try understood what your code does and how.
By using references in strange places you made it hard to read and understand. In this code it is hard to track if lifetime of variable exceeds lifetime of reference which is points to that variable.
If you are beginner use references only as a function argument.
Anyway I see undefined behavior here:
TreeN& addNode(TreeN &root, int &from, int &to) {
cout << "f: " << from << " t: " << to << " p: " << root.d << endl;
if (root.d == from) { // consider this false
root.chld.push_back(TreeN{to});
return root.chld[root.chld.size() - 1];
}
else {
for(auto &c : root.chld) {// root.chld is empty
return addNode(c, from, to); // this is weird and wrong - but not a source of crash
}
}
// then you can reach this place and there is no return statement
// leading to UB
// I'm pretty sure compilers warns you about this problem.
}
Also this last loop has no sense. First iteration will terminate loop execution. This defensively wasn't intended.
I think what happens is when a TreeN's vector resizes, the objects in it are copied to a new vector in memory and the reference to the nodes I had in iNode are to now deallocated memory. I'm not sure though, fairly new to Cpp.
It is possible. Vector allocates some reserve (see its capacity) and when size is becomes larger then capacity new memory is allocated and values are copied/moved to new buffer. This can make references to items on vector invalid.
BUT: form what I see you are not storing those references for longer then next possible resize so this is not the issue.
Related
It has been a while that I've written code in C/C++, and I've already found an alternative solution to my problem, but I would like to know why the original code doesn't work.
I have a test class which basically only stores a string.
class test {
private:
std::string name;
public:
test(std::string name) : name(name) {};
std::string get_name() { return name; }
};
In main I have a vector which I at one point fill with test objects. The code below simulates irregular usage of the vector vect.
int main(void) {
std::vector<test *> vect;
std::vector<test *>::iterator i;
//* Comment this for a working example
std::cout << "Searching empty vector" << std::endl;
i = *(_is_in_vector(vect, std::string("test 3")));
if (i == vect.end()) {
std::cout << "Nothing found" << std::endl;
} // */
vect.push_back(new test("test 1"));
vect.push_back(new test("test 2"));
vect.push_back(new test("test 3"));
std::cout << "All:" << std::endl;
i = *(_is_in_vector(vect, std::string("test 3")));
if (i != vect.end()) {
std::cout << "Erase " << (*i)->get_name() << std::endl;
vect.erase(i);
delete *i;
}
i = *(_is_in_vector(vect, std::string("test 3")));
if (i == vect.end()) {
std::cout << "Nothing found" << std::endl;
}
std::cout << "Left:" << std::endl;
for (i = vect.begin(); i!=vect.end(); ++i) {
std::cout << (*i)->get_name() << std::endl;
delete *i;
}
vect.clear();
return 0;
}
Because searching in the vector for a test object happens multiple times, I've created the function _is_in_vector that searches a test object and returns the iterator to it.
static std::vector<test *>::iterator * _is_in_vector(std::vector<test *> &vect, std::string find) {
std::string identity = find;
static std::vector<test *>::iterator i = vect.begin();
std::cout << "Vect size: " << vect.size() << std::endl;
for (i; i != vect.end(); ++i) {
std::string tmp = (*i)->get_name(); /* Segmentation fault after filling vector*/
if (0 == identity.compare(tmp)) break;
}
return &i;
}
The problem is that the code above works when I comment out the Searching empty vector part in main. Once the vector is filled with test objects, I call _is_in_vector a second time. The vector in this function does have three entries, but (*i) all point to NULL.
Output:
Searching empty vector
Vect size: 0
Nothing found
All:
Vect size: 3
Segmentation fault
Expected output:
Searching empty vector
Vect size: 0
Nothing found
All:
Vect size: 3
Erase test 3
Vect size: 2
Nothing found
Left:
test 1
test 2
First of all it is unclear why you need to store test objects by pointer but not by value. If you do need it use smart pointer.
As for your problem, why do you return pointer to an iterator? This is the root cause of your problem - to make &i legal to return you made it static, but static local variables initialized only once and do not change value btw calls - so after first call it pointed to an element in the vector, but after that you added elements and invalidated all iterators including static i hense the segmentation fault. So fix is simple - return iterator by value and make i non static but regular, it is light and it is totally fine to do so.
PS Identifiers starting with _ are illegal in global context, details can be found here What are the rules about using an underscore in a C++ identifier?
So your function actually should look like this:
static std::vector<test *>::iterator is_in_vector( std::vector<test *> &vect, const std::string &find)
{
return std::find_if( vect.begin(), vect.end(), [find]( test *p ) {
return p->get_name() == find;
} );
}
assuming the vector should never hold nullptr, if it is the case or to play safe change condition to:
return p && p->get_name() == find;
(Disclaimer: Pointers in C++ is a VERY popular topic and so I'm compelled to believe that someone before me has already raised this point. However, I wasn't able to find another reference. Please correct me and feel free to close this thread if I'm wrong.)
I've come across lots of examples that distinguish between pointer to first element of array and pointer to the array itself. Here's one program and its output:
//pointers to arrays
#include <iostream>
using namespace std;
int main() {
int arr[10] = {};
int *p_start = arr;
int (*p_whole)[10] = &arr;
cout << "p_start is " << p_start <<endl;
cout << "P_whole is " << p_whole <<endl;
cout << "Adding 1 to both . . . " <<endl;
p_start += 1;
p_whole += 1;
cout << "p_start is " << p_start <<endl;
cout << "P_whole is " << p_whole <<endl;
return 0;
}
Output:
p_start is 0x7ffc5b5c5470
P_whole is 0x7ffc5b5c5470
Adding 1 to both . . .
p_start is 0x7ffc5b5c5474
P_whole is 0x7ffc5b5c5498
So, as expected, adding 1 to both gives different results. But I'm at a loss to see a practical use for something like p_whole. Once I have the address of the entire array-block, which can be obtained using arr as well, what can I do with such a pointer?
For single arrays, I don't think there's much point to it. Where it becomes useful is with multi-dimensional arrays, which are arrays of arrays. A pointer to one of the sub-arrays is a pointer to the row, and incrementing it gets you a pointer to the next row. In contrast, a pointer to the first element of the inner array is a pointer to a single element, and incrementing it gets you the next element.
int (*)[10] is a "stronger" type than int* as it keeps size of the array,
so you may pass it to function without passing additional size parameter:
void display(const int(*a)[10]) // const int (&a)[10] seems better here
{
for (int e : *a) {
std::cout << " " << e;
}
}
versus
void display(const int* a, std::size_t size) // or const int* end/last
{
for (std::size_t i = 0; i != size; ++i) {
std::cout << " " << a[i];
}
}
What i am implementing is a bit more complicated so i have stripped down the code and made the simplest example possible that replicates the problem:
I have lists A and B, at some point i need to move 1 element from list A to list B. This object memorizes its position (iterator) on the list that is currently on. It works perfectly well for 1 list, inserting and erasing. Nevertheless, when change list B inside an iterative cycle of list A, i found that the order in which i execute the insert and erase instructions determines if i get an seg fault. Here is the code:
typedef struct AO_ {
int id;
list<AO_*>::iterator thispos;
} AnObject;
class MyList {
public:
list<AnObject*> ll;
int sizell;
MyList(){ sizell=0; }
void insert(AnObject* AnObjecti){
AnObjecti->thispos= ll.insert(ll.end(),AnObjecti);
sizell++;
}
list<AnObject*>::iterator remove(AnObject* AnObjecti){
return ll.erase(AnObjecti->thispos);
}
void print(){
cout << "contents:";
list<AnObject*>::iterator itAux;
for (itAux=ll.begin(); itAux!=ll.end(); itAux++)
cout << " " << (*itAux)->id;
cout << endl;
}
};
int main(int argc, char *argv[]){
MyList* listA= new MyList();
MyList* listB= new MyList();
AnObject* objAux= new AnObject();
for(int i=0; i<10; i++){
objAux= new AnObject();
objAux->id= i;
listA->insert(objAux);
}
cout << "list A:" << endl; listA->print();
list<AnObject*>::iterator it= listA->ll.begin();
while(it!=listA->ll.end()){
objAux= (*it);
if(objAux->id==2){
//listB->insert(objAux); //***THIS CAN GO HERE (seg fault on next cycle)
it= listA->remove(objAux);
listB->insert(objAux); //***OR THIS CAN GO HERE (all ok)
}
else
++it;
}
cout << "list A:"; listA->print();
cout << "list B:"; listB->print();
}
and the output:
list A:
contents: 0 1 2 3 4 5 6 7 8 9
list A:contents: 0 1 3 4 5 6 7 8 9
list B:contents: 2
I get a seg fault if exchange the instructions marked by *
Does someone know why?
Thanks in advance
jose
The call to listB->insert modifies objAux->thispos to refer to the new list. Then the call to listA->remove will try to use that iterator in its call to list::erase. Passing an iterator from the wrong container to erase gives undefined behaviour.
I am working on a Polynomial class which uses the STL linked list. One of the functions requires me to add two Polynomial's together. For some reason, the += operator seems to be duplicating the node, as opposed to merely modifying the contents.
Here is the class declaration:
class Polynomial
{
public:
Polynomial(pair<double,int>); //Specified constructor
void add(const Polynomial&);
void print();
private:
Polynomial(); //Default constructor
list<pair<double,int> > terms;
};
This is the add member function:
void Polynomial::add(const Polynomial& rhs)
{
list<pair<double,int> >::const_iterator r;
list<pair<double,int> >::iterator l;
for(r=rhs.terms.begin(); r!=rhs.terms.end(); r++)
{
bool match=0;
//Check to see if we have an existing nth order node
for(l=terms.begin(); l!=terms.end(); l++)
{
//If we do, just add the coefficients together
if(l->second == r->second)
{
l->first += r->first;
match = 1;
}
}
//If there was no matching existing node, we need to find out
//where to insert it into the list.
if(!match)
{
l=terms.begin();
bool inserted=0; //Sentinel for the loop
while(l!=terms.end() && !inserted)
{
//If there's only one term in the list
//Just compare and stick it in front or behind the existing node
if(terms.size()==1)
{
int this_exp = l->second;
int exp_to_ins = r->second;
if(exp_to_ins > this_exp) terms.push_back((*r));
if(exp_to_ins < this_exp) terms.push_front((*r));
inserted = 1;
}
//If there's more than one node, we need to traverse the list
if(terms.size()>1)
{
if(l!=terms.begin())
{
int this_exp = l->second;
l++;
int next_exp = l->second;
int exp_to_ins = r->second;
//If the new node value is between the current and next node
//Insert between them.
if((this_exp < exp_to_ins) && (exp_to_ins < next_exp))
{
terms.insert(l,(*r));
inserted = 1;
}
}
else if(l==terms.begin())
{
int this_exp = l->second;
int exp_to_ins = r->second;
//This will be the smallest order node
//Put it in the top spot
if(this_exp > exp_to_ins)
{
terms.push_front((*r));
inserted = 1;
}
l++;
}
}
}
//If we've traversed the list and can't find the right place
//this must be the greatest order node in the list
//so just tack it on the end.
if(!inserted) terms.push_back((*r));
}
}
}
Works fine with ordering the nodes in the correct order, but we have an existing nth order node, rather than just adding the coefficients together, it keeps the original node but seems to make a second node with the coefficients added together, and I have no idea why.
If I run the print function, for what should be F(x) = -2x^7 + 3x^6 - 11x^5 - 2x^4, instead I get F(x) = -2x^7 + 3x^6 - 11x^5 - 10x^5. If I call the size() function on the list, I get 4. But if I run the following code to print out the info from the nodes in the list:
stringstream test;
for(i=terms.end(); i!=terms.begin(); i--)
{
test << "Coefficient: " << i->first << " ";
test << "Exp: " << i->second << endl;
}
cout << "Size: " << terms.size() << endl;
cout << test.str();
The following is output:
Coefficient: -10 Exp: 5
Coefficient: -2 Exp: 7
Coefficient: 3 Exp: 6
Coefficient: -11 Exp: 5
Any help greatly appreciated.
EDIT: This is the test program.
Polynomial p(pair<double, int>(-10, 5));
p.add(Polynomial(pair<double,int> (-2,4)));
p.add(Polynomial(pair<double,int> (3,6)));
p.add(Polynomial(pair<double,int> (-2,7)));
p.add(Polynomial(pair<double, int> (-1,5)));
Your add() function seems to be correct except the print:
for(i=terms.end(); i!=terms.begin(); i--)
{
test << "Coefficient: " << i->first << " ";
test << "Exp: " << i->second << endl;
}
This is completely wrong, and invokes undefined behavior. i is initially terms.end() and you've dereferencing it? items.end() returns past-the-end iterator. Even if I assume it correct for a while, the condition i!=terms.begin() means the first element is never printed!
So the fix is this:
for(list<pair<double,int> >::iterator i=terms.begin(); i!=terms.end(); i++)
{
test << "Coefficient: " << i->first << " ";
test << "Exp: " << i->second << endl;
}
And it prints expected output:
Size: 4
Coefficient: -2 Exp: 4
Coefficient: -11 Exp: 5
Coefficient: 3 Exp: 6
Coefficient: -2 Exp: 7
Is it not correct?
See the output yourself here also : http://www.ideone.com/p8mwJ
By the way, instead of add, you could make it operator+= instead, as:
const Polynomial& operator+=(const Polynomial& rhs)
{
//same code as before
return *this;
}
If you write so, then you can add polynomials as:
Polynomial p(pair<double, int>(-10, 5));
p += Polynomial(pair<double,int> (-2,4));
p += Polynomial(pair<double,int> (3,6));
p += Polynomial(pair<double,int> (-2,7));
p += Polynomial(pair<double, int> (-1,5));
Demo : http://www.ideone.com/aA1zF
I just read your comment, and came to know that you want to print it in reverse order, in that case, you could use rbegin() and rend() instead of begin() and end() as:
for(list<pair<double,int> >::const_reverse_iterator i=terms.rbegin();
i!=terms.rend();
i++)
{
test << "Coefficient: " << i->first << " ";
test << "Exp: " << i->second << endl;
}
I would also advice you to make print a const function as :
void print() const
//^^^^ this makes the function const!
Better yet overload operator<< .
Anyway reverse order printing demo : http://www.ideone.com/Vk6XB
Your test loop (the one printing in the stringstream) is incorrect: it's undefined behavior to dereference the end () iterator. Probably your "std::list" is implemented in a circular way (i.e. with begin == end+1) so dereferencing "end" gives you *begin in your test loop.
Use reverse iterators to print the list in reverse order:
for (i = list.rbegin (); i != list.rend (); ++i)
{
test << "Coefficient: " << i->first ; // etc.
}
Besides the problem pointed out by #Nawaz, there is also a problem in the Polynomial::add function.
If the if(terms.size()==1) block is executed, a new item is inserted in the list. But that increases the size of the list by one, so the if(terms.size()>1) block will also be executed. And this can insert the same node once more.
A bit further in the while loop, you increment l, and proceed using the next node, without checking whether it's valid (ie. without comparing to terms.end()).
There might be more such mistakes, but these came up after a cursory glance.
I have the following functor:
class ComparatorClass {
public:
bool operator () (SimulatedDiskFile * file_1, SimulatedDiskFile * file_2) {
string file_1_name = file_1->getFileName();
string file_2_name = file_2->getFileName();
cout << file_1_name << " and " << file_2_name << ": ";
if (file_1_name < file_2_name) {
cout << "true" << endl;
return true;
}
else {
cout << "false" << endl;
return false;
}
}
};
It is supposed to be a strict weak ordering, and it's this long (could be one line only) for debug purposes.
I'm using this functor as a comparator functor for a stl::set. Problem being, it only inserts the first element. By adding console output to the comparator function, I learned that it's actually comparing the file name to itself every time.
Other relevant lines are:
typedef set<SimulatedDiskFile *, ComparatorClass> FileSet;
and
// (FileSet files_;) <- SimulatedDisk private class member
void SimulatedDisk::addFile(SimulatedDiskFile * file) {
files_.insert(file);
positions_calculated_ = false;
}
EDIT: the code that calls .addFile() is:
current_request = all_requests.begin();
while (current_request != all_requests.end()) {
SimulatedDiskFile temp_file(current_request->getFileName(), current_request->getResponseSize());
disk.addFile(&temp_file);
current_request++;
}
Where all_requests is a list, and class Request is such that:
class Request {
private:
string file_name_;
int response_code_;
int response_size_;
public:
void setFileName(string file_name);
string getFileName();
void setResponseCode(int response_code);
int getResponseCode();
void setResponseSize(int response_size);
int getResponseSize();
};
I wish I could offer my hypotesis as to what's going on, but I actually have no idea. Thanks in advance for any pointers.
There's nothing wrong with the code you've posted, functionally speaking. Here's a complete test program - I've only filled in the blanks, not changing your code at all.
#include <iostream>
#include <string>
#include <set>
using namespace std;
class SimulatedDiskFile
{
public:
string getFileName() { return name; }
SimulatedDiskFile(const string &n)
: name(n) { }
string name;
};
class ComparatorClass {
public:
bool operator () (SimulatedDiskFile * file_1, SimulatedDiskFile * file_2) {
string file_1_name = file_1->getFileName();
string file_2_name = file_2->getFileName();
cout << file_1_name << " and " << file_2_name << ": ";
if (file_1_name < file_2_name) {
cout << "true" << endl;
return true;
}
else {
cout << "false" << endl;
return false;
}
}
};
typedef set<SimulatedDiskFile *, ComparatorClass> FileSet;
int main()
{
FileSet files;
files.insert(new SimulatedDiskFile("a"));
files.insert(new SimulatedDiskFile("z"));
files.insert(new SimulatedDiskFile("m"));
FileSet::iterator f;
for (f = files.begin(); f != files.end(); f++)
cout << (*f)->name << std::endl;
return 0;
}
I get this output:
z and a: false
a and z: true
z and a: false
m and a: false
m and z: true
z and m: false
a and m: true
m and a: false
a
m
z
Note that the set ends up with all three things stored in it, and your comparison logging shows sensible behaviour.
Edit:
Your bug is in these line:
SimulatedDiskFile temp_file(current_request->getFileName(), current_request->getResponseSize());
disk.addFile(&temp_file);
You're taking the address of a local object. Each time around the loop that object is destroyed and the next object is allocated into exactly the same space. So only the final object still exists at the end of the loop and you've added multiple pointers to that same object. Outside the loop, all bets are off because now none of the objects exist.
Either allocate each SimulatedDiskFile with new (like in my test, but then you'll have to figure out when to delete them), or else don't use pointers at all (far easier if it fits the constraints of your problem).
And here is the problem:
SimulatedDiskFile temp_file(current_request->getFileName(),
current_request->getResponseSize());
disk.addFile(&temp_file);
You are adding a pointer to a variable which is immediately destroyed. You need to dynamically create your SDF objects.
urrent_request = all_requests.begin();
while (current_request != all_requests.end()) {
SimulatedDiskFile temp_file(...blah..blah..); ====> pointer to local variable is inserted
disk.addFile(&temp_file);
current_request++;
}
temp_file would go out of scope the moment next iteration in while loop. You need to change the insert code. Create SimulatedDiskFile objects on heap and push otherwise if the objects are smaller then store by value in set.
Agree with #Earwicker. All looks good. Have you had a look inside all_requests? Maybe all the filenames are the same in there and everything else is working fine? (just thinking out loud here)