From: Robby on
Hello Igor,

>You mean - a global variable whose type happens to be a struct type.

Yes, sorry if I have caused confusion.

>The term "global structure" is meaningless.

So this can't be considered as a *global structure* ???? .... I am just
asking!
==========================main.h
struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
=========================

>This question makes no sense to me, sorry. I don't know what you mean by
>"extern type".

I am sorry too, bad question. :-)

>So, your real question seems to be - is it ever valid to use global
>variables? Yes, sometimes global variables are justifiable - otherwise
>they wouldn't be in the language in the first place.

Okay, so here I declare 2 global variables bywhich the first one is called
ts which is of tag_ts type and the second one is p_ts of type pointer to
tag_ts, and I am okay doing it this way?
==================================main.h
struct tag_ts
{
int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;
}ts, *p_ts;
====================================main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h" // main.h included once only here! never included
elsewhere!
void f1()
{
p_ts->on_activation_state_1 = 12;
}
int main()
{
p_ts = &ts; // created here once and only once!
p_ts->on_activation_state_1 = 11;
f1();
return 0;
}
====================================

>You say this is the code you like and want to use. However, this
>fragment defines two global variables. Which you now say you are "not
>crazy about" (which I interpret as meaning that you don't particularly
>like the idea). How do you reconcile these two contradictory sentiments?

No, no, no, this fragment defines two global variables of type struct tag_ts
where one being a var and the other being a pointer to a struct tag_ts. When
I said "or simply create global variables which I am not crazy about!" I
meant creating these globally like this in the main.h header file and totally
forgetting about the structure:

int on_activation_state_1;
long ts_area_1;
long relevant_ctrl_msg_1;

I never said I was crazy about *two global variables* ! I guess I should of
been more specific.

--
Best regards
Roberto


"Igor Tandetnik" wrote:

> Robby <Robby(a)discussions.microsoft.com> wrote:
> > Hello Igor,
> >
> >> If you put this into a header file, and #include it into more than
> >> one source file, you'll get linker errors.
> >
> > No, that's the thing you see, I willl never include it again
>
> Then why have a separate header file at all? Just put the code directly
> in your C file. Then it's obvious that the variables are only intended
> for use in that one source. Mark them static for good measure.
>
> > But, you would rather see something like this:
>
> How do you know what I would or wouldn't rather see?
>
> > But now I have to pass ts to every function. In reality, the thing is
> > that in my application, when I go from window Procedure to window
> > Procedure and I need this data in the procedures and outside of all
> > procedures, this requires global scope and for this particular
> > information I though we could do an exception and do a global
> > structure... that's all!
>
> You mean - a global variable whose type happens to be a struct type. The
> term "global structure" is meaningless. Further, you made the word
> "typedef" a central part of your exposition - but typedefs have
> absolutely nothing to do with your actual question. This is why, I
> suspect, everybody seems to have difficulty understanding what you are
> trying to say.
>
> So, your real question seems to be - is it ever valid to use global
> variables? Yes, sometimes global variables are justifiable - otherwise
> they wouldn't be in the language in the first place.
>
> > But I would not include the structure more
> > than once if thats what you are afraid of.
>
> Why should I be afraid of anything in your code? You are the one who'll
> have to live with it.
>
> Again - if you don't intend to include it more than once, don't break it
> into a separate include file in the first place.
>
> > Can we make a structure of extern type?
>
> This question makes no sense to me, sorry. I don't know what you mean by
> "extern type".
>
> > So if I have to typedef the structure in the above code samples
>
> Forget about typedefs. Again - they have nothing to do with the
> substance of your question.
>
> > I
> > either have to extern it (which I am not that used to using extern)
> > or simply create global variables which I am not crazy about!
>
> Wait a minute. You keep showing this:
>
> struct tag_ts
> {
> int on_activation_state_1;
> long ts_area_1;
> long relevant_ctrl_msg_1;
> }ts, *p_ts;
>
> You say this is the code you like and want to use. However, this
> fragment defines two global variables. Which you now say you are "not
> crazy about" (which I interpret as meaning that you don't particularly
> like the idea). How do you reconcile these two contradictory sentiments?
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>
From: Igor Tandetnik on
Robby <Robby(a)discussions.microsoft.com> wrote:
>> You mean - a global variable whose type happens to be a struct type.
>
> Yes, sorry if I have caused confusion.
>
>> The term "global structure" is meaningless.
>
> So this can't be considered as a *global structure* ????

