TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: James Macken
from: Michael Stapleton
date: 1993-09-01 00:08:00
subject: Re: Help Request Message: MALLOC

Hi James,
JM> In the great tradition of self-learnt C, I am not sure when a programmer
JM> should make use of the Memory Allocation command. From what I have
JM> observed:
  C is probably one of the hardest languages to teach yourself.  What other
languages do you know & were they all self taught?  Some face-to-face
tutoring can be invaluable in getting initial concepts straight.  This echo
& the international C_ECHO are pretty good, though.  Pity they didn't exist
20 years ago ...
  From the following example I guess you're coming from BASIC, right?  C
does strings quite a bit differently.  A C string is an array of char that
has a '\0' char after the last valid character in the string.  Thus you
have to know the maximum size that your string is (plus 1) to know how big
to make this array.  If you can fix this size at compilation time, then you
can use a regular array.  Otherwise, if you only know the array size at run
time, then you use a pointer and malloc() or calloc to get a block of
memory the right size.

JM> If you declare a pointer to a char or structure:
JM> char *cp[];
  This is *not* a pointer to a char, it is an array (of unspecified size)
of pointers to char.  This is a pointer to a char :
 char *cp;
  To use cp, we have to get a valid memory address in there.  We can assign
it a char address that we already know, or we can use malloc() etc to ask
the OS to give us a fresh chunk off the heap.  For example:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*  P O I N T E R   E X A M P L E S   */
/* Public domain code by M. Stapleton */
/* Written & tested Sep 1 1993        */

#include 
#include 
#include 

/* Some declarations */
char ach[] = "Hello, there";    /* char array (size 13)              */
char *cst = "Test String";      /* pointer to char, currently points */
                                /*  at a const array                 */
main(int argc, char *argv[])
{
    int i, j;
    char *cp;               /* Some char pointers */
    char *pst, *qst;
    char ch = 'z';          /* single character variable */

    if (argc>1)             /* Is there a command line parameter? */
        j = atoi(argv[1]);  /* Convert first string after command */
                            /*  name into integer */
    i = sizeof(ach);        /* Get the size in bytes of the ach array */
    j = j>i ? j : i;        /* j = maximum(j,i) */

    /* Some more legal pointer expressions */
    pst = &ch;      /* Put address of ch in pst, i.e make pst 'point at' ch */
    qst = ach;      /* Put address of the first char of array ach in pst */
    *cst = 'B';     /* Put char 'B' in address pointed to by cst */

    /* Ask the OS for a block containing 12 byte-sized units */
    cp = calloc(j, 1);
    if (cp != NULL)         /* Could we get it? */
    {
        /* DO NOT change the value in cp now, or we lose our block! */
        strcpy(cp, ach);    /* Copy contents of array ach */
                            /* to block pointed to by cp  */

        /* Scan through j chars, start at beginning of our block */
        for(i=0, pst=cp; i Since I am declaring a POINTER, must I declare a size NOW between the
JM> square brackets or will this be catered for when I MALLOC?
  Every data object has to have storage reserved for it.  Regular variables
and arrays have this space in the data segment of your program or on the
stack.  A pointer is a variable that contains an address value.  It doesn't
'contain' the object(s) it points to.  It has to point to some storage that
you own, either a regular named variable or an unnamed block that you just
got from malloc() or calloc().

JM> Am I right in thinking that all pointers to chars and structs must be
JM> malloc'ed?
  No!  I hope my example clarifies this point.  Don't hesitate to ask if
some of my explanation is obscure.  The concepts aren't too complicated,
but the English gets trickier than the C!  :-)

JM> Is there any circumstance when you declare:
JM> char ch[100];
JM> ..and would need to allocate memory?
  The declaration reserves memory itself.

I hope all this helps,

Michael Stapleton of Graphic Bits.

___I'm only here for the taglines.

--- Blue Wave/RA v0.7 Q-Blue

* Origin: The Three Amigas - better than two (3:713/615.0)
SEEN-BY: 54/54 99 711/401 430 807 808 809 932 934 712/627 713/111 317 601 611
SEEN-BY: 713/615 618 700 729 805 888 906 714/906
@PATH: 713/615 888 54/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.