www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/12/05/12:06:37

From: "Greg Holdridge" <greg AT holdridge7 DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: realloc causing fault
Date: Tue, 5 Dec 2000 17:02:33 -0000
Organization: Customer of Energis Squared
Lines: 87
Message-ID: <90j6oj$fjt$1@news5.svr.pol.co.uk>
NNTP-Posting-Host: modem-17.colorado.dialup.pol.co.uk
X-Trace: news5.svr.pol.co.uk 976035411 15997 62.137.57.17 (5 Dec 2000 16:56:51 GMT)
NNTP-Posting-Date: 5 Dec 2000 16:56:51 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2615.200
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I have a problem with realloc. Whether it is in fact an issue with my code
that I just haven't noticed I couldn't say but when the code below (reduced
from a compression program) is run, it builds a string table for a little
while and then crashes the whole of RHIDE and windows tells me it
encountered a GP error.

Ok so the problem seems to be in the realloc since the last thing in the log
file is always 'attempt realloc.'  Examining the log file, it shows that the
base address of the memory block slowly moves upwards through memory as its
size increases up to about 150K of data having been allocated at which point
the program performs its dramatic exit.

Clearly the program cannot have run out of memory at this small size and it
doesnt explain why the base address of the memory block is moving (it surely
cant have to move the block every single time.)  Anyway I'd appreciate it if
someone could just have a glance at what Im doing below and tell me if there
is an obvious problem.

Thanks a lot,
Greg

// PROBLEM CODE

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <bios.h>

typedef struct lstring_s
{
   int len;
   char *buffer;
} lstring_t;

FILE *log;
int numstrings;
lstring_t *strtable;

void AddStringToTable(lstring_t *str)
{
   numstrings++;
   fprintf(log,"\nattempt realloc at (%#x) - %i numstrings(%i) =
%iK",strtable,numstrings,sizeof(lstring_t),numstrings*sizeof(lstring_t)/1024
);
   fflush(log);
   strtable = (lstring_t*)realloc(strtable, numstrings*sizeof(lstring_t));
   fprintf(log,"\nrealloc succeeded (%#x)",strtable);
   fflush(log);

   if (!strtable)
   {
      printf("\ndecompress error: couldnt allocate stringtable entry");
      exit(1);
   }

   strtable[numstrings-1].len = str->len;
   strtable[numstrings-1].buffer = malloc(str->len);
   if (!strtable[numstrings-1].buffer)
   {
      printf("\ndecompress error: couldnt allocate string space");
      exit(1);
   }
   memcpy(strtable[numstrings-1].buffer, str->buffer, str->len);
}

int main (void)
{
   lstring_t temp;

   log = fopen("C:\\Progra~2\\Projects\\djgpp\\source~2\\rarara","wb");
   numstrings = 0;
   strtable = NULL;

   temp.len = 1;
   temp.buffer = malloc(1);
   temp.buffer[0] = 'a';

   while (1)
   {
      AddStringToTable(&temp);
   }

   return 0;
}



- Raw text -


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