Xref: utzoo comp.sources.wanted:15351 alt.sources.wanted:950 Path: utzoo!utgpu!cs.utexas.edu!uunet!ogicse!milton!sumax!polari!6sigma2 From: 6sigma2@polari.UUCP (Brian Matthews) Newsgroups: comp.sources.wanted,alt.sources.wanted Subject: Re: What is strdup() supposed to do? Does anyone have a copy? Message-ID: <3407@polari.UUCP> Date: 20 Feb 91 07:14:57 GMT References: <1991Feb17.164731.7564@onion.rain.com> <1991Feb18.154159.430@persoft.com> <1991Feb19.004526.16033@onion.rain.com> Followup-To: comp.lang.c Organization: Seattle Online Public Unix (206) 328-4944 Lines: 39 This has little to do with strdup any more, but there seem to be some fairly serious C misconceptions herein, hence pointing followups to comp.lang.c. In article <1991Feb19.004526.16033@onion.rain.com> jeff@onion.rain.com (Jeff Beadles) writes: | In <1991Feb18.154159.430@persoft.com> dag@persoft.com (Daniel Glasser) writes: | >Nit #1: Why is rets static? |A creature of habit, I suppose. I've been bitten too often when I return |something from a function that isn't static. (int's, for example.) For this |case, it really doesn't matter. The function return value should never have to be static. What may have to be static is the area pointed to by a returned pointer. A scalar (long, double, etc.) shouldn't have to be static, unless you have a very seriously broken compiler. |It's kinda like the braces around the malloc failure. They're not technically |required, but it's not a bad habit. :-) The braces around the malloc failure are mostly style, and have no affect on the final executable. Making things static does. Static variables take up space in the final executable on disk, must be read into memory, and take up memory all the time, even if they're never used. Automatics don't take disk space, and only use memory when their enclosing function or a children thereof are active. Making variables static that don't have to be *is* a bad habit. | >Nit #2: You should simplify your expression in the "malloc()", that is, | > sizeof(char) * strlen(foo) + sizeof(char) | > could be written | > sizeof(char) * (strlen(foo) + 1) |Untrue. What if sizeof(char) != 1? Yours will break, and mine will work. Last I looked, the distributive law still holds (ignoring overflow and other hard stuff :-)). Multiply the second expression and you'll see that it's (at least algebraically) equivalent to the first expression. Either will work for the purpose at hand. -- Brian L. Matthews 6sigma2@polari.UUCP