free(): invalid pointer error need help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: Gannon56789 is an unknown quantity at this point 
Solved Threads: 0
Gannon56789 Gannon56789 is offline Offline
Newbie Poster

free(): invalid pointer error need help

 
0
  #1
Nov 12th, 2008
When i compile my code i keep getting an error. *** glibc detected *** ./main: free(): invalid pointer: 0xbfb82c34 *** . I can not figure out for the life of me what the issue is. I think it may have something to do with my constructor. Any suggestions would be appreciated.

  1. Employee* Earray[10]; // Declared as private member of payroll
  2. Payroll::Payroll()
  3. {
  4. Employee **EmpPointer;
  5. EmpPointer = new Employee*[10];
  6.  
  7. for ( int i = 0; i < 10; i++ )
  8. {
  9. Earray[i] = 0;
  10. }
  11. }
  12.  
  13. Payroll::~Payroll()
  14. {
  15. delete [] Earray;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: free(): invalid pointer error need help

 
0
  #2
Nov 12th, 2008
shouldnt you be deleting the memory allocated inside the ctor? That's a memory leak.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: free(): invalid pointer error need help

 
0
  #3
Nov 12th, 2008
What does Earray point to? You've made it to null pointer:
Earray[i] = 0 //like NULL
And your EmpPointer is unused...
Seems like to me you're trying to allocate 2D array of some sort, but doing it wrong
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: Gannon56789 is an unknown quantity at this point 
Solved Threads: 0
Gannon56789 Gannon56789 is offline Offline
Newbie Poster

Re: free(): invalid pointer error need help

 
0
  #4
Nov 12th, 2008
Ok i figured out the error, it had to do with my destructor. The program compiles now but it is not working the right way. I believe i am trying to declare a 2-D array when i want to do a 1-D one but it's the only way i could get the code to compile. For my code i am supposed to have a Payroll class that will contain a fixed size array of pointers to Employee. The array must be processed polymorphically. Constructor: Creates Payroll with no employees.
Destructor: Cleans up as the object leaves scope. The Employee class is an Abstract Base Class. Here is my constructor.

  1.  
  2. Employee* Earray[10]; //Declared private member in Payroll
  3. Payroll::Payroll()
  4. {
  5. Employee **EmpPointer;
  6. EmpPointer = new Employee *[10];
  7.  
  8. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,616
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: free(): invalid pointer error need help

 
1
  #5
Nov 12th, 2008
>Here is my constructor.
Is that the whole of your constructor? If so, it's not accomplishing much of anything as you allocate memory to the local variable EmpPointer, and then proceed to leak memory when EmpPointer goes out of scope at the end of the constructor.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: free(): invalid pointer error need help

 
0
  #6
Nov 12th, 2008
It would be much better if you post the problem statement that you're trying to solve and also the complete code or atleast all the required and relevant code.
thanks
-chandra
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC