c++ - Why this program running differently when compiled each time? -


when ran code, first time showed segmentation fault after taking input of m,n. used cout<< working statement find out can't find printing not. used gdb debug in vain. can explain wrong in it. have pasted output gdb output gcc compiler version in pastebin. pastebin

#include <iostream> #include <cstdio> using namespace std; int main() {     int n,m;     cin >> n,m;     cout<< "working\n";     int input[m][3];     int arr[n+1];     int f=1;     cout<< "working\n";     (int = 0; < n+1; ++i)     {         cout<< "working ... ";         arr[i]=0;     }     (int = 0; < 3; ++i)     {         cout<< "working # ";         cin >> input[0][i];         arr [ input[0][i] ] =i+1;     }      for( int i=1; i< m; i++)         for(int j=0; j< 3;j++)             {                 cout<< "working :";                 cin>>input[i][j];             }  return 0;  } 

this statement invalid. never input value of m.

cin >> n,m; 

it should rewritten so:

cin >> n >> m; 

because m uninitialised may contain contain value possible within range of int.

if m iz negative number, array allocation:

int input[m][3] 

will fail segmentation fault.

going second loop, following statement:

input[0][i] 

would fail if m 0 segmentation fault

given fail negative numbers , 0 there 50% probability of runtime failure.

thus wrong syntax cin causes random failures!


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -