Thread: Segfaults

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Segfaults

    I'm having problems with a function that writes image data to a buffer. Heres where I allocate the memory in main():
    Code:
       fb = malloc(sizeof(int)*FONTSET_WIDTH*FONT_WIDTH*FONTSET_HEIGHT*FONT_HEIGHT);
    And heres my function along with struct definition and global pointer declaration:
    Code:
    struct font_buffer
    {
           Uint32 pixels[FONTSET_WIDTH*FONT_WIDTH*FONTSET_HEIGHT*FONT_HEIGHT];
    }; typedef struct font_buffer font_buffer;
    
    struct screen_buffer *sb = NULL; 
    struct font_buffer *fb = NULL;
    
    void LoadBitmapFont()
    {
        SDL_Surface *font=NULL;
        font = SDL_LoadBMP("Font.bmp");           
        unsigned image_width=FONTSET_WIDTH*FONT_WIDTH;
        unsigned image_height=FONTSET_HEIGHT*FONT_HEIGHT;
        unsigned image_size = image_width * image_height * sizeof(int); 
        unsigned i, inc=sizeof(int);
        Uint32 *src=NULL, *dst=NULL;
        
        for(i=0; i<image_size; i+=inc)
        {
            src = font->pixels+i;
            dst = fb->pixels+i;
            *dst = *src;        
        }          
         
        SDL_FreeSurface(font);
        font=NULL;
    }
    My debugger just says I got segfaults If I dont call this function then the program runs, so it seems as if there must be a problem with the function. Can someone please tell me whats wrong with it? Thanks.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Sorry, I found out what was wrong just after posting (this allways seems to happen to me). I was incrementing +4 bytes, when I only have to increment by 1. This isn't the first time I have done this either >_<

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    Sorry, I found out what was wrong just after posting (this allways seems to happen to me). I was incrementing +4 bytes, when I only have to increment by 1. This isn't the first time I have done this either >_<
    If you stop worrying about how big stuff is, you'll stop making that mistake. You should probably never use sizeof() except inside a call to malloc() or an I/O function. So when you set "incr = sizeof(int)" that should have set off an alarm bell.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fb = malloc(sizeof(int)*FONTSET_WIDTH*FONT_WIDTH*FONTSE T_HEIGHT*FONT_HEIGHT);
    Ugh, that's horrible.
    And wrong, since it assumes sizeof(int) == sizeof(Uint32)

    Read the FAQ, then do
    fb = malloc ( sizeof *fb );
    Will always be right, no matter how you change the contents of the struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfaults - again
    By mike_g in forum C Programming
    Replies: 2
    Last Post: 07-20-2008, 12:49 PM
  2. copy() segfaults
    By Kaiser in forum C++ Programming
    Replies: 8
    Last Post: 09-29-2007, 06:17 PM
  3. Progress bar test segfaults
    By w00tw00tkab00t in forum C Programming
    Replies: 7
    Last Post: 07-01-2006, 09:38 AM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. STRTOK SegFaults!
    By DarthKoRn in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 02:29 PM