Thread: array of pointers issue

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    array of pointers issue

    I'm getting an error saying "Debug Assertion Failed!" whenever i try to run this program in VC++ :

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    #include "employee.h"
    
    int main(void)
    {
    	Employee *emp[1];
    
    	emp[0] = new Employee("firstname", "lastname", 9090);
    
    	cout << emp[0]->getSalary();
    
    	delete [] *emp;
    
    	return 0;
    }
    getSalary :
    Code:
    int Employee::getSalary() const
    {
    	return salary;
    }
    Why am i getting this error? and how do i fix it?

    note : this is a small sample to show the problem.

    *EDIT* btw , the class works fine with objects and pointer to object. So i'm guessing its the way i used the array.


    your help is appreciated
    Last edited by Brain Cell; 03-27-2005 at 11:25 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You should remove the "[]" in the call to delete since you didn't allocate an array.
    If that's not it then the problem is in your Employee class.

    gg

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    hmm... i replaced it with :
    Code:
    delete *emp;
    and it worked fine .

    why should it be 'delete *emp' and not 'delete emp' (as in a regular pointer) in order for it to work?
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Brain Cell
    hmm... i replaced it with :
    Code:
    delete *emp;
    and it worked fine .

    why should it be 'delete *emp' and not 'delete emp' (as in a regular pointer) in order for it to work?
    You allocated space for an Employee and initialized it, and set emp[0] equal to its address.

    Therefore, the following is appropriate:

    Code:
    emp[0] = new Employee("firstname", "lastname", 9090);
    
    
    delete emp[0];
    Now, emp[0] means the same thing as *(emp + 0), so delete emp[0] is the same as delete *emp

    delete emp doesn't work since emp is a statically allocated array of pointers (not something that you got from new).

    Regards,

    Dave
    Last edited by Dave Evans; 03-27-2005 at 11:57 AM.

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    What if i wanted to delete employees in an array that has more than one point to object? shall i do this for example ? :
    Code:
    for(int i=0; i < SizeOfArray; i++)
       delete emp[i];
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    yes--if they are allocated with the new operator. new and delete go hand in hand: when you use new, you use delete.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Brain Cell
    shall i do this for example ? :
    Code:
    for(int i=0; i < SizeOfArray; i++)
       delete emp[i];
    Sure, you can do this but only if you allocated in such way.
    For example:
    Code:
    ...
    Employee *emp[N]; //array of N (const ineteger) pointers
    ...
    
    for ( int i = 0; i < N; i++)
    {
    	emp[i] = new Employee(...);
    }
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  8. #8
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    ok Thanks Codeplug , Dave Evans , 7stud and Micko
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm getting an error saying "Debug Assertion Failed!" whenever i try to run this program in VC++ :
    For future reference, this is usually an indication that you've done something wrong with your memory in some way, as is the case here.

    >>Employee *emp[1];
    I really don't know why you're creating an array of 1 Employee. Unless it's just a quick pre-hundred-lines-of-code test that you plan to change later, you might as well create an ordinary Employee object.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. how do i declare array of pointers to base clase reobject
    By icantprogram in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2002, 02:30 AM