Greg Witczak
Greg Witczak

Reputation: 1684

Invalid dynamic table of SDL_Surface

I'm having difficulties with creating dynamic table of SDL_Surface. Take a look at that code:

SDL_Surface **testA = new SDL_Surface *[2];
for(int i=0;i<2;i++)
    testA[i] = new SDL_Surface;
SDL_Surface* testB[2];

As far as i'm concerned, TestA and textB should look identically. But Visual Studio locals look like that: image

How should I fix that?

Upvotes: 1

Views: 219

Answers (2)

Software_Designer
Software_Designer

Reputation: 8587

Try this:

    int size=2;
    SDL_Surface** testA ; 
    testA  = new SDL_Surface*[size];  

    for (int i = 0; i < size; i++) 
    { 
      surface[i] = NULL; //  here, surface[i] is the kth pointer, not an SDL_Surface 
   // surface[i] = SDL_CreateRGBSurface ( /* set your parameters */ );
    } 


    // Of course, somewhere later in the code, you'll need to free the memory ... 
    for (i = 0; i < size; i++) 
    { 
    SDL_FreeSurface(testA [i]); 
    testA [i] = NULL; 
    } 

    delete testA ; 

Upvotes: 2

alexisdm
alexisdm

Reputation: 29886

SDL_Surface shouldn't be allocated using new, but using the functions from the SDL API (e.g. SDL_CreateRGBSurface, SDL_ConvertSurface...) which will allocate it and initialize it properly.

If the problem is about the way Visual Studio debugger displays dynamically allocated arrays, you should look at that question: How to display a dynamically allocated array in the Visual Studio debugger?

Upvotes: 1

Related Questions