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: memcpy can't take null. Date: Wed, 08 Apr 2020 13:33:23 -0700 Organization: None to speak of Lines: 29 Message-ID: <87a73llnu4.fsf@nosuchdomain.example.com> References: <6c3de07e-15c8-4cc0-a805-636a5976d086@googlegroups.com> <87zhbnl10s.fsf@nosuchdomain.example.com> <8338dd09-dfed-447b-adf0-356695b1b1d8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="867f1cde61f02deaed0b5cba65780397"; logging-data="15615"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19poqhnJVT4xjKl338lZmU5" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:eERYyDCobafbOm4ZGRCeAS8JLCU= sha1:mqcaXlGfZo6uEZnhGAJNm7FwJo8= Xref: csiph.com comp.lang.c:151241 Malcolm McLean writes: [...] > If Nemployees is non-zero whilst the pointer employees points to invalid > memory, it's likely that the bug will be discovered soon enough. > But if Nemployees is zero and employees is NULL, then the loops through > the employees array will all be no-ops and have defined, correct > behaviour. It's only if you call a library function like memcpy that > the code becomes incorrect. Your function that takes a pointer to an employee structure and a count indicating the number of elements in the array should be accompanied by documentation (at least a comment) that tells the user what combinations are valid. If you write a call that violates those requirements, that call is incorrect -- even if the function happens not to misbehave. If that documentation says that func(NULL, 0) is valid and func(NULL, 1) is not, that's fine -- but then you need to guarantee that func(NULL, 0) won't blow up in future versions unless you want to risk breaking existing code. See memcpy for an example of this. Ideally, your function should be documented as clearly. If it isn't, it might be impossible to tell whether a given call is correct or not. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */