This is a C++ issue rather than a QT issue in my view hence the post here.
When I run the functions below I get
"Program received signal SIGABRT, Aborted.
free(): invalid next size (fast): 0x083101b0 ***"
plus a hundred or so lines of related errors.
I believe I am deleting an invalid pointer but cannot see where I am going wrong. What I'm trying to do is write two functions that gathers the non empty and non txt containing cells from my spreadsheet selected range then sums the numbers and puts the answer in the cell below the selected range. If there is only one number the functions work, if the range has two or more cells than I get the crash above. Can anyone help sort this out?
void Spreadsheet::sumColumn() // sums selected cells and dumps answer below lowest cell
{
int col=currentColumn(); //get current column position
int rowcount= range.rowCount(); // work out number of rows selected
int *number= new int; // create pointer to number of items to be collected
double *dataSet = new double; // create pointer for array of data to be collected
double total=0; // initialise total
collectData(number, dataSet); // get data from sheet see next function
int numberOfItems=0; // set this to zero just in case
numberOfItems=(int) *number; // cast to make numberOfItems = number of filled cells from collect function
for (int n=0; n<numberOfItems; n++)
total= total+ dataSet[n]; // calculate sum
QString num
= QString::number(total
);
// converts any number type to string -very easy!
setFormula(rowcount, col, num); // put sum on sheet in cell below selected range this works
// in future preferably needs check for overwriting of cell if not empty
// also needs to add row if all rows in sheet are selected
delete number; // clean up memory
delete dataSet;
}
void Spreadsheet::sumColumn() // sums selected cells and dumps answer below lowest cell
{
QTableWidgetSelectionRange range = selectedRange(); // get selected range
int col=currentColumn(); //get current column position
int rowcount= range.rowCount(); // work out number of rows selected
int *number= new int; // create pointer to number of items to be collected
double *dataSet = new double; // create pointer for array of data to be collected
double total=0; // initialise total
collectData(number, dataSet); // get data from sheet see next function
int numberOfItems=0; // set this to zero just in case
numberOfItems=(int) *number; // cast to make numberOfItems = number of filled cells from collect function
for (int n=0; n<numberOfItems; n++)
total= total+ dataSet[n]; // calculate sum
QString num = QString::number(total); // converts any number type to string -very easy!
setFormula(rowcount, col, num); // put sum on sheet in cell below selected range this works
// in future preferably needs check for overwriting of cell if not empty
// also needs to add row if all rows in sheet are selected
delete number; // clean up memory
delete dataSet;
}
To copy to clipboard, switch view to plain text mode
void Spreadsheet::collectData(int *count, double *data)
{
QList<QTableWidgetItem *> items = selectedItems();
int totalcount=0;
{
bool ok;
double value = item->text().toDouble(&ok);
if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in
{
//data[totalcount]=0.0; // set each element to zero just in case
data[totalcount]=value; // now fill array with values from sheet
totalcount++; //increment count for only filled cells with numbers in
}
}
*count=totalcount; // set pointer to total count
}
void Spreadsheet::collectData(int *count, double *data)
{
QList<QTableWidgetItem *> items = selectedItems();
int totalcount=0;
foreach (QTableWidgetItem *item, items) // loop through sheet selected items
{
bool ok;
double value = item->text().toDouble(&ok);
if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in
{
//data[totalcount]=0.0; // set each element to zero just in case
data[totalcount]=value; // now fill array with values from sheet
totalcount++; //increment count for only filled cells with numbers in
}
}
*count=totalcount; // set pointer to total count
}
To copy to clipboard, switch view to plain text mode
Bookmarks