0

I am new to C++ programming, you might think its silly question but I tried everything. I want to allocate a memory using malloc for three dimensional array.

I dont want to use pointers. I want size to be fixed.

float xyz[60000][28][28];

xyz = (float ***) malloc(60000 * sizeof(float ***));
for(int i=0;  i<60000;  i++)
    {
        xyz[i]=(float **)malloc(28 * sizeof(float *));

        for(int j=0;  j<28;  j++)
        {
            xyz[i][j]=(float *)malloc(28 * sizeof(float));
        }
    }

I tried above code, but its giving me error. Incompatible types.

Any kind of suggestions are welcomed.

Sachin Bankar
  • 372
  • 6
  • 13
  • You can't do anything dynamically unless you use pointers. The way you are trying to do things does not work. – user3344003 May 17 '19 at 00:53
  • 3
    If you're using C++, why not take advantage of containers like vectors and arrays? – ggorlen May 17 '19 at 00:56
  • There are a lot of things you can do. This just isn't one of them. The reason your code doesn't work is that you have an `=` and on the left hand side you can have a variable of type `float[60000][28][28]` so on the right you'd have to have the *value* of a variable of that type, which you don't. You can't use `=` to move `xyz` to a different place in memory. – David Schwartz May 17 '19 at 00:56
  • 4
    float xyz[60000][28][28]; already allocates memory for the array – xaxxon May 17 '19 at 00:58
  • The usual: A pointer is not an array and an array is not a pointer. `std::array<>` or `std::vector<>` ftw. – Swordfish May 17 '19 at 00:59
  • @DavidSchwartz What you would suggest the solution? – Sachin Bankar May 17 '19 at 01:04
  • @xaxxon does it mean that, its already on hip memory? – Sachin Bankar May 17 '19 at 01:05
  • *I want to allocate a memory using `malloc()` for three dimensional array.* – Why?? – Swordfish May 17 '19 at 01:17
  • @Swordfish I am writing CUDA programming(learning). I need to pass the reference of the array for cudamalloc – Sachin Bankar May 17 '19 at 01:27
  • 3
    `float xyz[60000][28][28];` allocated quite a bit of storage, but be cautious with it. That's something close to 200 MB of storage. If that's a local variable on the stack, the program has almost certainly overflowed the stack. – user4581301 May 17 '19 at 01:31
  • @SachinBankar [See this answer](https://stackoverflow.com/questions/52068410/allocating-a-large-memory-block-in-c/52069368#52069368). Your current method is not good at all, as it doesn't allocate the memory block in contiguous storage, doesn't check if the memory allocation fails, etc. – PaulMcKenzie May 17 '19 at 01:33
  • If you are using `cudamalloc`, there should be no need to `malloc` anything. allocating storage is `cudamalloc`'s job. I think you may be better off explaining what the over-arching goal is. If we want to know that, we can suggest how to get you there. – user4581301 May 17 '19 at 01:34
  • 1
    @SachinBankar It depends what the actual problem is, which I don't know. You say you want to allocate memory using `malloc` for a three dimensional away (which you can't do) but you don't say *why* you want to do that. You have to tell us the problem you're trying to solve -- just telling us your proposed solution to it is not sufficient. – David Schwartz May 17 '19 at 04:26

0 Answers0