Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: C wish list Date: Mon, 25 Mar 2019 13:05:09 -0700 Organization: None to speak of Lines: 41 Message-ID: References: <18RkE.296542$DH4.255445@fx24.am4> <1d54b052-49e7-4aee-a4cc-a394a0a77886@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="784bbd8fb1c90ff7036d4663e7b2295d"; logging-data="3087"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DZhTW7rR4gu7p66k16nBT" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:4VmvblaYie/SEZFg0//EWOlSelE= sha1:1ToK5OONbLkDY+yFwpqGuv7h5sY= Xref: csiph.com comp.lang.c:142434 Richard Damon writes: > On 3/25/19 12:54 AM, Keith Thompson wrote: > [...] >> The heap might be *implemented* as a single character array, but it's >> not an object as far as C is concerned. This code fragment: >> >> char *a = malloc(1); >> char *b = malloc(1); >> a < b; >> >> has undefined behavior (unless, I think, one of the pointers happens >> to point just past the object pointed to by the other pointer) -- >> regardless of how malloc is implemented. > > The behavior is defined IF the heap did start as a single array, so that > the pointers are all part of a single object. Since we, as users of the > implementation, don't know that for sure, we can't depend on it. The > implementer of malloc, might know that it is true, and could then depend > on that fact. I don't believe that's correct. I don't think that the definedness of `a < b` depends on how malloc is implemented. If I write my own "my_malloc()" function that returns pointers to chunks of a single static array, then `a < b` is well defined. But if that same code is used *by the implementation* to implement `malloc`, then that code is not part of the program, and the behavior of `a < b` is undefined (meaning it's not defined by the language). > You CAN write malloc and family in portable C (at least if you are using > a recent enough version of C to get the alignment right, or use > knowledge of what is the strictest alignment type). Agreed. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */