From: David Schwartz on
On Feb 24, 3:57 pm, Nicolas George <nicolas$geo...(a)salle-s.org> wrote:
> Moi  wrote in message
>
> <7ffbd$4b85aa91$5350c024$29...(a)cache120.multikabel.net>:
>
> > sync_core = (struct sync_proc*) shmat(shmid, NULL, 0);
>
> sync_core = shmat(...);
>
> Casting from void * is useless in C

No, it's not useless. It indicates to a reviewer that the programmer
intended the conversion and is claiming that it was safe and valid.

> and may hide legitimate warnings, for
> example if the header declaring shmat was forgotten and thus shmat got the
> implicit int return type.

True, but leaving it out hides legitimate warnings. The cast warns
anyone looking at the code that there's a cast whose safety/validity
they must check in order to review the code thoroughly.

The advantages and disadvantages on both sides are quite comparable
and it is totally incorrect to argue that one way or the other is
clearly correct.

DS
From: Nicolas George on
David Schwartz wrote in message
<b7fdd7cc-2bad-43d7-93b7-6b6e4bba260d(a)d2g2000yqa.googlegroups.com>:
> True, but leaving it out hides legitimate warnings. The cast warns
> anyone looking at the code that there's a cast whose safety/validity
> they must check in order to review the code thoroughly.

If you want to do that, you can put the cast in a comment:

name = /* (char *) */ malloc(len);

Or use an empty macro:

#define VOID_CAST(type)

name = VOID_CAST(char *) malloc(len);

This has exactly the same documentation purpose, but contrary to an actual
cast, it is neutral with regard to the compiler's warnings.
From: David Schwartz on
On Feb 25, 12:53 am, Nicolas George <nicolas$geo...(a)salle-s.org>
wrote:

> If you want to do that, you can put the cast in a comment:
>
> name = /* (char *) */ malloc(len);
>
> Or use an empty macro:
>
> #define VOID_CAST(type)
>
> name = VOID_CAST(char *) malloc(len);
>
> This has exactly the same documentation purpose, but contrary to an actual
> cast, it is neutral with regard to the compiler's warnings.

I agree.

DS
From: Eric Sosman on
On 2/25/2010 6:17 PM, David Schwartz wrote:
> On Feb 25, 12:53 am, Nicolas George<nicolas$geo...(a)salle-s.org>
> wrote:
>
>> If you want to do that, you can put the cast in a comment:
>>
>> name = /* (char *) */ malloc(len);
>>
>> Or use an empty macro:
>>
>> #define VOID_CAST(type)
>>
>> name = VOID_CAST(char *) malloc(len);
>>
>> This has exactly the same documentation purpose, but contrary to an actual
>> cast, it is neutral with regard to the compiler's warnings.
>
> I agree.

Does anybody have a barf bag I can use? Mine's full.

(Hhyyuuuccckkk-bfltsch!)

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Rainer Weikusat on
David Schwartz <davids(a)webmaster.com> writes:
> On Feb 24, 3:57�pm, Nicolas George <nicolas$geo...(a)salle-s.org> wrote:
>> Moi �wrote in message
>>
>> <7ffbd$4b85aa91$5350c024$29...(a)cache120.multikabel.net>:
>>
>> > sync_core = (struct sync_proc*) shmat(shmid, NULL, 0);
>>
>> sync_core = shmat(...);
>>
>> Casting from void * is useless in C
>
> No, it's not useless. It indicates to a reviewer that the programmer
> intended the conversion and is claiming that it was safe and valid.

It indicates to a reviewer that whoever wrote the code had a C/C++
confusion problem. The sole purpose of 'a pointer to void' in C is to
have a generic pointer type which is assignment compatible with every
other pointer type (minus pointers to functions), mostly for the
reason that this enables easy use of memory allocation routines which
do not allocated typed objects but storage areas of particular sizes.

Usually, this will look like this:

struct blah *blah;

blah = malloc(sizeof(*blah));

and the part a reviewer needs to look at is the argument to malloc,
because it is completely possible to write

struct blah {
double data[1748];
};

struct blah *blah;

blah = (struct blah *)malloc(1);

and this conversion is decidedly not correct.

[...]

> The advantages and disadvantages on both sides are quite comparable
> and it is totally incorrect to argue that one way or the other is
> clearly correct.

A programming language usually has an annotation facility ('comments')
with a specific syntax which explictly informs a read that a
particular text is supposed to be documentation. This means someone
who is really convinced that the fact that malloc does not return a
pointer to an object of some type must be mentioned whenever malloc is
used would be free to write

struct blah *blah;

/*
Remember that malloc allocates typeless
storage areas of a certains size.
*/
blah = malloc(sizeof(*blah))

This communicates the same information and doesn't have the downside
that someone could accidentally confuse it with code which has
presumably has a purpose.