matt_rule
matt_rule

Reputation: 1400

Unexplained memory error using C++, SDL, OpenGL and GLEW

This is my entire application (stripped down to the essentials):

#include <stdio.h>
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <gl/glu.h>

int main( int argc, char* args[] )
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* gWindow = SDL_CreateWindow( "title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
    SDL_GLContext gContext = SDL_GL_CreateContext( gWindow );
    glewInit();

    GLfloat *floatArray = new GLfloat(108);
    floatArray[107] = 0.0f;

    GLuint glBuffer;
    glGenBuffers(1, &glBuffer);

    return 0;
}

This and the required libraries are all compiled in Visual Studio 2012.

This code initialises all the required systems for an OpenGL context, in the right order and nothing is missing as far as I can see (cleaning up is only needed after the point it breaks so I took that out).

It breaks on the line glGenBuffers(1, &glBuffer);, with the error "SOFT323_vs11_2012.exe has triggered a breakpoint."

Setting an element beyond floatArray[9] is required to break it.

How do I approach this problem? Is it a glitch with one of the libraries or am I using them incorrectly?

Upvotes: 1

Views: 159

Answers (1)

Drew Dormann
Drew Dormann

Reputation: 63912

This allocates a single GLfloat, initialized with the value 108.0f.

GLfloat *floatArray = new GLfloat(108);

You wanted to allocate an array:

GLfloat *floatArray = new GLfloat[108];

Upvotes: 5

Related Questions