www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/13/14:58:24

From: damien diederen <s961135 AT pop3 DOT student DOT ulg DOT ac DOT be>
Newsgroups: comp.os.msdos.djgpp
Subject: Strange world of Malloc()
Date: Thu, 13 Feb 1997 16:41:10 +0100
Organization: Université de Liège
Lines: 102
Message-ID: <33033616.E4C@pop3.student.ulg.ac.be>
Reply-To: s961135 AT student DOT ulg DOT ac DOT be
NNTP-Posting-Host: sdet01.student.ulg.ac.be
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Could somebody help me ?

        I got DjGpp ont the WWW about a month ago and learned C/C++ with 
it, so I am a Newbie (don't laugh, please ;-) ).

        I've got a big problem with the few next lines of code : I don't 
get any errors when compiling nor when linking. But when I am using the 
function VCF_Font * VCF_LoadFont(char * FName), a lot of strange things 
are happening ...

        This function has to read a 256 color font in a file (a type I 
created with BP 7.0), and store it to memory. It works very well with 
many fonts I created previously, but when I read the others, the program 
exits (SIGSEV fault).

        I though there was a bug in my code (perhaps this is the case) 
and begun to debug my function with Rhide's integrated debugger. All 
things seem to be right, but when I arrive at certain characters (say 
#37), tmpbyte1 gets filled with the right value, tmpbyte2 and tmpword 
too, and malloc(tmpword) executes, then all my watches are wrong 
(Unavailable !), and I must exit from Rhide ?!?

This is the source code of the function :

// LIBVCF.H ///////////////////////////////////////////////////////

#ifndef _VCF_
#define _VCF_

#ifndef SI
#define SI short int
#endif

#include "libgdi.h"

typedef struct VCF_Font
{
   GDI_Sprite Chars[256];
};

VCF_Font * VCF_LoadFont(char * FName);
void VCF_WriteString(SI x1, SI y1, char * String, VCF_Font * Font);
int VCF_Length(char * String, VCF_Font * Font);

#endif

--------

// LIBVCF.CC ////////////////////////////////////////////////////

#include <stdio.h>
#include "libvcf.h"

VCF_Font * VCF_LoadFont(char * FName)
{
   VCF_Font * TPtr = (VCF_Font *) malloc(sizeof(VCF_Font));
   FILE * FontFile = fopen(FName, "rb");
   if(FontFile == NULL) return NULL;
   int i;
   unsigned char tmpbyte1, tmpbyte2;
   long unsigned int tmpword; // Like in the header file with malloc()
   for(i = 0; i <= 255; i++)
   {
      fread(&tmpbyte1, 1, 1, FontFile);
      TPtr->Chars[i].Width = tmpbyte1;
      fread(&tmpbyte2, 1, 1, FontFile);
      TPtr->Chars[i].Height = tmpbyte2;
      tmpword = tmpbyte1 * tmpbyte2;
      if(tmpword == 0)
         TPtr->Chars[i].Data = NULL;
      else
      {

// !!!!!!!!!!!!! Here's the problem !!!!!!!!!!!!!!!!!

         TPtr->Chars[i].Data = (char *) malloc (tmpword);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

/*
        NOTE : when I am using (char *) malloc (1024), max. size
of a character, the function works with all my fonts, but
it wastes a lot of memory !!!
        Why does (char *) malloc (1024) works while (char *) malloc 
(tmpword) with tmpword = 224 doesn't ??????
*/

         fread(TPtr->Chars[i].Data, 1, tmpword, FontFile);
      }
   }
   fclose(FontFile);
   return TPtr;
}

        Thank you very much ...	

        NB. Sorry for my bad english, I'll try to be better next time 
... ;-)

*********************************************************************
**** DASH                                                        ****
**** E-Mail : s961135 AT student DOT ulg DOT ac DOT be                          ****
*********************************************************************

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019