www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/12/05:00:45

Message-ID: <35D158A1.83BC6888@geocities.com>
From: Merlin <merlin__ AT geocities DOT com>
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: malloc()...free()...WTF??
References: <6qr22o$rjn$1 AT birch DOT prod DOT itd DOT earthlink DOT net>
Lines: 43
Date: Wed, 12 Aug 1998 08:58:58 GMT
NNTP-Posting-Host: 196-cy-wpg.ilos.net
NNTP-Posting-Date: Wed, 12 Aug 1998 03:58:58 CDT
Organization: MBnet Networking Inc.
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


Steven "Ionicis" An wrote:

> OK, I understand that malloc() and free() allocate and free memory, but how
> does memory work in general?  Like, what's the heap and how does an array
> manage itself?  I might not sound too clear about wtf I wanna know, but that
> just shows how confused I am about memory, pointers, etc.  I know C pretty
> well...but it's just this memory allocation crap I can't understand...I
> don't understand.  Is there a page that explains how C programs handle
> memory?  Do I have to learn assembly to understand this?
>
> Eh...a website (URL) would be best...or if you care, write me an explanation
> yourself!  Oh well, whatever you do, thanks for reading...

  ok.... I have no url....and i recommend getting a good book on c..perhaps
maybe advanced c....but i will do my best to explain it anywayz...
well now...ok....let me think....where o begin...ahh yes...
ok....when you make an normal array(not a pointer) it stored it'sself in
memory....in the 64k reserved for variables...very wastful for large arrays
IMO.... but anyway.. since the size is pre-specified...it will just find a place
where it will fit....
When you declare a pointer....it doesn't use the 64k which is allocated for
variables... so you have to call malloc or calloc to give it a size.. example:
int  *a_ptr = (int *)malloc(64000)  //calloc(64000,1) can be used instead of
malloc
ok...this would make an array-like pointer with 64000 spaces...
access them by doing:
a_ptr[#]
just like an array really....just doesn't use the space allocated for your
normal variables.
to free the memory used by the pointer use:
free(a_ptr);

there..i hope i have answered your question.... even though it was not exacly
djgpp related...oh well.. I still recommend you get yourself a good book with
pointer info in it...

l8r.
       Merlin.



- Raw text -


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