www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/04/04/08:21:35

Date: Tue, 4 Apr 1995 13:06:01 GMT+0200
From: IBBT0 AT cc DOT uab DOT es
Subject: Abstract of memory allocation answers
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Organization: Universitat Autonoma de Barcelona

Dear programmers,

Few days ago I posted some questions about dynamic allocation.
Many people answered to help me. Here is the abstract of the
messages I obtained:

==============================================================

When from inside some function you allocate memory via malloc,
this memory will not be freed when the function returns. In
other words, memory allocated via malloc() is not automatically
free()'d on termination of whatever function contains the malloc call.

If the function does not free the memory block and does not return
the pointer, that memory block will be occupied until the program
stops, and there is no way to free it. It is good practice to go to
the trouble to do explicit free's.

If the function returns the pointer, it is possible to use it, to
use realloc() and even to free it.

The memory management modules (malloc, free, etc.) 'know' the size of
the allocated block. The OS only knows how *much* memory the program
has. There is no portable way of getting the amount of memory a
pointer points to. Some people write their own
my_malloc/my_realloc/my_free functions that keep track of the block
size in a portable way and use these routines consistently. You
can allocate four bytes more than you need (using some struct joining
the pointer and the size) or access information that
malloc stores [if you have access to the library's source
(for gcc, it usually is <compiler path>/libsrc/c/lib/malloc.c) it
is possible to take a look on it].

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 ...


Moreover, jerry AT jvdsys DOT textlitho DOT nl sent to me a text
(UNDERSTANDING POINTERS. For beginners) from Ted Jensen
that clearly shows many basic aspects. More advanced questions
can be read from the FAQ list available on Newsgroups:
comp.lang.c,comp.answers,news.answers, although not in this topic.

==============================================================

Many thanks to:
  IN%"bworthy AT MT DOT net"
  IN%"bworthy AT MT DOT net"
  IN%"dj AT delorie DOT com"
  IN%"UCKO AT VAX1 DOT ROCKHURST DOT EDU"
  IN%"rdc AT freenet DOT vancouver DOT bc DOT ca"
  IN%"corno AT galileo DOT polito DOT it"
  IN%"ldoan1 AT osf1 DOT gmu DOT edu"
  IN%"jerry AT jvdsys DOT textlitho DOT nl"
  IN%"bdavidson AT ra DOT isisnet DOT com"


Xavier Pons

- Raw text -


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