Article View: comp.std.c
Article #1748[m]allocation question
From: Adam Stoller
Date: Wed, 19 Sep 1990 12:56
Date: Wed, 19 Sep 1990 12:56
32 lines
1541 bytes
1541 bytes
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
Technical Details
Message-ID:
<UaxqffS00Vtq8akksB@andrew.cmu.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!snorkelwacker!mit-eddie!uw-beaver!cornell!rochester!pt.cs.cmu.edu!o.gp.cs.cmu.edu!andrew.cmu.edu!ghoti+Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!snorkelwacker!mit-eddie!uw-beaver!cornell!rochester!pt.cs.cmu.edu!o.gp.cs.cmu.edu!andrew.cmu.edu!ghoti+ From: ghoti+@andrew.cmu.edu (Adam Stoller) Newsgroups: comp.std.c Subject: [m]allocation question Message-ID: <UaxqffS00Vtq8akksB@andrew.cmu.edu> Date: 19 Sep 90 12:56:11 GMT Organization: Information Technology Center, Carnegie Mellon, Pittsburgh, PA Lines: 32