Debug assertion failed in tree implementation

D

David Kevin

Hi,
I would be grateful if somebody could explain why following code
causes assertions failures:

//#include "stdafx.h" //neccessary if you compile with VC++ 2008
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "stdio.h"
using namespace std;

class A {
public:
A() {}
A(int currentDepth, int depth) { cout << currentDepth << " " << depth
<< "\n"; }
vector<A>* vecA;
};

vector<A> recursion(int fixedDepth) {
fixedDepth--;
getchar();
cout << "We are on level: " << fixedDepth << "\n";
vector<A> vecA;
int Max=rand() % 5+1;
for(int i=0; i<Max; i++) {
A a;
vecA.push_back(a);
}
vector<A>::iterator it;
for(it=vecA.begin(); it!=vecA.end(); it++) {
if(fixedDepth==0) {
if(it==vecA.end()) break;
it++;
continue;
}
it->vecA=new vector<A>;
*it->vecA=recursion(fixedDepth);
}
return vecA;
};

int main()
{
srand((unsigned)time(0));
vector<A> a=recursion(2);
return 0;
}

It is interesting that if one doesn't use random numbers but in for
loop there is single, known before run-time number instead of MAX it
seems to work nicely. My compiler is Visual C++ 2008.

Thanks in advance for responses,
Greetings.
 
A

Alf P. Steinbach

Hi,
I would be grateful if somebody could explain why following code
causes assertions failures:

Because it's wrong.

//#include "stdafx.h" //neccessary if you compile with VC++ 2008

No, it's not necessary. Just turn off use of precompiled headers in the
settings.

#include<iostream>
#include<cstdlib>
#include<ctime>

Just to avoid some possible grief, better use the ".h" versions of the
above headers.
#include<vector>
#include "stdio.h"
using namespace std;

class A {
public:
A() {}
A(int currentDepth, int depth) { cout<< currentDepth<< " "<< depth
<< "\n"; }
vector<A>* vecA;

Uh, oh, a pointer to a vector. Which isn't even initialized. And which
is a public data member.

You can't get it much wronger than that.

This code is doomed.

};

vector<A> recursion(int fixedDepth) {
fixedDepth--;
getchar();

What's that for?

cout<< "We are on level: "<< fixedDepth<< "\n";
vector<A> vecA;
int Max=rand() % 5+1;

Use 'const' where you can.

for(int i=0; i<Max; i++) {
A a;
vecA.push_back(a);
}
vector<A>::iterator it;
for(it=vecA.begin(); it!=vecA.end(); it++) {
if(fixedDepth==0) {
if(it==vecA.end()) break;
it++;
continue;
}
it->vecA=new vector<A>;

The above doesn't do anything, really. Except use time and invoke
possible side effects.
*it->vecA=

Here you have a problem.

I have already told you what it is.

What is it?

recursion(fixedDepth);

Here you have another problem - can you guess what it is?

}
return vecA;
};

int main()
{
srand((unsigned)time(0));
vector<A> a=recursion(2);
return 0;
}

It is interesting that if one doesn't use random numbers but in for
loop there is single, known before run-time number instead of MAX it
seems to work nicely. My compiler is Visual C++ 2008.

Hm.

Cheers & hth.,

- Alf
 
D

David Kevin

Here you have another problem - can you guess what it is?
No problem here either. He is decrementing fixedDepth (sic!) elsewhere.
Bad style again, of course.

In the fact it is extracted from a larger problem and when I have
writing
that post I was a bit tired; but what is really interesting to me and
what I
was really afraid is: is it safe to declare a pointer to a vector and
how far
I can rely on that construction?

I have realized what is the problem with it++ before continue
instruction and
when I have introduced the changes which you have suggested it begun
to work
nicely but still there are some assertion failures in larger problem.

But another thing what I would like to know is:
No, this works fine, even if a bit inefficient.

What is a more efficient way to do that?

Greetings.
 
M

Miles Bader

David Kevin said:
but what is really interesting to me and what I was really afraid is:
is it safe to declare a pointer to a vector

Of course; Alf was just being obnoxious.

But of course, using raw pointers like that puts the onus on you to
keep track of everything and make sure you're not leaking or using
dangling pointers etc. So in C++ it's often a good idea to use other
methods instead (smart pointers, references, just passing by value,
etc), and leave pointers for special circumstances (in, e.g., C
they're harder to avoid).

-miles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top