943,548 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 958
  • C++ RSS
Nov 12th, 2008
0

free(): invalid pointer error need help

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gannon56789 is offline Offline
2 posts
since Nov 2008
Nov 12th, 2008
0

Re: free(): invalid pointer error need help

shouldnt you be deleting the memory allocated inside the ctor? That's a memory leak.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Nov 12th, 2008
0

Re: free(): invalid pointer error need help

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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Nov 12th, 2008
0

Re: free(): invalid pointer error need help

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.

C++ Syntax (Toggle Plain Text)
  1.  
  2. Employee* Earray[10]; //Declared private member in Payroll
  3. Payroll::Payroll()
  4. {
  5. Employee **EmpPointer;
  6. EmpPointer = new Employee *[10];
  7.  
  8. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gannon56789 is offline Offline
2 posts
since Nov 2008
Nov 12th, 2008
2

Re: free(): invalid pointer error need help

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2008
0

Re: free(): invalid pointer error need help

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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Multi-Tasks & Multi-Threads Concepts Under Windows! Help
Next Thread in C++ Forum Timeline: Circular shifts, bits distorted/missing?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC