TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Frank Adam
from: David Nugent
date: 1996-06-13 14:09:16
subject: free()

> What i would like to know now is, if the allocated size of the array
 > ptr points at can be determined in some way.

Not by any documented means. The RTL or at least the operating system
almost certainly would need to track the existing size of allocated memory
blocks in order to implement realloc(), but the C programmer has no direct
access to that information. It is ultimately up to you to keep track of it
yourself.


 > I'm in the process of putting all my library function neatly together,
 > and i would love to make some functions capable of determining whether a
 > reallocation is necessary or not, and not rely on the programmer taking
 > care of it.

Write a front-end to malloc() to do it for you. I often use such a wrapper
since it helps in debugging as well - I've included a variation of it
below. You can detect heap corruption (boundary overwrite), attempting to
free pointers which were not allocated from the heap to begin with, blocks
that have already been freed, and you can issue diagnostics on where the
function was called from and so forth.

The following code does make some assumptions as to architecture (ie. that
it is either 16- or 32-bit and that it uses at least integral alignment of
memory blocks), but it is a reasonable compromise between portability and
usefulness.

Warning: this code has been adapted from a module in production use. It
probably works as is (at least it compiles :-)), but since it is not
identical to the original, so there may be bugs. Test thoroughly before you
depend on it.


/* mem.c
 * Prototypes for memory allocation wrappers
 * Compile with -D_32BIT_ for 32-bit environments
 * This code is public domain
 * written by David L. Nugent 3:632/348{at}fidonet 
 */

#ifndef _MEM_H
#define _MEM_H
#include 

void * mymalloc(size_t sz);
void * myrealloc(void * p, size_t sz);
size_t mymemsize(void * p);
void myfree(void * p);

#endif


/* mem.c
 * Basic allocation wrappers for debugging and sizing
 * Compile with -D_32BIT_ for 32-bit environments
 * This code is public domain
 * written by David L. Nugent, 3:632/348{at}fidonet, 
 */

#include "mem.h"

#ifdef _32BIT_
#define MEM_MAGIC_BLOCK 0x1af59cf3
#else
#define MEM_MAGIC_BLOCK 0x9cf3          /* Used as a crude check */
#endif

void * mymalloc(size_t sz)
{
    void * p = NULL;
    if (sz)
    {   /* Must use integral for alignment */
        unsigned * i = malloc(sz + sizeof(unsigned) * 2);
        if (i == NULL)
            /* issue diagnostic, optionally quit */;
        else
        {
            *i++ = sz;
            *i++ = MEM_MAGIC_BLOCK;
            p = i;
        }
    }
    return p;
}

void * myrealloc(void * p, size_t sz)
{
    if (p == NULL)
        p = mymalloc(sz);
    else if (sz == 0)
    {
        myfree(p);
        p = NULL;
    }
    else
    {
        unsigned * i = p;
        if (*--i != MEM_MAGIC_BLOCK)
            p = NULL;           /* Failed */
        else if (*--i != sz)    /* Avoid it if we can */
        {
            if ((i = realloc(i, sz + sizeof(unsigned) * 2)) == NULL)
            {   /* issue diagnostic, optionally quit? */
                p = NULL;
            }
            else
            {
                *i++ = sz;
                *i++ = MEM_MAGIC_BLOCK;
                p = i;
            }
        }
    }
    return p;
}

size_t mymemsize(void * p)
{
    size_t sz = 0;
    if (p)
    {
        unsigned * i = p;
        if (*--i == MEM_MAGIC_BLOCK)
            sz = *--i;
    }
    return sz;
}

void myfree(void * p)
{
    if (p)
    {
        unsigned * i = p;
        if (*--i != MEM_MAGIC_BLOCK)
            /* issue diagnostic */ ;
        else
        {
            *i ^= (unsigned)-1;
            free(--i);
        }
    }
}

--- MaltEd/2 1.0.b6
* Origin: Unique Computing Pty Limited (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 107 348 360 633/371 634/388 396
SEEN-BY: 635/301 502 503 506 544 639/252 711/401 409 410 413 430 808 809 932
SEEN-BY: 711/934 712/515 713/888 714/906 800/1
@PATH: 632/348 635/503 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™.