| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Memory Blocks snuffed ? |
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
--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)SEEN-BY: 50/99 620/243 623/630 632/349 635/503 544 727 711/401 409 410 413 SEEN-BY: 711/430 808 809 932 934 712/515 713/888 714/906 800/1 @PATH: 635/544 50/99 711/808 934 |
|
| 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™.