0

I am trying to use malloc to allocate the inner section of a dynamically allocated 2D array. I know that vectors and new are better but I am required to use malloc and realloc. I start off with two variables.

buffer_count = 0
  buffer_size = 3

I then create the 2D array of size [3][26](each inner array just has to be of size 26).

Here is that creation:

int ** array_creation (){
int ** arr = (int **) malloc(buffer_size * sizeof(int *));
            for(int i=0;i<buffer_size;i++){
                arr[i] = (int *)malloc(26 * sizeof(int));     
            }
}

Every time I add to the next array location I increase the buffer_count. When the count is equal to the size, I am trying to double the size of the outer part of the array.

if(buffer_count >= buffer_size){
            buffer_size = buffer_size * 2; 
            arr = (int **) realloc(*arr, buffer_size *sizeof(int));
            for(int i=0;i<buffer_size;i++){
                    arr[i] = (int *)malloc(26 * sizeof(int));
            }

This code above will run but it will obviously write over the first 3 since I am using malloc in the inner for loop.

Any attempt at only adding from say buffer_count / 2 and so on and make the inner for loop arr[I] be something like arr[buffer_size/2] to start from the end of what was already allocated to not overwrite it I get a Segmentation Error. I have also tried doing something like:

if(arr[i] == NULL){
   //write to array
}

That also does not work. Like I said, I know the draw backs of using malloc in C++ but I am required to here. Thank you

iueass
  • 9
  • 3
  • Why are you not using `new[]`? You might as well use C and not C++. – PaulMcKenzie Dec 10 '22 at 01:23
  • Don't use `malloc` in C++. – Jesper Juhl Dec 10 '22 at 01:24
  • 2
    Also, the big flaw in all of your code is that `malloc`, `realloc`, etc. will return NULL if there is a problem allocating the memory. None of your code tests for this. – PaulMcKenzie Dec 10 '22 at 01:27
  • 1
    You really can't create true variable-sized, contiguous, multidimensional arrays in standard C++. `int ** arr` does not refer to a "2-d array" - it's a pointer to an array of pointers to individual and separate one-dimensional arrays. But you can in C: [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) (at least you can use VLAs if you're not using a benighted compiler stuck in the last century that still can't support C99) – Andrew Henle Dec 10 '22 at 01:31
  • This is too cumbersome with what you are doing. If you want to go down this path, simply allocate from scratch the bigger array, copy the old array to the new array, delete the old array. BTW, your method of allocating the array is not optimal at all. [See this answer](https://stackoverflow.com/questions/21943621/how-to-create-a-contiguous-2d-array-in-c/21944048#21944048). Yes, it uses `new[]`, but you can retrofit it to use `malloc`. – PaulMcKenzie Dec 10 '22 at 01:33
  • "Like I said, I know the draw backs of using malloc in C++ but I am required to here" – iueass Dec 10 '22 at 01:34
  • Just use a `std::vector` already and forget about all the manual memory management nonsense. – Jesper Juhl Dec 10 '22 at 01:39
  • "Any attempt at only adding from say buffer_count / 2 and so on and make the inner for loop arr[I] be something like arr[buffer_size/2] to start from the end of what was already allocated to not overwrite it I get a Segmentation Error." -- this did not make any sense whatsoever, and yet it is the only thing that matters. You could skip the entire question and just show us those "any attempts" of yours and ask us why they segfault. – Mike Nakis Dec 10 '22 at 02:28
  • 1
    You say `realloc(*arr` works, but it can't possibly work. If it does, then it is only by coincidence. You need `realloc(arr`. – Mike Nakis Dec 10 '22 at 02:32
  • you would have the exact same problem with `new`. Do you understand how this code works? I think if you understand what your code is doing, the answer is fairly obvious – user253751 Dec 10 '22 at 03:01

0 Answers0