PGM Help

humidifire's Avatar, Join Date: Feb 2011
Newbie Member
I keep getting a segmentation fault when I run it.
Program will not get past the prinf line.
Here is my code

Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc,char **argv)

{
    int i,j,xres,yres,max,**image;
    ofstream fp;

// xres, yres, maxintensity, and image[][] must already be defined
    cin >> xres;
    cin >> yres;
    
    fp.open(argv[1]);
    cout << "P2" << endl;
    cout << xres << " " << yres << endl;
    cout << "255" << endl;      // Almost always equals 255
    for (i=0;i<xres;i++)
       for (j=0;j<yres;j++)
          printf("%4d", image[i][j]);
return 0;
}

Last edited by shabbir; 16Feb2011 at 10:26.. Reason: Code blocks
0
jimblumberg's Avatar
Ambitious contributor
So where is image initialized?
Why are you mixing C++ io (cin/cout) and C stdio (printf) you should pick one type or the other.


Jim
0
humidifire's Avatar, Join Date: Feb 2011
Newbie Member
The Instructor wants me to use printf and not cout.
and image is initialized as an int.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
Originally Posted by humidifire View Post
image is initialized as an int.
No it's not. See those two asterisks? They change the meaning significantly. Also look at the comment; that should give you a clue. The reason for the segfault is basically that you've ignored the comment and are trying to use an uninitialised 2D array.