Another one for the list

D

Dave Vandervies

Seen in another place:
--------
typedef void my_lib_void;
my_lib_void my_lib_func(my_lib_void);
--------
(summarized from what should be appearing at
<http://yossi.kreinin.hates-software.com/2007/07/10/7a21fc28.html>
eventually, though it's broken as I'm writing this)

This is perfectly valid C; I don't know why anybody would actually WANT
to do it, but that apparently doesn't stop people.

It's reported that a C++ compiler will choke on it, though.


dave
 
B

Ben Pfaff

Seen in another place:

Wow. Only slightly more moronic than a certain library that
typedefs each of the standard C types to a similar name beginning
with "g", though.
 
S

sumedh

I tried running following c++ code
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
typedef void my_lib_void;
printf("Hello void %d",sizeof(my_lib_void));
system("PAUSE");
return 0;
}
it gives an error on second line of main()..
but at same time if c code
int main()
{
typedef void my_lib_void;
printf("Hello void %d",sizeof(my_lib_void));
system("PAUSE");
return 0;
}
it works showing sizeof(my_lib_void) as 1
Can anyone explain why?
cheers,
Sumedh
 
S

santosh

sumedh said:
I tried running following c++ code
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
typedef void my_lib_void;
printf("Hello void %d",sizeof(my_lib_void));
system("PAUSE");
return 0;
}
it gives an error on second line of main()..
but at same time if c code
int main()
{
typedef void my_lib_void;
printf("Hello void %d",sizeof(my_lib_void));
system("PAUSE");
return 0;
}
it works showing sizeof(my_lib_void) as 1
Can anyone explain why?

Please don't top-post. Also post C++ related questions to a C++ group
like comp.lang.c++.

Both your programs make no sense. What's the point in trying to take
the size of a non-existent type? C++'s rules are strict enough to
prevent compilation, while in C it compiles with a diagnostic and
produces nonsense as result.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Ben said:
Wow. Only slightly more moronic than a certain library that
typedefs each of the standard C types to a similar name beginning
with "g", though.

Which standard (the standard for whichever version of the language the
library is written in) types would those be?
 
R

Richard

Harald van Dijk said:
Which standard (the standard for whichever version of the language the
library is written in) types would those be?

GTK does something similar. And for very good reasons. Nothing "moronic"
about it at all.
 
R

Richard

Richard said:
GTK does something similar. And for very good reasons. Nothing "moronic"
about it at all.

Actually I take that back since they do more than add a "g" or whatever
: they have their own types typedef'd back to the core C ones.
 
S

santosh

Richard said:
GTK does something similar. And for very good reasons. Nothing "moronic"
about it at all.

The Windows API is an infamous example of this too.
 
E

Eric Sosman

Ben Pfaff wrote On 07/10/07 11:04,:
Wow. Only slightly more moronic than a certain library that
typedefs each of the standard C types to a similar name beginning
with "g", though.

How old is the code? Might it have been an attempt
to sneak void past a pre-Standard compiler that didn't
have it? On an old compiler, you'd typedef my_lib_void
to something "harmless" like int ...

But then, a compiler lacking void probably didn't
understand prototypes, either. So maybe it's merely
moronic. "Never attribute to malice that which can be
adequately explained by stupidity."
 
W

Walter Roberson

sumedh said:
but at same time if c code
int main()
{
typedef void my_lib_void;
printf("Hello void %d",sizeof(my_lib_void));
system("PAUSE");
return 0;
}
it works showing sizeof(my_lib_void) as 1
Can anyone explain why?

Because you are using gcc without the various options that would
flag the use of non-portable gcc extensions.
 
B

Barry

Richard said:
GTK does something similar. And for very good reasons. Nothing "moronic"
about it at all.

The Windows API is an infamous example of this too.

Arguably, Windows has produced a more consistent interface over the
years than many *nix implementions. But it is outside the realm of clc.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Ben said:
What are these "very good reasons"?

GLib's typedefs (the ones relevant to your message) are either 1) because
standard C90 doesn't provide a type with the meaning GLib needs, or 2) for
consistency with the types defined for reason 1.
 
R

Richard

Barry said:
The Windows API is an infamous example of this too.

Arguably, Windows has produced a more consistent interface over the
years than many *nix implementions. But it is outside the realm of clc.


It is in the realms of this thread and the naming of data types.
 
R

Richard

Harald van Dijk said:
GLib's typedefs (the ones relevant to your message) are either 1) because
standard C90 doesn't provide a type with the meaning GLib needs, or 2) for
consistency with the types defined for reason 1.

And cross platform compilation where the "basic" types *might* differ ....
 
K

Keith Thompson

santosh said:
Please don't top-post. Also post C++ related questions to a C++ group
like comp.lang.c++.

Both your programs make no sense. What's the point in trying to take
the size of a non-existent type? C++'s rules are strict enough to
prevent compilation, while in C it compiles with a diagnostic and
produces nonsense as result.

void is not a nonexistent type; it's merely an incomplete type.

Applying sizeof to an incomplete type is a constraint violation
requiring a diagnostic.

<OT>gcc, in order to allow pointer arithmetic on void* sets
sizeof(void) to 1. This extension is disabled if you use the right
options.</OT>
 
C

CBFalconer

sumedh said:
I tried running following c++ code
#include<stdio.h>
#include<iostream>
using namespace std;

Which is definitely not C code. So what is it doing in c.l.c?
 
B

Ben Pfaff

Harald van Dijk said:
GLib's typedefs (the ones relevant to your message) are either 1) because
standard C90 doesn't provide a type with the meaning GLib needs, or 2) for
consistency with the types defined for reason 1.

You are saying that standard C90 doesn't provide, for example, a
type with the meaning of "gint", which is typedefed to int? The
same question can be applied to gchar, gshort, glong, gfloat,
gdouble, and possibly more.
 
K

Keith Thompson

Ben Pfaff said:
You are saying that standard C90 doesn't provide, for example, a
type with the meaning of "gint", which is typedefed to int? The
same question can be applied to gchar, gshort, glong, gfloat,
gdouble, and possibly more.

I think that GLib provides a number of typedefes for types that may or
may not be provided by the C implementation (gboolean, gssize); it
also provides typedefs for the predefined types for the sake of
consistency.

This means that, for example, a programmer using GLib doesn't have to
remember that size_t is standard C, bool is standard only in C99, and
ssize_t is not in any C standard.

Personally, I find the idea of extending this to "gint" and "gchar" to
be a bit much, but in terms of consistency it's not entirely absurd.

(I wonder why they have gchar and guchar but not gschar, but that's a
GLib question, not a C question.)

See <http://developer.gnome.org/doc/API/2.0/glib/glib-Basic-Types.html>.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Ben said:
You are saying that standard C90 doesn't provide, for example, a
type with the meaning of "gint", which is typedefed to int? The
same question can be applied to gchar, gshort, glong, gfloat,
gdouble, and possibly more.

No, that's not what I said or meant. Those types are defined for the second
reason: consistency with the other types defined by GLib that aren't
available in standard C90.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top