1

I've just started programming in C++ and I'm having a problem with accessing an array from a structure that I defined.

I'm using OpenGL to draw an array of pixel data to the screen using a texture. The array of pixel data is stored in an Image structure:

struct Image {

GLubyte *pixels;
int width;
int height;
int bitrate;

Image(int w, int h, GLubyte* pixels, int bitrate){
    this->width = w;
    this->height = h;
    this->pixels = pixels;
    this->bitrate = bitrate;
}

};

The image is initialized like so:

GLubyte* pix = new GLubyte[WIDTH*HEIGHT*3];
Image image(WIDTH, HEIGHT, pix, 3);

There is a separate structure called Screen which is initialized using an Image instance:

    struct Screen{

    int width, height;
    Image* screen;

    void setScreen(Image* screen, const int width, const int height){
        this->screen = screen;
        this->width = width;
        this->height = height;
    }

};

The screen is initialized like so (right after the Image declaration):

displayData = new Screen();
displayData->setScreen(&image, WIDTH, HEIGHT);

When I access the Width or Height variables it gets the correct value. I then pass the screen instance to this method:

void renderScreen(Screen *screen){
std::cout << "Rendering Screen" << std::endl;
for(int x = 0; x < screen->width; x++){
    for(int y = 0; y < screen->height; y++){

        std::cout << "rendering " << x << " " << y << std::endl;

                    //Gets here and crashes on x - 0 and y - 0

        screen->write(0xFFFFFFFF, x, y);
    }
}
std::cout << "done rendering Screen" << std::endl;
}

which is called like this:

render(displayData); (displayData is a Screen pointer)

Pointers are quite new to me and I don't know rules about passing pointers to structures or anything like that.

3
  • Assuming Jack is right, your misunderstanding is not about the passing of pointers in structures, it's about the lifetime of objects. It seems that your image data has been destroyed when you are still trying to use it. Commented Aug 27, 2013 at 20:54
  • Yeah it's just because I'm used to Java. In Java, the object isn't destroyed until there is no reference to it anywhere in the program. Commented Aug 28, 2013 at 1:45
  • Trying to apply Java thinking to C++ will usually go horribly wrong, very different languages despite the similar syntax. Commented Aug 28, 2013 at 6:34

1 Answer 1

3
displayData->setScreen(&image, WIDTH, HEIGHT);

This seems to point out that you are passing a pointer to an Image by obtaining the address of a local variable eg.

void method() {
  Image image;
  displayData->setScreen(&image, ..)
}

In this situation, image is allocated on stack and a pointer to it is not valid anymore upon exiting the scope which declared that local variable.

You should allocate it on heap instead:

Image *image = new Image(..);
displayData->setScreen(image, ...);

in this way the object will survive until you explicitly delete it with delete image.

Sign up to request clarification or add additional context in comments.

1 Comment

It's odd the way the OP chose to new everything except the image variable.

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.