Home > Software engineering >  Question on dynamic allocation in vectors
Question on dynamic allocation in vectors

Time:12-04

#include <iostream>
#include <vector>
#include "malloc.h"

using namespace std;

int main() {
    // Write C   code here
    vector<vector<vector<int*>>> storage;
    for (int i=0; i< 13; i  )
    {
        storage.push_back(vector<vector<int*>>()); 
        for (int j=0; j< 13; j  )
        {
            storage[i].push_back(vector<int*>());

            storage[i][j].push_back((int*)malloc(5 * sizeof(int)));
               
            for (int k =0; k<4; k  )
            {
                storage[i][j][k]=k;
            }
        }
    }
    
    return 0;
}

I am trying to dynamically allocate a list inside the innermost dimension of the last vector, but it turns out it throws some compilation error when I try to set a value to the vector:

error: invalid conversion from 'int' to '__gnu_cxx::__alloc_traits<std::allocator<int*>, int*>::value_type' {aka 'int*'} [-fpermissive]

CodePudding user response:

I assume you're trying to index into the C-style array, in which case you would have to cut out one dimension of the vector, like so:

vector<vector<int*>> storage;

Then you can index into the C-style array as expected.

storage[i][j] currently accesses a vector of int*, in which you have only pushed one element. If you want to keep the rest of your code the same you could just do storage[i][j][0][k]=k, however, I would advise removing a dimension instead.

CodePudding user response:

The error message is telling you that you are trying to assign an int to an int*. Specifically, on this statement:

storage[i][j][k]=k;

storage[i][j][k] returns (a reference to) an int*, but k is an int.

Since you have 3 levels of vectors containing an int[] array, you need 4 loops to initialize the individual ints, but you only have 3 loops, so add another loop:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<vector<vector<int*>>> storage;
    for (int i = 0; i < 13;   i)
    {
        storage.push_back(vector<vector<int*>>()); 

        for (int j = 0; j < 13;   j)
        {
            storage[i].push_back(vector<int*>());

            for(int k = 0; k < N;   k) // <-- decide what N should be!
            {
                storage[i][j].push_back(new int[5]);
               
                for (int m = 0; m < 5;   m)
                {
                    storage[i][j][k][m] = k;
                }
            }
        }
    }
    
    // don't forget to delete[] all of the new[]'ed arrays!
    // consider using either std::unique_ptr<int[]> or
    // std::array<int,5> instead of int* ...

    return 0;
}

I would suggest simplifying the code to make it more readable, eg:

#include <iostream>
#include <vector>
#include <array>
using namespace std;

using arr5Ints = array<int, 5>;
using vec1D_arr5Ints = vector<arr5Ints>;
using vec2D_arr5Ints = vector<vec1D_arr5Ints>;
using vec3D_arr5Ints = vector<vec2D_arr5Ints>;

int main() {
    vec3D_arr5Ints storage(
        13,
        vec2D_arr5Ints(
            13,
            vec1D_arr5Ints(N) // <-- decide what N should be!
        )
    );

    for (auto &vec2d : storage)
    {
        for (auto &vec1d : vec2d)
        {
            for(auto &arr : vec1d)
            {
                int k = 0;
                for (int &i : arr)
                {
                    i = k  ;
                }
            }
        }
    }

    return 0;
}
  • Related