0

I've seen that there is a quite similar question here: Dynamic array in C cause segmentation fault by that it's on C using malloc and I didn't understand the answers. I'm starting to learn programming and I'm just beginning to study arrays, I'm afraid I don't understand well how it works. I'm trying to create a square dynamic array. I'm trying to do it using pointers and 'new' to point to a var to get its size. I don't understand why my code (compiles with no warnings) doesn't work, it crahes after the user entered the value of size. That's what I have:

int main(){

    int **dyn_array=nullptr; //dynamic multidimensional array pointer to pointer

    int *rows_cols=nullptr; //pointer to var with number of rows that wil be introduced by the user 

    int n_elements=0;

    rows_cols = &n_elements;    //now the pointer points to memory location of var with number of rows/cols

    dyn_array = new int *[n_elements];          //pointer of array now points to pointer of rows/cols var to get its size

    cout << "ENTER A EVEN NUMBER OF ELEMENTS FOR A SQUARE ARRAY: " << endl;
    cin >> *rows_cols;  //the value entered by the user its assigned by the pointer to var that its assigned as size of the array

    dyn_array[n_elements][n_elements]={0}; //It crashes. Why? I don't understand...

   return 0;
}

Thank you very much. (excuse my english, i'm learning yet).

Plàcid Masvidal
  • 130
  • 1
  • 2
  • 12

2 Answers2

0

You allocated space for an empty array of int pointers. empty because n_elements is zero at the time of allocation. You'd also need to allocate space for the elements of dyn_array by adding lines like dyn_array[0] = new int[size];.

However, it might be better to try std::vector<int> instead of messing with dynamic memory allocation.

Cornholio
  • 454
  • 2
  • 8
0

There are three problems in the code:

  1. C++ array starts with index 0, indexing the array with n_elements causes off-by-one access error.
dyn_array = new int *[n_elements]; 
dyn_array[n_elements] 
  1. dyn_array points to an int array of size 0
int n_elements=0;
dyn_array = new int *[n_elements]; 
  1. the second n_elements indexes the array pointed by dyn_array[n_elements], which does not exist.
dyn_array[n_elements][n_elements]={0};

a fix

int main(){

    int **dyn_array=nullptr; //dynamic multidimensional array pointer to pointer

    int *rows_cols=nullptr; //pointer to var with number of rows that wil be introduced by the user 

    int n_elements=0;

    rows_cols = &n_elements;    //now the pointer points to memory location of var with number of rows/cols

    cout << "ENTER A EVEN NUMBER OF ELEMENTS FOR A SQUARE ARRAY: " << endl;
    cin >> *rows_cols;  //the value entered by the user its assigned by the pointer to var that its assigned as size of the array

    dyn_array = new int *[n_elements];          //pointer of array now points to pointer of rows/cols var to get its size

    for (int i = 0; i < n_elements; ++i) 
       dyn_array[i] = new int [n_elements];

    dyn_array[n_elements-1][n_elements-1]={0}; //It crashes. Why? I don't understand...

   return 0;
}
stensal
  • 401
  • 4
  • 8
  • thank you, your solving code works perfectly. Now I can let the user decide the size of the array (square). I completed the code with a function to load random the dyn array. Trying to run it, it works well if the user introduces an even value bigger than 8, but crashes by segmentation fault if the value introduced it's 2, 4 or 6. I don't understand why... – Plàcid Masvidal Jan 30 '19 at 17:50
  • You can test your new code at https://segfault.stensal.com to find out what might cause the new segmentation fault. Disclaim, it's a tool of my company. – stensal Jan 31 '19 at 15:01
  • Result(I don't understand): @stensal DTS_MSG: DTS detected a fatal program error! DTS_MSG: Continuing the execution will cause unexpected behaviors, abort! DTS_MSG: OOB Read:reading 4 bytes at 0xa530010 will read undefined values. DTS_MSG: Diagnostic information: - The object to-be-read (start:0xa530010, size:1 bytes) is allocated at - file:/src/new.cpp::54, 17 - 0xa530010 0xa530010 - the object to-be-read - the read starts at the object begin. - Stack trace (most recent call first): -[1] file:/prog.cc::36, 29 -[2] file:/musl-1.1.10/src/env/__libc_start_main.c::180, 11 – Plàcid Masvidal Jan 31 '19 at 20:39
  • Can you use the share button to create a sharable link and post the link back here? – stensal Jan 31 '19 at 21:04
  • The stdin was misused in your link, I made a change. please take a look if this what you want: https://segfault.stensal.com/a/Ou8gjMbgfkUuwKbJ – stensal Feb 01 '19 at 00:24
  • to @stensal Yes, thank you, that's what I want. But It is still crashing by segmentation fault if the size entered by the user its 2, 4 or 6. It works fine if it's 8 or bigger. – Plàcid Masvidal Feb 01 '19 at 06:33
  • sizeof(dyn_array) is not the size of each dimension. take a look, https://segfault.stensal.com/a/7tA9LobFzypKasch – stensal Feb 01 '19 at 10:37
  • It's that! I've replaced sizeof(dyn_array) by n_elements in every loop and now it works with no problems. Thank you. – Plàcid Masvidal Feb 01 '19 at 12:59