Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Debug assertion failed in tree implementation

Reply
Thread Tools

Debug assertion failed in tree implementation

 
 
David Kevin
Guest
Posts: n/a
 
      05-05-2012
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-05-2012
On 05.05.2012 17:17, David Kevin wrote:
> 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


 
Reply With Quote
 
 
 
 
David Kevin
Guest
Posts: n/a
 
      05-05-2012
On 5 Maj, 20:51, Paavo Helde <myfirstn...@osa.pri.ee> wrote:
"Alf P. Steinbach" <alf.p.steinbach+use...@gmail.com> wrote in
>> recursion(fixedDepth);

> 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:

>>> *it->vecA=

>> Here you have a problem.

>No, this works fine, even if a bit inefficient.


What is a more efficient way to do that?

Greetings.

 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      05-06-2012
David Kevin <> writes:
> 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

--
Egotist, n. A person of low taste, more interested in himself than in me.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debug assertion failed str!=NULL fprintf.c Arti Potnis C++ 2 09-02-2004 05:05 PM
Debug Assertion Failed! and no obvious Error Kapt. Boogschutter C++ 1 06-23-2004 03:27 PM
debug assertion failed w3r3w0lf C++ 2 04-15-2004 05:21 AM
Student requires urgent help! Debug Assertion Failed error Cormac C++ 3 04-06-2004 08:44 PM
Debug Assertion Failed! ARGH! NOT AGIAN!! lol SorceCode C++ 2 01-26-2004 03:52 PM



Advertisments