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: Correct way of doing string manipulation functions Date: Fri, 13 Jan 2017 12:14:09 -0800 Organization: None to speak of Lines: 58 Message-ID: References: <1414fce8-8d42-4ced-8289-2eaca032baef@googlegroups.com> <2ed0e3b8-3330-4854-bb4a-207d2c764978@googlegroups.com> <71d645f1-065f-40fb-8130-010950fd5166@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="5af804fde095f0102ceb1f5e23093854"; logging-data="32712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vgsdf0PEHkSamLw6z9Pft" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:3NxcQsLDtAcnlfCK4+XgfhUjF4s= sha1:NwRKN6njkXvVf5guDdEa6rdZPOk= Xref: csiph.com comp.lang.c:101655 Malcolm McLean writes: > On Friday, January 13, 2017 at 6:17:56 PM UTC, Richard Heathfield wrote: >> On 13/01/17 17:45, Malcolm McLean wrote: >> >> > Anything which implements its string copy function as a tight loop >> > reading 4 or 8 bytes at a time. Any string copy function that requires the string to be strictly aligned does not work correctly on C strings. >> > Or anything which does runtime >> > checks for valid UTF-8. >> >> If that's a problem at all, it's already a problem for strings. Why >> would it be an /extra/ problem for substrings? >> > The compiler can and usually will prevent string from being created > with unaligned first addresses, on platforms where unaligned accesses > are cheaper or easier to integrate with other code. It can't do > anything if you take a suffix. If I define an array object containing a string: const char arr[] = "hello"; then there's no guarantee that arr is strictly aligned. It likely is, but nothing in C requires it, and if you're writing code that assumes it is, your code is broken. > There does need to be a fix, but that fix might involve patching in > some special code, or even prohibiting the C from passing an unaligned > string at all. Part of the art of programming is to be nice to other > people, by which I don't mean old ladies (except old ladies who also > happen to be programmers). Suppose you have a string "foobar.txt", and you want just the "txt" suffix as a string: const char *filename = "foobar.txt"; const char *suffix = filename + 7; // simplified for this example `filename` *might* be strictly aligned. If it is, `suffix` certainly is not. What's your solution? Allocate another strictly aligned array object, and copy the suffix to it -- using a copy that has to deal with unaligned memory anyway? In the name of efficiency? If you need a string to be aligned for some reason, sure, you'll have to do that kind of thing. But most of the time, you don't, and imposing a general rule to allow for the rare cases where you might need extra alignment is nonsense. -- 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"