Debug assertion failed in tree implementation

Discussion in 'C++' started by David Kevin, May 5, 2012.

  1. David Kevin

    David Kevin Guest

    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.
    David Kevin, May 5, 2012
    #1
    1. Advertising

  2. 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
    Alf P. Steinbach, May 5, 2012
    #2
    1. Advertising

  3. David Kevin

    David Kevin Guest

    On 5 Maj, 20:51, Paavo Helde <> wrote:
    "Alf P. Steinbach" <> 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.
    David Kevin, May 5, 2012
    #3
  4. David Kevin

    Miles Bader Guest

    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.
    Miles Bader, May 6, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SorceCode
    Replies:
    2
    Views:
    3,361
    Christian Jan├čen
    Jan 26, 2004
  2. Cormac
    Replies:
    3
    Views:
    2,571
    Kevin Goodsell
    Apr 6, 2004
  3. w3r3w0lf

    debug assertion failed

    w3r3w0lf, Apr 15, 2004, in forum: C++
    Replies:
    2
    Views:
    2,557
    John Harrison
    Apr 15, 2004
  4. Kapt. Boogschutter

    Debug Assertion Failed! and no obvious Error

    Kapt. Boogschutter, Jun 23, 2004, in forum: C++
    Replies:
    1
    Views:
    594
    red floyd
    Jun 23, 2004
  5. Arti Potnis
    Replies:
    2
    Views:
    6,686
    Howard
    Sep 2, 2004
Loading...

Share This Page