-1

So, I wrote a function which resizes a Two-Dimensional array.

void resizeArray(int **&arr, short &arrSize1, short* &arrSize2, const int &amount)
{
    int** arrTemp = new int*[arrSize1 + 1]; // Creating a 2D array
    for (short i(0); i < arrSize1 + 1; i++) // For each arrTemp[i] an array is created
        arrTemp[i] = new int[(arrSize2[i] == 0 ? 1 : arrSize2[i])];

    for (short i(0); i < arrSize1; i++) // Copying old values to the new BIGGER temp array
        for (short i2(0); i2 < (arrSize2[i] == 0 ? 1 : arrSize2[i]); i2++)
            arrTemp[i][i2] = arr[i][i2];

    for (short i(0); i < arrSize1; i++)
        delete[] arr[i];
    if (arrSize1) // The first time this function is ran arrSize1 is 0 and arr is a nullptr
    {
        delete[] arr;
        arr = nullptr;
    }

    arr = new int*[arrSize1 + 1]; // Creating the new BIGGER arr
    for (short i(0); i < arrSize1 + 1; i++) // Creating the sub-arrays(or second dimension)
        arr[i] = new int[(arrSize2[i] == 0 ? 1 : arrSize2[i])];

    for (short i(0); i < arrSize1 + 1; i++) // Copying from arrTemp to arr
        for (short i2(0); i2 < (arrSize2[i] == 0 ? 1 : arrSize2[i]); i2++)
            arr[i][i2] = arrTemp[i][i2];

    for (short i(0); i < arrSize1 + 1; i++) // Deleting arrTemp
        delete[] arrTemp[i];
    delete[] arrTemp;
    arrTemp = nullptr;

    for (short i(0); i < (arrSize2[arrSize1] == 0 ? 1 : arrSize2[arrSize1]); i++)
        arr[arrSize1][i] = amount; // Setting the newly created sub-array to a value
}

The code above works perfectly - the only problem is when I run Code Analysis it says Warning (Memory Safety) C6001 - '*arrTemp[i]' is not initialized.

I don't quite understand why it thinks that it isn't initialized - I also have function like this for 3D arrays(of course everything with dynamic memory allocation) and it doesn't show any problem there.

I tried fixing it several times, but no avail: 1. Initializing to nullptr after the new int*[] is done; 2. Initializing the values of the 2D array to 0 after the loop is done; 3. A few other stupid things.....

So, is it bug in Visual Studio(which I'm using by the way :)) or is it something in my code that I can't find out?

P.S. Added comments;

Nick Dawn
  • 3
  • 4

1 Answers1

0

Your code generates this warning because when you build your arrTemp you allocate arraySize1 + 1 sub-arrays

for (short i(0); i < arrSize1 + 1; i++)
    arrTemp[i] = new int[(arrSize2[i] == 0 ? 1 : arrSize2[i])];

but assign meaningful values only to arraySize1 of those sub-arrays

for (short i(0); i < arrSize1; i++)
    for (short i2(0); i2 < (arrSize2[i] == 0 ? 1 : arrSize2[i]); i2++)
        arrTemp[i][i2] = arr[i][i2];

After that all entries like arrTemp[arrSize1][i2] still contain garbage.

Later you do

for (short i(0); i < arrSize1 + 1; i++)
    for (short i2(0); i2 < (arrSize2[i] == 0 ? 1 : arrSize2[i]); i2++)
        arr[i][i2] = arrTemp[i][i2];

which attempts to read garbage from arrTemp[arrSize1][i2]. Hence the warning.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    Actually, he doesn't initialize anything except the loop counters. There are only assignments here. – Ben Voigt Jun 25 '15 at 19:42
  • @AnT -> After a bit of thinking I finally found out what was wrong - it was that the newly created sub-array contained garbage which it copied to arr. The rest of the array doesn't contain garbage(it basically can't or the program will assert) - the function that calls this one assures that new values WILL be set to the new sub-array(Hexadecimal data from file in my case). – Nick Dawn Jun 26 '15 at 17:52
  • @Nick Dawn: Which is exactly what it says in my answer: `arrTemp[arrSize1]` sub-array contains garbage which is copied to `arr`. – AnT stands with Russia Jun 26 '15 at 17:53
  • @AnT -> BTW the new sub-array's garbage values are not used until set to something. Also I think I'm talking a bit too much :). Either way thanks for the answer - it really helped! – Nick Dawn Jun 26 '15 at 17:56
  • @NickDawn The simplest way to not get garbage values is this:`new int[whatever]();` Notice the parens at the end? That sets all of the values to their default, which for `int` is 0. – PaulMcKenzie Jun 26 '15 at 18:43
  • @PaulMcKenzie -> `The rest of the array doesn't contain garbage(it basically can't or the program will assert) - the function that calls this one assures that new values WILL be set to the new sub-array` what I want in this program(which is a DirectX game by the way) is speed, so I don't initialize them at all -> they are later set to a value. – Nick Dawn Jun 26 '15 at 18:53
  • @NickDawn If you want more speed, look at the comment in the original comment section to the coliru example. You are calling `new` to create your 2D array an unnecessary number of times. See this post: http://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048 Only 2 calls to `new` are required to build a matrix, not a call to `new` and repeatedly calling `new` again in a loop. – PaulMcKenzie Jun 26 '15 at 18:58
  • @PaulMcKenzie -> I will use that - thanks to the guy who gave me idea in the first comment section and to you! – Nick Dawn Jun 26 '15 at 19:03
  • @PaulMcKenzie -> Also I'm creating it over and over because of this `(arrSize2[i] == 0 ? 1 : arrSize2[i])` the size of a certain sub-array depends on another variable's value. – Nick Dawn Jun 26 '15 at 20:09