delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/25/03:43:55

From: "John M. Aldrich" <fighteer AT NO DOT SPAM DOT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: memory allocation: malloc vs. calloc
Date: Thu, 24 Apr 1997 12:32:41 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 31
Message-ID: <335F52E9.2CAB@NO.SPAM.cs.com>
References: <Pine DOT D-G DOT 3 DOT 91 DOT 970424102456 DOT 20048B-100000 AT dg1> <5jops9$sbc AT lyra DOT csx DOT cam DOT ac DOT uk>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp204.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Michael Meeks wrote:
> 
>         I'm lost here, which is the alloc that gets temp. memory from the
> stack, and saves fragmentation ? and is there a variant of that that zeros
> the memory ?

Hehe... loads of *alloc* questions.  It's time to confuse the newbies! 
;)

malloc, calloc, and their relative, xmalloc, allocate memory from the
heap (i.e., permanent storage).  malloc() is the standard function,
calloc() zeroes the memory it allocates, and xmalloc() acts like
malloc() but prints an error and exits if it can't get enough memory.

alloca() is the function that grabs memory from the stack.  Any memory
grabbed in this way vanishes when the function exits.  alloca() does NOT
zero memory, but it's not like you can't do that yourself.  Please note
that this function can eat up tremendous amounts of stack space, so use
it for small structures only or else you'll need to boost your program's
stack by setting the _stklen global variable.

WARNING:  Failure to ensure that you have sufficient stack space can
cause your programs to crash in really _strange_ ways.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary  |
|                      - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------

- Raw text -


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