[m]allocation question

Adam Stoller ghoti+ at andrew.cmu.edu
Wed Sep 19 22:56:11 AEST 1990


I've been looking at a book, "Algorithms in C" by Robert Sedgewick - and
although some of the coding examples are fairly sloppy (in terms of
error checking) I can understand that since that isn't the focus of the
book.  

However there is at least one piece of code where he allocates pointers
in a way I've never seen before -- and it isn't clear to me whether it
should work -- and was wondering if anyone could clarify it for me (in
regards to whether it works according to the Standard).

Example:

    static struct node
      { int key; struct node *next; };
    static struct node *head, *z, *t;
.....
    push (int v)
      {
        t = (struct node *) malloc(sizeof *t);
.....

Isn't *t garbage at the time the sizeof is performed - isn't this
[almost?] de-referencing a NULL pointer.

Traditionally, I believe, the above line is written:
        t = (struct node *) malloc (sizeof (struct node));
so my question is not how to correctly write the allocation, but whether
his method of writing it is at all acceptable?

Primarilly curious,

--fish



More information about the Comp.std.c mailing list