pnewbie
pnewbie

Reputation: 1

Segmentation fault two dimensional array

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) "

Upvotes: 0

Views: 221

Answers (4)

Rajeev Singh
Rajeev Singh

Reputation: 3332

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++;
}

Upvotes: 0

pnewbie
pnewbie

Reputation: 1

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;
}

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 30489

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;

Upvotes: 1

mjwach
mjwach

Reputation: 1214

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.

Upvotes: 0

Related Questions