My problem is that I keep a gettting a segmentation fault when I run the destructor for the PriceFeed object. It occurs on the following line: "delete [] _inputRecords[i];". I can tell because it runs fine after I comment out this line. Can someone please help? THANK YOU IN ADVANCE!!!!
I think it's because if you use delete [] you can only delete the whole thing and you can't delete a part of it like an array.
So only delete [] _inputRecords works and not delete [] _inputRecords[i]
#include <iostream>
#include <string>
usingnamespace std;
int main ( int iArgc, char ** ppcArgv ) {
int counter = 0;
int dim1 = 5;
int dim2 = 10;
int **k = newint* [5];
for( int m = 0; m < dim1; m++ ) {
k[ m ] = newint[ dim2 ];
for( int p = 0; p < dim2; p++ ) {
k[ m ][ p ] = counter++;
printf( "k[ %d ][ %d ] = %d\n", m, p, k[ m ][ p ] );
} }
for( int m = 0; m < dim1; m++ ) {
delete[] k[ m ];
printf( "deleting pointers \n");
}
delete[] k;
return( 0 );
}
Yes, you can only delete[] entire arrays. Passing a pointer that doesn't point to the beginning of an array has undefined behavior.
EDIT: Oh, but line 23 in the last example is indeed deleting an entire array. bluezor really needs to pay more attention.
The problem is line 67 in priceFeed.cpp. Assigning pointers without transferring ownership is just asking for trouble. You need to properly allocate the array for the new object and copy the data between the arrays.
line 23 is in my toy example that works perfectly fine. I posted it as a counter-example to what bluezor said ... which is that the following pointer to pointers:
You're doing it fine. bluezor misread delete [] _inputRecords[i] as delete [] _inputRecords+i. That would have been invalid.
Back to your original question,
The problem is line 67 in priceFeed.cpp. Assigning pointers without transferring ownership is just asking for trouble. You need to properly allocate the array for the new object and copy the data between the arrays.
I deleted the assignment operator altogether from the class and the implementation. Still getting the same error and when I comment that line (priceFeed.cpp line 47) it goes away. Suggestions?
Helios is correct. You need to allocate the memory for inputRecords in your assignment operator, then copy the data from rhs. With the way it is currently implemented, every assignment will eventually result in calling delete an additional time for the same pointer. That is what is causing the segfault.
Why not just use vectors and avoid the new/delete brain damage?
How exactly do I do this? I am assuming that commenting out the assignment operator's declaration and implementation isn't enough ... what should I do? and btw, I don't think I have a copy constructor - should add one?
It's because there's a default assignment operator defined by the compiler which does exactly what yours does. If you declare yours as private, the compiler can't define its own and you can't make assignments to objects of that type.
Why are you disabling the assignment operator? PanGalactic and I already told what's wrong.
Further, I removed the assignment operator and copy constructor from private and implemented them with the pasted code below. I still got the same error. PLEASE HELP!
I tried making the changes and am still stuck (going on four days now). Any thoughts on what could be causing the segmentation fault issue?
If needed, I can update what the original four files (i.e. priceFeed.h, priceFeed.cpp, priceRecord.h and priceRecord.cpp) look like after revising the class declaration and implementation using the changes that helios and PanGalactic indicated.
UPDATED FILES - the problem occurs in line 48 of priceFeed.cpp (in the destructor for the priceFeed object)
Original Problem
My problem is that I keep a gettting a segmentation fault when I run the destructor for the PriceFeed object. It occurs on the following line: "delete [] _inputRecords[i];". I can tell because it runs fine after I comment out this line. Can someone please help?