Arrow operator

Nov 7, 2009 at 9:10pm
I'm playing around with SDL in C and for some reason; I'm getting a segfault using the pointer-to-member ('->') operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void __init init(SDL_Surface* screen) {
    /* Initialze SDL */
    SDL_Init(SDL_INIT_EVERYTHING);

    scr_t* scr;
    
    /* Set up the screen: */
    scr->scr_width  = 1024;
    scr->scr_height = 768;
    scr->scr_bpp    = 32;
    
    screen = SDL_SetVideoMode(
        scr->scr_width, scr->scr_height, scr->scr_bpp,
        SDL_SWSURFACE | SDL_DOUBLEBUF /* We want a double buffered screen */
    );
    
    if (screen == NULL) {
        die("Error: Couldn't set video mode.\n");
    }
}


It is at the point where I use the arrow operator that it segfaults.
Why is this? I always get segfaults using it; so obviously I'm doing something wrong. I googled it and I got this page http://boredzo.org/pointers/#structures and it looks to me like he's accessing a structure in the same way I am :l

Here is the structure:
1
2
3
4
5
typedef struct scr {
    int scr_width;  /* Screen height */
    int scr_height; /* Screen width  */
    int scr_bpp;    /* Bits per pixel (colour depth) */
}   scr_t;


I thought for the moment that, though unlikely, it might be the typedef. I tried it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "fps.h"

void __init init(SDL_Surface* screen) {
    /* Initialze SDL */
    SDL_Init(SDL_INIT_EVERYTHING);

    struct scr* sc;
    
    /* Set up the screen: */
    sc->scr_width  = 1024;
    sc->scr_height = 768;
    sc->scr_bpp    = 32;
    
    screen = SDL_SetVideoMode(
        sc->scr_width, sc->scr_height, sc->scr_bpp,
        SDL_SWSURFACE | SDL_DOUBLEBUF /* We want a double buffered screen */
    );
    
    if (screen == NULL) {
        die("Error: Couldn't set video mode.\n");
    }
}

it still segfaults, so obviously that wasn't the problem.

1
2
3
    (*scr).scr_width  = 1024;
    (*scr).scr_height = 768;
    (*scr).scr_bpp    = 32;

this also fails.
Last edited on Nov 7, 2009 at 9:18pm
Nov 7, 2009 at 9:21pm
You aren't assigning sc to anything before using -> so you can't access it
Nov 7, 2009 at 9:22pm
Huh? I have to assign it to the structure? Argh; I thought making it a pointer to that structure type would work :l

1
2
3
4
    scr_t* scr = NULL;
    scr_t sc;
    
    scr = ≻

This seems to work for now, thanks :)
Last edited on Nov 7, 2009 at 9:24pm
Nov 7, 2009 at 9:24pm
You may want to create an object dynamically
Nov 7, 2009 at 9:25pm
Oh? As I say, I'm using C so I can't use new; although I guess I could "swap" to C++...

Or can I do this with malloc() and free()?

1
2
3
4
5
    scr_t sc;
    scr_t* scr = (scr_t*) malloc(sizeof(sc));
    
    scr = ≻
    

?
Last edited on Nov 7, 2009 at 9:27pm
Nov 7, 2009 at 9:26pm
malloc and free are the same (~) as new and delete in C++
Nov 7, 2009 at 9:28pm
Yes; but they work differently. Or at least, you use them differently.

In the snippet above, I free()'d scr after using it... It still works.

Thanks :)

Ok that snippet doesn't work; it causes a backtrace and is obviously very stupid...
I can't free() it for some reason...
Last edited on Nov 7, 2009 at 9:32pm
Nov 7, 2009 at 10:08pm
Hmmm... I'm getting a segfault with something different now; whenever I call SDL_Flip it segfaults; but if I remove it, it doesn't :l

Never mind. I wasn't handling the surfaces right.
Last edited on Nov 7, 2009 at 10:17pm
Topic archived. No new replies allowed.