TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Frank Adam
from: Paul Edwards
date: 1996-10-08 01:09:50
subject: Auto string-length deter

FA> PS: if portability is not an issue look at heapwalk in Borland, or go
PW> What does heapwalk() do?  (Forgive all these questions -- I only got

FA> Heapwalk goes through the internal allocation table, and report the number 
FA> of bytes allocated to each variable and the state of the memory.
FA> With a bit of coding it can return the REAL size of any variable.
FA> I've posted a piece of code here few months back on that, but i can't 
FA> seem to find it just now.

Nothing that a 4800-deep messagebase can't fix...

Date:   1996-06-17 08:18:00
From:   Frank Adam of 3:635/544
To:     All
Subj:   Memory Blocks snuffed ?
Attr:   
Conf:   AUST_C_HERE                   

Hi,

Tested in TC2 and BC4, worked correctly in both.
This function will return the size of the memory block held by a pointer.
It will do a realloc too, according to parameter 2 and 3.                

Keeping in mind it's Borland specific code, but other compilers may have 
a similar function. 
I also found that in Borland the size of the data is at ptr-4h, where 
that byte is the low byte and the following byte(ptr-3) is high byte.
Perhaps the following two bytes are reserved for 32 bit environments, but
they have some values in them. This i came upon when i've noticed that
heapwalk returns the malloced size + 4. 
Had to rewrite it to put it through Pacific C, but the idea does not work 
there , so it seems it's the RTL doing the shuffle not the OS.             
                        

I do however have a problem, with those return values, initially i thought
that i can modify the pointer's size from within the function, and return
a meaningful value instead, but the original pointer gets corrupted 
unless i actually return the pointer to it. 
I know i'm doing something wrong.
Any ideas ?

Then again this might crash everyones PC here, if so, whoops sorry.:-)     
                                                        

#include 
#include 
#include 
#include 
                          /* parameter 3 */    
#define SRGETSIZE 0   /* gets the memory block size only */
#define SRANY     1   /* realloc as long as ptr is dynamicly alloced */
#define SRUP      2   /* realloc only if smaller than required */
        /* does SRDOWN make any sense ???? */

/* #define SROK      0  /* success */
/* #define SRBADHEAP 1  /* bad heap */
/* #define SRNOMEM   2  /* no memory left */
/* #define SRBADPTR  3  /* bad or NULL pointer passed */
/* #define SRNOIDEA  4  /* No idea :-) haven't got there yet */

char memlen[5]; /* Until i work out the returns,i have to have this here*/

char* AddStr(char*,char*);
char* Smart_reAlloc(char*, unsigned, int );


int main( void )
{
 char *s = malloc(10);
 printf("\n\n\n");        /* just dropping a few lines between tests */
 strcpy(s,"The ");        /* Put something into the location */
 printf("At the start :\n");
 printf("S = %s\nLives at %p.\nOwns %s bytes.\nLength = %d bytes.\n\
\nAfter Realloc :\n",s,s,Smart_reAlloc(s,0,SRGETSIZE),strlen(s));

 s = AddStr(s,"wicked wombat whoopied with Wally's willing wife.");
     /* You didn't expect "Hello world" from me ? :-)*/

 printf("S = %s\nIt lives at %p\nOwns %s bytes.\nLength = %d\
  bytes",s,s,Smart_reAlloc(s,0,SRGETSIZE),strlen(s));

 free(s);
 return 0;
}



char* Smart_reAlloc(char* chkptr, unsigned size,int mode)
{
 struct heapinfo hi;
 if(!chkptr) return NULL;
 if( heapcheck() == _HEAPCORRUPT) return NULL;

 hi.ptr = NULL;
 while(heapwalk (&hi) == _HEAPOK && hi.ptr != chkptr) heapwalk(
&hi );

 if(hi.ptr == chkptr)
 {
  switch(mode)
  {
   case 0 : return itoa(((int)hi.size - (sizeof(char*)*2)),memlen,10);
              
   case 1 : chkptr = realloc(chkptr,size);
     if(NULL == chkptr) return NULL;
     return chkptr;

   case 2 : if(( hi.size - (sizeof(char*)*2)) < size)
      {
       chkptr = realloc(chkptr,size);
       if(NULL == chkptr) return NULL;
       }
      return chkptr;

   }
  }

return NULL;
}

char* AddStr(char *ptr,char *txt)
{
 int len;
 if(!ptr) return NULL;

 len = strlen(ptr) + strlen(txt) + 1;
 ptr = Smart_reAlloc(ptr,len,SRUP);

 if(ptr) strcat(ptr,txt);

return ptr;
}
 
  L8r Frank (part time inventor)
                                  
___ Blue Wave/DOS v2.21
                              
@EOT:

---
* Origin: X (3:711/934.9)

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.