Thread: problem - pointer arrays to class

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    problem - pointer arrays to class

    Hi All,
    please can you help me with this? The program works but when I'm exiting the program I'm getting 'assertion error'.

    example:
    Code:
    class A
    {
    public:
        A(int a, int b);   //I'm using one class from Qt that does not have empty constructor
    
    private:
          int a,b;
    }
    now I want to create 5 pointers of this class in a separate class
    Code:
    class JustATest
    {
    private:
       A *pClassA[4];
    
    public:
       JustATest();
       void startA() {
                              for (int i=0; i < 5; i++)
                                  pClassA[i] = new A(i,55);
                           }
        ~JustATest() {delete [] pClassA}
    }
    Is the problem in the destructor? I can not use vector because of I have more than one input variables to the class methods.

    Any idea would be more than helpful

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    I don't see any assert, so this can't be the relevant code.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    ok,...maybe I was wrong with the error description. This is the error I'm getting:

    *** glibc detected *** /media/Data/01SourceCode/VFST/bin/OCD: free(): invalid pointer: 0x09251434 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb53b9604]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb53bb5b6]
    .
    .
    .
    .
    .
    there is a lot more lines (didn't post them)...

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Code:
    A *pClassA[4];
    How many elements do you think this array can hold ?
    Code:
    for (int i=0; i < 5; i++)
        pClassA[i] = new A(i,55);
    Surely not 5 ?
    delete [] pClassA
    Also, since your allocating memory 5 times, you have free it 5 times as well.

    Code:
    for(int i =0; i < 5; i++)
    	delete pClassA[i];
    Spidey out!

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by miroslav_karpis View Post
    ok,...maybe I was wrong with the error description. This is the error I'm getting:

    *** glibc detected *** /media/Data/01SourceCode/VFST/bin/OCD: free(): invalid pointer: 0x09251434 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb53b9604]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb53bb5b6]
    .
    .
    .
    .
    .
    there is a lot more lines (didn't post them)...
    Ok that hasn't got anything to do with assert. To solve that problem look at the post above.

  6. #6
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Ok that hasn't got anything to do with assert. To solve that problem look at the post above.
    Yeah, although I think I know what he/she is talking about, Visual Studio spits out a cryptic error when you have memory leaks which says
    Debug assertion failed: _BLOCK_TYPE_IS_VALID(phead->nBlockUse)
    So, Im guess the OP was talking about that, heres what I mean - http://msdn.microsoft.com/en-us/libr...40(VS.60).aspx
    Spidey out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM