0
#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]

14
  • If you are asking what the error message means, please paste the full message into your question. I suspect that your error has something to do with the difference between int and int*. Commented Dec 3, 2021 at 20:40
  • 4
    I am trying to dynamically allocate a list inside the innermost dimension of the last vector Don't, just use another vector and let it handle the memory management for you. Commented Dec 3, 2021 at 20:42
  • 2
    Whenever you feel the need to use a C-style cast (like you do with (int*)malloc(...)) you should take that as a sign that you're doing something wrong. Commented Dec 3, 2021 at 20:46
  • 1
    Do you want a 3D or a 4D array? If you want 4D, you have too few nested loops and too few levels of []. If you want 3D, you have one layer of arrays too many. (In this case I recommend removing * and malloc. By the way malloc.h is a wrong header, you need stdlib.h) The fact that you have storage[i] and storage[i][j] at the same indentation level right next to each other is a hint that something fishy is going on. Commented Dec 3, 2021 at 20:52
  • 1
    It seems rather unusual that you are permitted to use std::vector for most of your object's dimensions, but are required to use the very C++ unfriendly malloc for exactly one of the dimensions. Commented Dec 3, 2021 at 21:26

2 Answers 2

1

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; } 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thanks. But in the code I pasted, doing this gives a segmentation fault. Looks like I'm missing something else too.
I've corrected my answer! ^^

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.