Date: Sun, 2 Apr 1995 19:27:28 -0700 (PDT) From: ".ASM SoftWare Systems" Subject: Re: Dynamic allocation question To: Mat Hostetter Cc: djgpp AT sun DOT soe DOT clarkson DOT edu, IBBT0 AT cc DOT uab DOT es On Sun, 2 Apr 1995, Mat Hostetter wrote: > >>>>> "DJ" == DJ Delorie writes: > > >> If ASSUMPTION 2 is true, is there an ANSI-C manner to get from > >> the O.S. the amount of memory a pointer points to? I have > >> tried "sizeof(pointer)", but it only returns the size of a > >> single element. > > DJ> There is no portable way of doing this. ANSI and POSIX > DJ> specifically stay away from memory allocation techniques. In > DJ> theory, you may be able to reverse-engineer malloc() (or, for > > Some people write their own my_malloc/my_realloc/my_free that keep > track of the block size in a portable way and use these routines > consistently. What you do is allocate four bytes more than you need > The non-ANSI Standard way of doing these things is with functions like: unsigned __freect(sizeof(float)) Returns # of items of size 'float' that can be allocated from free memory left in the heap. uint __memavl() Returns approx # of bytes of free mem left in the heap. unsigned __msize(char *ptr) Returns # of bytes allocated to ptr. uint stackavail() Returns # of bytes available for stack allocation using alloca(). TurboC has functions like: int heapcheck(), heapfillfree(), heapcheckfree(), heapchecknode(), heapwalk() and others ... If you understand how gcc's malloc works you can write a ONE version specific function that does one of the above and incorporate it into your program. As long as you don't compile with a later (or earlier) version with an incompatable malloc method you'll be ok ... Veronica/WWW/Jughead for the above functions, you may find .c !