[comp.lang.c] CALLOC and MALLOC

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/23/88)

In article <11415@brl-adm.ARPA> V4039%TEMPLEVM.BITNET@CUNYVM.CUNY.EDU (Stan Horwitz) writes:
>Why do the basic memory allocation functions CALLOC and MALLOC clear the newly
>allocated memory they yield?

malloc() doesn't.  calloc() stores 0 bytes in the allocated area, which may
not be equivalent to 0-valued objects if some of the storage is interpreted
as pointers or floating-point data.

>I am a novice C programmer and am trying to build dynamic arrays as I
>data is received by the program in question, yet each time I add a new
>element of memory onto my array, all the previous elements are cleared.

I don't know how you're trying to do this; realloc() is used to extend
the allocated memory, and it does NOT alter the previous contents,
although it may move them to new addresses.

asjoshi@phoenix.Princeton.EDU (Amit S. Joshi) (01/23/88)

 I would use realloc() to increase the size of the array.

Hope this helps.


-- 
Amit Joshi	BITNET	|	Q3696@PUCC.BITNET
		USENET	| {seismo, rutgers}\!princeton\!phoenix\!asjoshi
"There's a pleasure in being mad... which none but madmen know!" - St.Dryden

chris@mimsy.UUCP (Chris Torek) (01/23/88)

In article <11415@brl-adm.ARPA> V4039%TEMPLEVM.BITNET@CUNYVM.CUNY.EDU
(Stan Horwitz) writes:
>Why do the basic memory allocation functions CALLOC and MALLOC clear
>the newly allocated memory they yield?

They do not.  calloc sets the allocated memory either to all zero
bytes or to integer zeroes (it is implemented as integers on Unix,
but I do not know what the dpANS says).  malloc sets nothing at all.

>I ... am trying to build dynamic arrays ... yet each time I add a new
>element of memory onto my array, all the previous elements are cleared.

If you mean you are doing something like this:

	getthem()		/* buggy */
	{
		int nel, *el, v;

		nel = 0;
		el = NULL;
		while (scanf("%d", &v) == 1) {
			nel++;
			el = (int *)malloc(nel * sizeof(int));
			if (el == NULL)
				panic("out of memory");
			el[nel - 1] = v;
		}
		...
	}

then you are doing it wrong!  To change the size of a previously
allocated region, use `realloc', which copies as much data as you
had before (if the region is growing) or as much as will fit (if
it is shrinking).

The example above continuously allocates more space, never releasing
the previously-allocated space.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

miket@ccicpg.UUCP (Mike Tracy) (01/27/88)

In-reply-to: your article <11415@brl-adm.ARPA>


cunyvm.cuny.edu writes:

> Why do the basic memory allocation functions CALLOC and MALLOC clear the newly
> allocated memory they yield?  
> I am a novice C programmer and am trying to build dynamic arrays as I
> data is received by the program in question, yet each time I add a new
> element of memory onto my array, all the previous elements are cleared.  This
> is rather annoying.  Am I doing something wrong?  The manuals I have read
> say that these functions will indeed clear newly allocated memory.  Are there
> any other functions which do not clear the whole array?  I can understand
> it clearing just the newly allocated memory element, but not the whole set
> of elements.  Why oh why is this so?  Any help you guys can provide a wayward
> C programmer would be appreciated.  I am learning C because I want to write
> a particular mathematics related peice of software and C seems like a good
> choice in which to write this software.

> Thanks Much


> Stan Horwitz
> V4039 at TEMPLEVM.BITNET

It sounds like you have a bug in your program.

Calloc does in fact clear out the block of memory.  Malloc does not
(it just so happens that if malloc is allocating 'virgin' memory (see sbrk(2)),
then it is cleared by the system).
The only difference between malloc and calloc is that calloc does
a multiply (i.e. nelem * elsize), calls malloc and then clears the memory.
Malloc and calloc will only return 'unused' memory.  That is, memory that has
been freeed (via a call to free() by your program) or by expanding your 
programs data area via sbrk().
If you find data in your program is disappearing, I would suggest that
you take a closer look at your code.  One of the easiest things to do
is to over write allocated memory with a run away pointer or overwrite the
pointer itself.  If you need to change the size of a previously allocated 
piece of memory, you can always use realloc.


I rewrote a version of malloc,calloc, etc. once upon a time (in a
previous life :-)) that made
better use of system memory resources (malloc is very fast at reallocating
previously freeed memory but does no garbage collection so memory can 
become very fragmented under certain conditions).  
I also included a debug (compile time) option.  
What I found is that many programs, that were written by inexperienced
programmers in our group, were trashing allocated memory.  These bugs
did not show up under normal circumstances (luck).. I was just amazed that the
programs ran at all :-). 
I wish that malloc had some sort of debug option available because these 
types of bugs can be very difficult to find.

If you do not have a nice debugger, try putting more diagnostics into
your program (e.g., printf when ever you use malloced memory or when
ever you malloc new memory.)  There is nothing magic about memory allocation.


Note: it is very important to only free memory allocated.



Michael D. Tracy	Computer Consoles Incorporated
(714)458-7282		9801 Muirlands Boulevard
			Irvine, CA 92718
{allegra!hplabs!felix,seismo!rlgvax}!ccicpg!miket

eva@cblpf.ATT.COM (Eva Martin) (01/28/88)

In article <9991@ccicpg.UUCP> miket@ccicpg.UUCP (Mike Tracy) writes:
>
>If you do not have a nice debugger, try putting more diagnostics into
>your program (e.g., printf when ever you use malloced memory or when
                                                                 ^^^^
>ever you malloc new memory.)  There is nothing magic about memory allocation.
 ^^^^^^^^^^^^^^^^^^^^^^^^^^

I tried this once.  What happened was I got to find out how quickly the
stack would overflow.  printf is a buffered write.  It calls malloc.