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 10:11:00 -0800 Organization: None to speak of Lines: 42 Message-ID: References: <1414fce8-8d42-4ced-8289-2eaca032baef@googlegroups.com> <58789578.2159625@news.xs4all.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="5af804fde095f0102ceb1f5e23093854"; logging-data="28735"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZKlK0WnWwe9vIujuBdUHO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:TvKCInBxrfkH48T6P6SskdyvGTI= sha1:VsGqywKufTnIWbYV9nW/cPbMfKo= Xref: csiph.com comp.lang.c:101636 raltbos@xs4all.nl (Richard Bos) writes: > Richard Heathfield wrote: >> On 13/01/17 00:50, Malcolm McLean wrote: [...] >> > The reasons are firstly to avoid creating unaligned pointers, >> >> Huh? Consider: >> >> #include >> >> int main(void) >> { >> const char hw[] = "Hello, World."; >> const char *s = hw + 7; >> printf("%s\n", s); >> return 0; >> } >> >> Would you say that s is "unaligned"? If so, what do you mean by that? > > I would say the word "unaligned" is badly chosen, but I agree with > Malcolm that if you do these things without needing to, you may well > start changing s and wondering what happened to hw, or vice versa. If you do *anything* without needing to, you may run into problems. Since hw is defined as const, and s is a pointer to const char, there's no need to be concerned about accidentally modifying anything. I think Malcolms's concern is that the base address of the array object hw is likely to be on a 4-byte or 8-byte boundary, and the address of the character pointed to by s is not. But since s is a byte pointer, it is as aligned as it needs to be. Malcolm's concern about "unaligned pointers" is unfounded. [...] -- 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"