Since I don't know the meaning of this term, I can't tell you what can
or cannot be properly described by it.

> .... I am
> just asking!
> ==========================main.h
> struct tag_ts
> {
> int on_activation_state_1;
> long ts_area_1;
> long relevant_ctrl_msg_1;
> }ts, *p_ts;
> =========================

This fragment of code defines a structure named tag_ts, and two
variables: ts of type struct tag_ts, and p_ts of type struct tag_ts*.
These two variables are global variables: more precisely, their names
have external linkage. Type names don't have linkage.

> Okay, so here I declare 2 global variables bywhich the first one is
> called ts which is of tag_ts type and the second one is p_ts of type
> pointer to tag_ts, and I am okay doing it this way?
> ==================================main.h
> struct tag_ts
> {
> int on_activation_state_1;
> long ts_area_1;
> long relevant_ctrl_msg_1;
> }ts, *p_ts;

I would question your decision to put these definitions into a header
file. Otherwise, I don't see anything immediately wrong with this.

> I guess I
> should of been more specific.

Quite.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


From: Robby on
Thanks Igor and the rest of you for being there to answer my questions!

--
Best regards
Roberto


"Igor Tandetnik" wrote:

> Robby <Robby(a)discussions.microsoft.com> wrote:
> >> You mean - a global variable whose type happens to be a struct type.
> >
> > Yes, sorry if I have caused confusion.
> >
> >> The term "global structure" is meaningless.
> >
> > So this can't be considered as a *global structure* ????
>
> Since I don't know the meaning of this term, I can't tell you what can
> or cannot be properly described by it.
>
> > .... I am
> > just asking!
> > ==========================main.h
> > struct tag_ts
> > {
> > int on_activation_state_1;
> > long ts_area_1;
> > long relevant_ctrl_msg_1;
> > }ts, *p_ts;
> > =========================
>
> This fragment of code defines a structure named tag_ts, and two
> variables: ts of type struct tag_ts, and p_ts of type struct tag_ts*.
> These two variables are global variables: more precisely, their names
> have external linkage. Type names don't have linkage.
>
> > Okay, so here I declare 2 global variables bywhich the first one is
> > called ts which is of tag_ts type and the second one is p_ts of type
> > pointer to tag_ts, and I am okay doing it this way?
> > ==================================main.h
> > struct tag_ts
> > {
> > int on_activation_state_1;
> > long ts_area_1;
> > long relevant_ctrl_msg_1;
> > }ts, *p_ts;
>
> I would question your decision to put these definitions into a header
> file. Otherwise, I don't see anything immediately wrong with this.
>
> > I guess I
> > should of been more specific.
>
> Quite.
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>
From: Ulrich Eckhardt on
Robby wrote:
> struct tag_ts
> {
> int on_activation_state_1;
> long ts_area_1;
> long relevant_ctrl_msg_1;
> }ts, *p_ts;
[...]
> p_ts = &ts; // created here once and only once!
> p_ts->on_activation_state_1 = 11;

This is asinine, you could just write this:

ts.on_activation_state_1 = 42;

There is no need to create a global and then create another global pointer
to that global.

> struct tag_ts will only be required once in the whole program! So for this
> special structure, there will never be another creation of struct tag_ts.

Two choices:
1. The struct is also only used in a single translation unit
You can then simply use an anonymous structure, no need for any type names:
struct { ... } foo;

2. The struct is used in multiple translation units
This means you have to split into a declaration and definition, which in
turn requires that the type has a name.
// foo.h
struct foo_type
{ ... };
extern foo_type foo;
// foo.cpp
foo_type foo;


Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
From: Ben Voigt [C++ MVP] on

> 2. The struct is used in multiple translation units
> This means you have to split into a declaration and definition, which in
> turn requires that the type has a name.
> // foo.h
> struct foo_type
> { ... };
> extern foo_type foo;

extern struct foo_type foo; // works in both C and C++

> // foo.cpp
> foo_type foo;

struct foo_type foo; // works in both C and C++

>
>
> Uli
>
> --
> C++ FAQ: http://parashift.com/c++-faq-lite
>
> Sator Laser GmbH
> Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932