Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: writing a c program to substitute text Date: Tue, 28 Feb 2017 08:46:18 -0800 Organization: None to speak of Lines: 85 Message-ID: References: <6b1df453-b6df-3cf8-eb6e-255c37ef241a@verizon.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="6f3f7ca58527ffa674d8982151c3ccd8"; logging-data="1028"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+K4Hs0xzqIIjQ/gpb+FTRc" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:mVVfrgroMCRbHOWhuIeL9YJbSzc= sha1:ZoYEOE0Na3iaigJAt5ubguWzunA= Xref: csiph.com comp.lang.c:105110 fred flintstone writes: > On 02/26/2017 03:55 PM, James R. Kuyper wrote: >> On 02/26/2017 07:14 AM, fred flintstone wrote: [...] > Listen to me james. Have you ever used 'malloc' as a verb? If you have a > declaration like: > > const char *parrow = "arrow"; > > , can one say that it has been malloced, or is it only correct to use > the grown-up term of "allocated?" (Might there be a malloc right under > the patina?) I won't speak for James, but I've certainly used malloc as a verb. But it would not be correct to say that anything has been malloced in the above declaration. Memory for the pointer object parrow is allocated either at program startup or on entry to the enclosing block, depending on whether it's defined inside or outside a function body. Memory for the string "arrow" is allocated at program startup. (At least logically; an implementation can arrange for them to be allocated in any way that produces the same semantics.) It's unlikely that any of these will be allocated via an underlying call to malloc. >>> Do I malloc a 1000 because someone has to make an adult decision? It's hard to tell what you mean here, but I'd suggest that allocating some amount that you assume is much more than you'll actually need is *probably* poor practice. For example, if you allocate 1000 bytes to hold a line of text read from a file, then most of that space will be wasted in the usual case. On the other hand, I've certainly dealt with text files with lines longer than 1000 bytes, and most of the tools I use can deal with them just fine. On the other other hand, allocating a large fixed chunk of memory is simpler and can often be quicker. But if you can manage to allocate just what you need, whether it's large or small, that can often (but not always) be the best approach. [...] > while (1) { > c = fgetc (fptr); > > if (c == EOF) > break; > else > putc (c, gptr); > } The usual idiom for this kind of loop is: while ((c = fgetc(fptr)) != EOF) { putc(c, gptr); } [...] > char * > mystdrdup (const char *str) > { > size_t len = strlen (str); > char *answer = malloc (len + 1); > if (answer) > strcpy (answer, str); > else { /* error handling here */ > }; > > return answer; > } *If* you need this function (you probably don't), it doesn't need any error handling other than returning a null pointer. A low-level function like this has no way of knowing how an allocation failure should be handled. Just let the caller know it failed, and the caller can decide how to proceed. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"