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 13:39:50 -0700 Organization: None to speak of Lines: 84 Message-ID: References: <577ba42f.13700328@news.xs4all.nl> <48c43c3f-8e7e-0509-5b9d-04133e7bb537@verizon.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="10454"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/GU7uA/0VfH+BgYJLoQDx/" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:CW3yHX6Xj3aoN/rn9InW6lpeaaQ= sha1:n7Z61f8N5B9gRyVudN+sCUn3ojs= Xref: csiph.com comp.lang.c:90157 David Brown writes: > On 09/07/16 16:16, James R. Kuyper wrote: >> On 07/09/2016 01:13 AM, Nobody wrote: >>> On Wed, 06 Jul 2016 15:22:51 -0400, James R. Kuyper wrote: >>> >>>>> Casting is /less/ common in C++ than in C -- there's nothing to lament! >>>>> Unless you compile C code with a C++ compiler, which is generally a bad >>>>> idea. >>>> >>>> Casting (preferably by using static_cast<>) is required by C++ to convert >>>> a void* to a pointer to object type (C++ 2011 C.1.3, cross-referencing >>>> 4.10). Such conversions do not occur implicitly the way they do in C. >>> >>> OTOH, C++ code typically has fewer reasons to use void* in the first place. >>> >> >> True - but the case under discussion is one of those reasons. In pure >> C++ code, calls to malloc() should almost always be replaced by more >> type-safe approaches such as direct or indirect use of the 'new' >> operator. But in code meant to be usable in both C and C++, it's >> annoying that casting the value returned by malloc() is mandatory in >> C++, and a bad idea in C. >> > > 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. And if that is likely enough for the > extra cast to be a risk of bugs or of extra effort, then you are > probably going to have to change a lot more code anyway - and you are > better off writing: > > typedef int elemType; > ... > elemType* array = (elemType*) malloc(10 * sizeof(elemType)); > > Or you can put the malloc inside a function or macro so you don't have > to use the cast throughout the code. Your defense of the cast seems to be that it's only *slightly* inconvenient and error-prone. Why do the cast at all? What advantage does using the cast have over simply *not* using the cast? Why should I write some_type *ptr = (some_type*)malloc(sizeof (some_type)); rather than some_type *ptr = malloc(sizeof *ptr); ? The version with the cast, writing the type name three times, isn't terribly difficult, and I can certainly manage to do it correctly if I choose to. But why should I when there's a simpler and more robust alternative? > Casting the result of malloc is neither hard nor error-prone - it's just > one of these things that some C programmers think is important to avoid > because using the cast makes your code look like C++. It is much like > some C++ that get upset when someone writes "void foo(void);", because > in C++ it "should" be written "void foo();". Not at all. My reasons for not casting the result of malloc() have absolutely nothing to do with making my code "look like C++". I avoid the cast because code without the cast is shorter, cleaner, and more robust than code with the case. > Sometimes writing your C code in a C++ compatible manner involves a > little extra effort, but it is rare (IME) that the result is "bad" C code. If I needed to write C code that can also be compiled as C++ then sure, I'd cast the result of malloc(). But that's rarely necessary or appropriate, given the excellent support C++ has for interfacing to pure C code. If there were some advantage to adding the cast, I'd add it. There isn't, so I don't. -- 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"