🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #204

Re: C Programming : Need help please .

#204
From: Jack Klein
Date: Mon, 18 Jan 1999 00:00
40 lines
2121 bytes
On Mon, 18 Jan 1999 01:26:57 -0500, Martin Ambuhl
<mambuhl@earthlink.net> wrote:

> CalvinKong wrote:
> > 
> > I had find out the part that causing the problem . But I still don't know
> > why it occurs .. Logically , it should be okay . Here is a program that I
> > extracted out and modified , please discuss with me ...
> > ************************************************************
> > # include <stdlib.h>
> > typedef struct ListType {
> >  int       item;
> >  struct ListType *link;
> > }Node;
> > typedef Node *List;
> > 
> > int main()
> > {
> >  List temp;
> > 
> >  temp = (List)malloc (sizeof(List));
> 
> sizeof(List) is just the size of a pointer.
> If you would do with a typedef less, you would not confuse yourself:
>    Node *temp;
>    temp = malloc(sizeof *temp);
> /* or, to be slightly less safe, 'temp = malloc(sizeof(Node));' */
> remember to check the return value from malloc.

<Jack>

Martin is absolutely correct, but to be slightly MORE safe:

temp = malloc(sizeof(*temp));

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.
Technical Details
Message-ID: <36c4dddd.604573317@netnews.worldnet.att.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <36a2cb23.0@news2.tm.net.my> <36A2D431.D11CAB41@earthlink.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 114592,44c9aa94f48f7403
X-Google-Attributes: gid114592,public
X-Google-Thread: 109edf,44c9aa94f48f7403
X-Google-Attributes: gid109edf,public
X-Google-Thread: 10d4ba,44c9aa94f48f7403
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 1011f8,44c9aa94f48f7403
X-Google-Attributes: gid1011f8,public
X-Google-Thread: f9d99,44c9aa94f48f7403
X-Google-Attributes: gidf9d99,public
From: jackklein@att.net (Jack Klein)
Subject: Re: C Programming : Need help please .
Date: 1999/01/18
Message-ID: <36c4dddd.604573317@netnews.worldnet.att.net>
X-Deja-AN: 433904710
Content-Transfer-Encoding: 7bit
References: <36a2cb23.0@news2.tm.net.my> <36A2D431.D11CAB41@earthlink.net>
Content-Type: text/plain; charset=us-ascii
Organization: AT&T WorldNet Services
Mime-Version: 1.0
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,jaring.pcbase,tmnet.communities,tmnet.support