-4
    PNG * original;
    original->readFromFile("in.png");
    int width  = original->width(); 
    int height = original->height();

I'm getting a segmentation fault in this bit of code. What am I doing wrong?

3 Answers3

2

You must allocate memory, because original it's just a pointer.

Like this:

PNG *original = new PNG();
Sergio
  • 143
  • 12
1

You are dereferencing original without first having assigned anything to it.

You declared it as a PNG * but did not assign an object instance to that pointer.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

Perhaps you don't need to use a pointer

PNG original;
original.readFromFile("in.png");
int width  = original.width(); 
int height = original.height();

Despite what some newbies seem to think pointers are not obligatory.

john
  • 85,011
  • 4
  • 57
  • 81