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: double free Date: Sat, 09 Jul 2016 17:32:03 -0700 Organization: None to speak of Lines: 29 Message-ID: References: <577ba42f.13700328@news.xs4all.nl> <48c43c3f-8e7e-0509-5b9d-04133e7bb537@verizon.net> <1b60sel6mc.fsf@pfeifferfamily.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="20371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zlDVgbdIxe64TndREu676" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:zCMPuP7YmO187Ez784lZ9Q8Xi+g= sha1:0lAThKo6MdAWJs8K04riLDjdP6o= Xref: csiph.com comp.lang.c:90164 Joe Pfeiffer writes: > David Brown writes: [...] >> Casting the return value from malloc is not "a bad idea". It is no >> more and no less than an extra repetition of types you are already >> using: >> >> int* array = (int*) malloc(10 * sizeof(int)); >> >> Yes, you need to write the same type three times if you want to >> express your allocation in this manner. But so what? The cast does >> no harm to your code, and it is only really relevant if you need to >> change the type in some revision of the code. > > Of course, you shouldn't be using sizeof(int) either -- you should be > using sizeof(*array) (I would argue that this is a case where the > unnecessary parentheses make 10 * sizeof(*array) ). > > Now the hypothetical change in data type is automatically taken care of. I'd also question whether an object of pointer type should be called "array". (In real code, it should probably have a name that corresponds to what it's used for.) -- 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"