What is wrong with this assignment

Hi

I just got feedback from my teacher and he gave a comment for the following piece of code:

1
2
3
4
5
6
7
void IOclass::printDice( vector<Die> diceVec)
{
    int diceToPrint[diceVec.size()];
.
.
.
}


He pretty much said that my compiler must be very forgiving to let that slide. Since the compiler doesn't know the size of diceVec and therefore can't decide the size needed to allocate the array. He also recommends that I instead should use dynamic memory allocation:

int[] diceToPrint = new int[diceVec.size()];

I'm not sure why my way shouldn't work since my compiler (GNU GCC Compiler) lets it through without warning or error. The diceVec is sent to the function and I though that vectors had this size() function so one could do things like this? I've also tried printing diceVec.size() and I get the number of elements in diceVec printed with no problems.

If I were to use the suggested dynamic way I need to delete the array diceToPrint to avoid memory leak right? When should this be done, right before I exit the function?

And this is how I would do it:
delete diceToPrint;
right?

I would ask my teacher but since it's weekend I'll try the forum out first :)
Thanks in advance!

EDIT: I should say that I tried what my teacher proposed but didn't get it to work. I tried
1
2
3
4
5
6
7
8
void IOclass::printDice( vector<Die> diceVec)
{
    int[] diceToPrint = new int[diceVec.size()];
    // ...
    // some code
    // ...
    delete diceToPrint;
}


Ironically the error I get is:
error: initializer fails to determine size of 'diceToPrint'
AND
error: type '<type error>' argument given 'delete', expected pointer
Last edited on
Declaring the size of an array at runtime like that is non-standard C++ so your teacher is right to ask you to do it differently.

You need to delete[] your array when you finish using it. In your case I would think that was before you leave the function as its a local array. And note that you need array delete rather than ordinary delete. Use the brackets [] as in delete[] diceToPrint;

I'm not sure why you don't just use another vector like this?

1
2
3
4
5
6
7
void IOclass::printDice( vector<Die> diceVec)
{
    vector<int> diceToPrint;
.
.
.
}
Last edited on
1
2
3
4
5
6
7
8
9
void IOclass::printDice( vector<Die> diceVec)
{
    int[] diceToPrint = new int[diceVec.size()];
    // ...
    // some code
    // ...
    // delete diceToPrint; // not like this
    delete[] diceToPrint; // like this
}
Thank you Galik!

the int[] declaration didn't work for some reason, but int* worked?!
1
2
3
4
5
6
7
8
9
void IOclass::printDice( vector<Die> diceVec)
{
    //int[] diceToPrint = new int[diceVec.size()]; // this didn't work 
    int* diceToPrint = new int[diceVec.size()]; // had to do it like this, how come?
    // ...
    // some code
    // ...
    delete[] diceToPrint; // like this
}


about using another vector. You are right I could (maybe even should) use a vector but it's an assignment that has grown over the course and I begun with only arrays until now (since arrays can't take objects from what I understand) and haven't changed every array to vector.

Declaring the size of an array at runtime like that is non-standard C++

Oh I thought it had something to do with my use of size(), but its just that I let the size of the array depend on earlier things in the program (which could be different from each run). If I used vectors instead like you said I could go around the problem by using (for example): diceToPrint.push_back(...); when I populate the vector.

Last edited on
Declaring the size of the array like that is invalid C++, but since a lot of C++ compilers use a C compiler to help them, it works (that code *is* valid C99). However since you are in C++ you shouldn't do that.

Arrays can contain whatever type you want. A vector is usually the better option though.
Last edited on
 
int* diceToPrint = new int[diceVec.size()]; // had to do it like this, how come? 


Because this...

 
int[] diceToPrint = new int[diceVec.size()];


...is Java... ;o)
Arrays can contain whatever type you want. A vector is usually the better option though.

Ah thanks.


Because this...



int[] diceToPrint = new int[diceVec.size()];



...is Java... ;o)

haha, yeah that would explain it :)

Thank you for your help. I always learn something new from this forum :D
Topic archived. No new replies allowed.