2

I need to dynamically resize my array by 2 every time it fills up. It starts with a capacity of 2. This is what I've done (in main.cpp), but it gives me these errors: "warning: deleting array 'test'" and "error: incompatible types in assignment of 'ItemInfo*' to 'ItemInfo [cap]'".

ItemInfo test[cap];

//I have code here to open file, read from file, etc

if(itemCount >= cap){
    cap += 2;
    ItemInfo *temp;
    temp = new ItemInfo[cap];
    for(int i = 0; i < cap; i++){
        temp[i] = test[i];
    }
    delete[] test; //error here
    test = temp;   //error here
}

This is the class (in .hpp file):

#include <iostream>
#include <iomanip>

using namespace std;

class ItemInfo {
private:
    int itemId;
    char description[40];
    double manCost;
    double sellPrice;
public:
    ItemInfo() {
        itemId = 0;
        *description = '\0';
        manCost = 0.0;
        sellPrice = 0.0;
    }
    void setItemId(const char *num);
    void setDescription(const char *cstr);
    void setManCost(const char *num);
    void setSellPrice(const char *num);
    int getItemId();
    const char *getDescription();
    double getManCost();
    double getSellPrice();
3
  • 3
    If you're allowed to use it, use std::vector. If not, look around the site a bit more and you'll find dozens of examples of how to do this with new. Commented Oct 8, 2020 at 0:14
  • As an aside, cap += 2 is an unusual reallocation strategy, since reallocation is expensive. cap *= 2 would be more common. Commented Oct 8, 2020 at 0:51
  • the assignment asks to resize by 2, just so we can make sure we do it right Commented Oct 8, 2020 at 1:08

1 Answer 1

2

The problem is that you use ItemInfo test[cap];. Using this declaration results in test being saved on the stack. Therefore, it has constant size. Using temp = new ItemInfo[cap]; dynamically allocates memory on the heap. This is the memory which you can also free. Thus, test and temp are different types and test cannot be freed. ItemInfo *test = new ItemInfo[cap]; should work for you.

Also mind that there is another small mistake: You copy the first cap many values after increasing cap where the old cap should be used.

You could use realloc too if ItemInfo is trivially copyable so that you do not need the loop to copy the data and temporarily use more memory than maybe needed. The code for using realloc would seem like

ItemInfo *test = (*ItemInfo) malloc(cap * sizeof(ItemInfo));
// use malloc here as realloc and new should not be mixed
[...]
if(itemCount >= cap){
    cap += 2;
    test = (*ItemInfo) realloc(test, cap * sizeof(ItemInfo));
}

As user4581301 mentioned you could also try to use Vectors if there is nothing specific against it.

Sign up to request clarification or add additional context in comments.

6 Comments

Prefer to use the terms Automatic and Dynamic storage instead of stack and heap. You'll probably never see it done, but C++ don't need no stinking stacks or heaps.
@user4581301 Could you expain this a bit more? I mean, on the one hand, the C++ documentation seems to prefer these words and they are more high-level. On the other hand, the underlying fundamentals are stack and heap and these words are used by many other languages (and architecture-specific and theoretical aspects) making it easier to transfer knowledge from and to these cases.
You could use realloc too -- No. If the class is not trivially-copyable, then realloc or malloc can't be used at all.
@PaulMcKenzie Sorry, I did not know that coming from C. Edited the answer, thanks for the info!
Not much to say, really. Stack and heap are implementation details and are almost always used for automatic and dynamic storage. I don't think I've seen a computer make it out of the lab that wasn't stack-and heap and I've never seen a C++ implementation with anything but stack and heap, but the C++ Standard has completely abstracted both concepts away. This leads to something interesting: The implementation detail is more widely recognized than the more general abstraction. Take that Scott McCloud.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.