0

i am new to programing and i am trying to understand the two dimensional array. i wrote this code to just test my code to see if it working or not. unfortunately, i am getting a segmentation error. i know that means that something i wrote is unreadable for the compiler but i do not know what is it. because everything seems fine to me.

#include<iostream>


using namespace std;


int main(){

   int col, row,i;
   int **array;

   cout << "How many rows?\n";
   cin >> row;
   cout << "How many colomns\n";
   cin >> col;

cout << "!!!!!!!!!!!!";

   array = new int*[row];
   for (int i = 0;i<row;i++){
          array[i] = new int[col];
   }   

        cout << "!!!!!!!!!!!!";

   for( i=0; i<row; i++){
        int x=1;
      array[0][i]= x;; 
   x++;
   }   
        cout << "!!!!!!!!!!!!";
   cout << array[row][col];

   for(i=0; i<row; i++){

   delete [] array[i];
   delete [] array;
   }   
   return 0; 
}

the ERROR is: " How many rows? 3 How many colomns 3 Segmentation fault (core dumped) "

3
  • cout << array[row][col]; is out of range. array[row-1][col-1] is as deep as you can go. Commented Mar 31, 2016 at 4:59
  • x++; is not doing anything; x goes out of scope at the end of each loop iteration and is recreated at the start of the next iteration. Commented Mar 31, 2016 at 5:02
  • We don't use pointers and arrays and new and deete in C++ much these days. There are healthier alternatives like std::vector and std::shared_ptr. Commented Mar 31, 2016 at 5:14

4 Answers 4

1

array[0][i]= x; looks wrong. It should be:

array[i][0]= x;

First index is for row and second for col.

Later cout << array[row][col]; is also wring as row is out of range.

The way you delete the array is also wrong, it should be:

for(i=row - 1; i >= 0; --i){
  delete [] array[i];
}
delete [] array;
Sign up to request clarification or add additional context in comments.

1 Comment

so it should be like this?
0
cout << array[row][col];

Out of range in each of the two dimensions, as others have said.

for(i=0; i<row; i++){

delete [] array[i];
delete [] array;
}

You're deleting array's elements three times, once for each element. This is good. You're also deleting array itself three times. This is dangerous and wrong.

Comments

0

so it should be like this?

#include<iostream>


using namespace std;


int main(){

   int col, row,i;
   int **array;

   cout << "How many rows?\n";
   cin >> row;
   cout << "How many colomns\n";
   cin >> col;

   array = new int*[row];
   for (int i = 0;i<row;i++){
          array[i] = new int[col];
   }   

        cout << "!!!!!!!!!!!!";

   for( i=0; i<row; i++){
        int x=1;
      array[i][0]= x;; 
   x++;
   }   
        cout << "!!!!!!!!!!!!";
   for(i=0; i<row; i++){
        for(int j=0; j<col; j++){
           array[i][j];
        }   
   }   
    cout << array[row][col];    

   for(i=0; i<row; i++){

   delete [] array[i];
   delete [] array;
   }   
   return 0;
}

1 Comment

x++; and array[i][j]; do nothing, but they will also harm nothing. delete[] array; is still incorrectly running three times (assuming row is 3). Otherwise I see no problems, though I haven't confirmed that by running the code.
0

This line is out of range cout << array[row][col];.If you want to print the last element then change this line to cout << array[row-1][col-1];

As others mentioned the below is code for deleting the allocated memory

for(i=0; i <row; i++){
    delete [] array[i];       
}
delete [] array;

Also in your code below x is always going to be 1 , x++ is noneffective.

for( i=0; i<row; i++){
 int x=1;      
 array[0][i]= x; 
 x++;
}

if you want to increment x for each row then initialize x outside the loop like this

int x=1; 
for( i=0; i<row; i++){     
  array[0][i]= x;; 
  x++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.