Bitmap fonts and memory allocation woes

Hi, I wrote a little bitmap font library to use in a game I’m making but
I’m sort of stuck. I use an array of structs:

struct font
{
int charnum;
int *loffset;
int *roffset;
SDL_Surface *fontmap;
};

to store my font. The font bitmap is stored in fonts[i].fontmap, the
number of characters in charnum and loffset and roffset are pointers to
arrays of ints which are allocated at runtime depending on charnum.

struct font *fonts;

[…]
[determine number of fonts to load from config file]

/* allocate memory for sufficient number of font structures /
if( (fonts = malloc(maxfonts
sizeof(font))) == NULL)
{
printf(“Unable to allocate memory for font structures!\n”);
exit(1);
}

[…]

// determine number of chars
fonts[fontcount].charnum = fonts[fontcount].fontmap->h /
fonts[fontcount].fontmap->w;

// allocate memory for offset arrays
fonts[fontcount].loffset = malloc(fonts[fontcount].charnumsizeof(int));
fonts[fontcount].roffset = malloc(fonts[fontcount].charnum
sizeof(int));

up until here, everything works beautifully, I can access the arrays
like so:
fonts[i].loffset[k] and they work, however, when it comes to freeing
the allocated memory I get a segfault:

for(i = 0; i<maxfonts; i++)
{
SDL_FreeSurface(fonts[i].fontmap);
free(fonts[i].loffset); // <-- segfault
free(fonts[i].roffset); // <-- segfault
}

also I cannot free the initially allocated memory for the array fonts of
font structures

free(fonts) gives me illegal type

any ideas?

Thanks!

Bartek

Okay, solved it. The code was good but the implementation was not quite.
I initialized the *fonts array and the instance of the font struct in
the main program file, obviously, in the font module it was impossible
to free it. It works like a charm after moving the intialization fully
into the font library.On Mon, 2005-07-04 at 08:40 +0100, Bartek Kostrzewa wrote:

Hi, I wrote a little bitmap font library to use in a game I’m making but
I’m sort of stuck. I use an array of structs:

struct font
{
int charnum;
int *loffset;
int *roffset;
SDL_Surface *fontmap;
};

to store my font. The font bitmap is stored in fonts[i].fontmap, the
number of characters in charnum and loffset and roffset are pointers to
arrays of ints which are allocated at runtime depending on charnum.

struct font *fonts;

[…]
[determine number of fonts to load from config file]

/* allocate memory for sufficient number of font structures /
if( (fonts = malloc(maxfonts
sizeof(font))) == NULL)
{
printf(“Unable to allocate memory for font structures!\n”);
exit(1);
}

[…]

// determine number of chars
fonts[fontcount].charnum = fonts[fontcount].fontmap->h /
fonts[fontcount].fontmap->w;

// allocate memory for offset arrays
fonts[fontcount].loffset = malloc(fonts[fontcount].charnumsizeof(int));
fonts[fontcount].roffset = malloc(fonts[fontcount].charnum
sizeof(int));

up until here, everything works beautifully, I can access the arrays
like so:
fonts[i].loffset[k] and they work, however, when it comes to freeing
the allocated memory I get a segfault:

for(i = 0; i<maxfonts; i++)
{
SDL_FreeSurface(fonts[i].fontmap);
free(fonts[i].loffset); // <-- segfault
free(fonts[i].roffset); // <-- segfault
}

also I cannot free the initially allocated memory for the array fonts of
font structures

free(fonts) gives me illegal type

any ideas?

Thanks!

Bartek


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl