Date: | March 26, 2008 / year-entry #98 |
Tags: | history |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20080326-00/?p=22993 |
Comments: | 25 |
Summary: | In Windows header files, many structures are declared like this: typedef struct tagXYZ { ... } XYZ; typedef struct _XYZ { ... } XYZ; /* there are other variations, too */ Why is the structure name different from typedef name? This is a holdover from very early versions of the C language where structure tags,... |
In Windows header files, many structures are declared like this: typedef struct tagXYZ { ... } XYZ; typedef struct _XYZ { ... } XYZ; /* there are other variations, too */ Why is the structure name different from typedef name?
This is a holdover from very early versions of the C language
where structure tags, union tags,
and typedefs were kept in the same namespace.
Consequently, you couldn't say
The C language standardization process separated the structure
and typename name spaces, so this workaround is no longer necessary,
but it doesn't hurt either.
Besides, even if new structures followed the
Next time, why you also don't see the pattern
|
Comments (25)
Comments are closed. |
Raymond,
I always thought this had more to do with name mangling in C++ than C namespaces.
Thanks for teaching me something new!
This may sound like a dumb question, and the answer probably exists on the web, though I don’t know the right way to phrase it as such..
Which came first, windows or MSVC (or whatever the first microsoft compiler was that contained all the windows libraries and headers) ?
I’m wondering if MS had a C compiler on hand to develop windows, or if an alternate compiler was used to develop and compile windows, and later MS developed their own compiler.
I recall that win95 and MSVC 6 were released in roughly the same time frame (as in, I had a "wow, neat, a windows 95 box!" and MSVC 6 in the same first job… )
@Nathan_works: In the past, Microsoft didn’t have a compiler that was hosted on Windows. Quick C for Windows and Visual C++ 1.0 were the first.
Before that, you could use Microsoft C 6.0 to target DOS, Windows or OS/2, if I recall correctly.
If i remember correctly (it was many years ago) the first C compiler from microsoft was produced by Lattice…
<blockquote>Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.</blockquote>
I’m guessing this has to do with compiler error messages.
<i>Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.</i>
That seems trivial: you can’t then have pointers to the same type of struct within the struct.
"Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either."
My guess is because you can’t forward-declare a struct which has been defined that way. IIRC, it’s technically a typedef to an unnamed struct.
Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either."
Mostly, because I don’t believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)
The more basic question is "why do we need the pattern typedef struct XYX { … } XYZ; at all" (Answer, so we can refer to "struct XYZ" as just "XYZ" is a C program — The designers of C++ wisely put that ability right into the language without needing the typedef"
"Mostly, because I don’t believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)"
Anonymous struct types (structs without a tag naming them: `struct {int a, double b} foo={42,3.14};’) have been legal since at least 1989 when ANSI standardized the language; I don’t have a K&R1 handy to check, but I suspect it goes even farther back than that.
You may be thinking of anonymous struct members of another struct: ‘struct foo { struct {int a; double b;}; char *c;};’. The common extension is to allow access to the members of the anonymous inner struct as ‘x.a’ (instead of x.inner.a), but the construct isn’t legal C.
@Nathan_works:
The compiler released around Win95 timeframe was Microsoft Visual C++ 4.0. It was followed by versions 4.1, 4.2, 5.0 and finally 6.0 in 1998.
There was no MSVC++ 3.0 AFAIR.
One of the annoyances with this use of typedefs is that VC++ debugger doesn’t know whether to use a type’s real name (“tagXYZ”) or the typedef’d name (“XYZ”). Since a type can be typedef’d to multiple names, it makes sense for it to pick the canonical name, but as a user, that’s not what you usually want to see.
So, why bother typdef’ing at all? Why not just name the struct XYZ?
struct DRAWITEMSTRUCT *pdis = (struct DRAWITEMSTRUCT)lParam;
you’re going to say “Why can’t we create a typedef for this structure?” -Raymond]IIRC, Win95 was published at the same time of Visual C++ 2.0 – the first true 32bit version after the hybrid 1.5. 4.0 came shortly after.
According to http://www.emsps.com/oldtools/mscppv.htm, the MSVC 2.0 folders are dated 9/20/94 2:55am – that’s almost a year before Win95 release.
Thanks for the correction on which version of MSVC was released at/around win95 time — I certainly don’t have the install media around to check ;)
It was 32bit, as the guys before me had just ported it over. I was too green to deal with thunks and other 16 bit issues …
Struct and union field names used to live in the same global name-space too, and this is where tradition to prepend a unique prefix to struct fields is from. As a byproduct, fields weren’t bound to any particular struct type:
<pre>
</pre>
(v6root/usr/sys/ken/iget.c:iput())
This is the first time on Raymond’s blog I’ve read a technical post and have known the answer before I read it.
I feel so smart.
Day after tomorrow, though, I will stop knowing the answers and will feel dumb again.
It’s an annoyance, actually, because in C++ you can’t derive a class from the the typedef you’re used to see.
In other words, you have to derive a c++ class from ‘tagPOINT’ instead of from "POINT".
I absolutely hate the ‘tag’ stuff in Windows header, because programmers have copied it into their code in many companies I’ve been at, without knowing why. Just like the "LP" typedefs for pointers, or capital VOID.
The reason why there are typedef at all was that in "C" we had to write "struct POINT pt;" when defining a variable. The typedef saves us from having to type the word ‘struct’. In C++, this isn’t necessary, and would be totally transparent for us if it wasn’t for that ‘tag’ prefix, which forces us to be aware that there is a type and a typedef.
Here’s a gem of an article about running Windows 1.0 under Virtual PC by Charles Petzold, which might make some oldies smile.
http://www.charlespetzold.com/etc/Windows1/index.html
I was looking around for a circa Win2.0 Windows.h file to look at structs there… stupid internet, it’s like the world didn’t exist before 2000.
The Win1.0 SDK came with Microsoft C 3.0, which predates Microsoft VisualC by many years.
Some compilers alow me to do this:
typedef struct _a {
int (*some_func_ptr)(struct _b *);
} AType;
typedef struct _b {
…
} BType;
other compilers require this declaration up front before everything else:
struct _b;
typedef struct _a {
int (*some_func_ptr)(struct _b *);
} AType;
typedef struct _b {
…
} BType;
I have no idea what is "more" legal.
"
Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.
"
Short answer: Anonymous structures don’t benefit from good error/warning/debug information.
Side note:
I remember that Borland C supports something like typedef struct XYZ { … }; as a special syntax for typedef struct XYZ { … } XYZ;
C had one namespace for struct and union tags, but typedefs were in the same namespace as variables. C did not automatically define a typedef name when you defined a struct. It was C++ that introduced the implicit definition of a typedef name.
So in C people would write
typedef struct foo { … } foo;
and then they sould refer to the type without mentioning struct.
But when they tried compiling this in C++, the mention of "struct foo" would define the typedef name ‘foo’, and when the compiler got around to the ‘foo’ after then ‘}’, it would complain that ‘foo’ had already been defined.
So to use anything like this in C++ you had to change the structure tag.
I speak from memory – I was a C user in the 70’s, and a C++ implementer in the early nineties. C++ may hae chenged then, of course. I haven’t been tracking it since then.
I really should have added this here :
http://www.embedded.com/columns/programmingpointers/9900748
instead of here :
http://blogs.msdn.com/oldnewthing/archive/2008/03/27/8338530.aspx#8340834
I think the Windows 1.0 SDK came around the time of MS C 4.0, and the Windows 2.0 SDK came around the time of MS C 5.0. Visual C++ 2.0 was released around the time of NT 3.5 (I wonder how many people believed Win95 was the first version of Windows to run Win32 apps natively). Visual C++ 4.0 was released around the release of Win95. Visual C++ 4.2 was released around the release of NT 4.0 and contained the beta of several SDKs, including the ActiveX SDK. 4.2c was released to patch them up to released versions. Visual C++ 6.0 was released around the release of Win98, but still contained the NT 4.0 headers and libraries.
IgorD:
A scope for a structure/class name implicitly declared in the function declaration argument list is limited to the function declaration. It’s not propagated up to the enclosing scope.
void a(class b *c); // not in the global scope
class b;
void a(class b *c); // same as in the global scope
On the other hand, a scope for a structure/class name implicitly declared in a member declaration is propagated to the enclosing scope.
class d
{
class a *b;
};
Here ‘class a’ belongs to the global scope, as if it were declared before class d declaration.
But if you do like this:
class d
{
class a;
class a *b;
};
Then it’s class d::class a.