Return to BSD News archive
Xref: sserve comp.unix.bsd:5917 comp.sys.ibm.pc.rt:1682 comp.unix.large:612 Newsgroups: comp.unix.bsd,comp.sys.ibm.pc.rt,comp.unix.large Path: sserve!manuel!munnari.oz.au!sgiblab!spool.mu.edu!agate!linus!linus.mitre.org!heckle!wheeler From: wheeler@heckle.mitre.org (Brien L. Wheeler) Subject: Re: strdup Message-ID: <1992Oct1.122748.12067@linus.mitre.org> Sender: wheeler@heckle (Brien L. Wheeler) Nntp-Posting-Host: heckle.mitre.org Organization: The MITRE Corporation, Bedford, MA References: <br.pct.52.717898310@RLG.Stanford.EDU> <lckirmINNosg@spim.mti.sgi.com> Date: Thu, 1 Oct 1992 12:27:48 GMT Lines: 24 In article <lckirmINNosg@spim.mti.sgi.com>, koblas@mips.com (David Koblas) writes: > Here's a copy (typed in off the cuff, so I won't say it is guaranteed to work): > > char *strdup(char *str) > { > char *cp = (char *)malloc(strlen(str)); > return cp ? strcpy(cp, str), cp : NULL; > } Try this instead -- it mallocs a byte for the NULL terminating character. (There is no need for "strcpy(cp,str),cp" as strcpy always returns its first argument.) char *strdup(char *str) { char *cp = (char *)malloc(strlen(str)+1); return cp ? strcpy(cp,str) : NULL; } ----- Brien L. Wheeler | I don't speak for MITRE, and they blw@mitre.org | don't speak for me. "When I think back on all the crap I learned in high school..."