jeudi 20 septembre 2018

convert a double manually to a cstring

Im trying to resize a dynamically allocated array of ItemInfo objects. However Im certain im doing it wrong because after I delete [] line the temp stops working. I know the rule of thumb is to have a delete for every new but i think I need to not delete [] list for this to work.

    if (objectSize >= objectCapaticy) {
                objectCapaticy *= 2;                             //double capacity
                ItemInfo *temp = new ItemInfo[objectCapaticy];   //new bigger array
                for (int i = 0; i < objectSize; i++) {           //fill temp w/ list
                    char tempVal[40];
                    char *tempPtr = &tempVal[0];

                    stuDblToCstr(tempPtr, list[i].getItemId());
                    temp[i].setItemId(tempPtr);
                    stuDblToCstr(tempPtr, list[i].getManCost());
                    temp[i].setManCost(tempPtr);
                    stuDblToCstr(tempPtr, list[i].getSellPrice());
                    temp[i].setSellPrice(tempPtr);
                    temp[i].setDescription(list[i].getDescription());

                }
                delete [] list;                     //delete list

                for (int i = 0; i < objectSize; i++) {    //fill list w/ temp
                    char *tempVal;
                    stuDblToCstr(tempVal, temp[i].getItemId());
                    list[i].setItemId(tempVal);
                    stuDblToCstr(tempVal, temp[i].getManCost());
                    list[i].setManCost(tempVal);
                    stuDblToCstr(tempVal, temp[i].getSellPrice());
                    list[i].setSellPrice(tempVal);
                    list[i].setDescription(temp[i].getDescription());
                }
            }

Aucun commentaire:

Enregistrer un commentaire