What is strdup() supposed to do? Does anyone have a copy?

Christopher R Volpe volpe at camelback.crd.ge.com
Fri Feb 22 00:33:10 AEST 1991


In article <FISCHER.91Feb20184612 at thiele.iesd.auc.dk>,
fischer at iesd.auc.dk (Lars P. Fischer) writes:
|>>>>>> dag at persoft.com (Daniel Glasser) writes:
|>
|>Daniel>Nit #2: You should simplify your expression in the "malloc()",
that is,
|>Daniel>		sizeof(char) * strlen(foo) + sizeof(char)
|>Daniel>	  could be written
|>Daniel>		sizeof(char) * (strlen(foo) + 1)
                                       ^
                                       |-This paren isn't closed, and
                                         may be causing much of the confusion.

|>
|>>>>>> jeff at onion.rain.com (Jeff Beadles) then said:
|>
|>Jeff> Untrue.  What if sizeof(char) != 1?  Yours will break, and mine
|>Jeff> will work.
|>
|>Gimme a break. Are you going to tell us that
|>
|>	A * (B + 1) != A * B + A
|>
|>for A != 1 ?? If you can't handle basic arithmetic, what makes you
|>think you can write a C program?

I think Lars and Jeff are talking about different expressions here, due to
the missing closing parenthesis. Jeff saw the expression
         sizeof(char) * strlen(foo) + 1 (he didn't see the '(' before 'strlen')
and noted that this wouldn't be equal to 
         sizeof(char) * strlen(foo) + sizeof(char)
in the event that sizeof(char) != 1. This is absolutely true, but a moot
point since sizeof(char) == 1 by definition. 

However, Lars saw the expression
         sizeof(char) * (strlen(foo) + 1) (he added the missing paren)
and noted that this is equal to
         sizeof(char) *  strlen(foo) + sizeof(char)
by mathematical identity (distribution of multiplication over addition)
regardless of whether or not sizeof(char) == 1.

The non-expression originally posted by Daniel, however, was not what
either of them saw, and they each extrapolated in different directions
to arrive at what they thought Daniel intended to write. I hope this
clears up the confusion.

-Chris                       
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list