From: Robby on
"Barry Schwarz" wrote:

> But if stack space is the problem, why is he using long instead of
> short or int.

Because I am porting from an 8bit MCU to a 32 bit MCU and in the 8 bit MCU a
long is only 16 bits wide. In summary a long in an 8 bit MCU = a short in a
32 bit MCU !

Eventually I will change this:

long MQ[10]; >>>>>>>>> to this: int MQ[10];

But before I start changing var types and create a grave for myself here, I
have to take baby steps and do one thing at a time. So first I bring over the
program exactly the way it is and see if it even compiles... because remember
I am porting a program from a non C compliant compiler to a C compliant
compiler. I have a feeling I will see many things like this global variable
issue come up.

Rob
From: Robby on
"Leslie Milburn" wrote:

> My preferred approach is to declare the variable at the top of the .c module
> in which it is first created and usually (but not always) populated. Then
> for each .c module that requires access to the variable I place "extern
> variable name" at the top of that module with a comment indicating why I am
> using it. To make the code more readable I use the naming convention of
> modulename_variablename to help identify where the declaration is located.
> So in your case it would be api_MQ.

So leslie, please confirm to me that what you are suggesting is this:
=====================KERNAL.h
//...

=====================KERNAL.c
#include "KERNAL.h"
long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <at top of a particular .c file!

=====================API.c
#include "API.h" // API.h not showm !!!
extern long MQ[10]; // Global Message queue variable

void API_InsertMessage(enum enumKM m)
{ MQ[0] = m; }
===========================

I have not tried the above code, but from the paragraph you wrote it seems
to be what you are recommending! and its okay with me.

However, my original sample would work aswell... to be noted here, MQ is my
only global variable I need in my whole project. The rest of the application
declares structures and preprocessor commands in header files.

Thankyou all for your replies. Sorry for the delay!

regards
Robert
From: Leslie Milburn on

"Robby" <Robby(a)discussions.microsoft.com> wrote in message
news:D7E4F275-613E-46DF-9736-09DFB6DB4BD2(a)microsoft.com...
> "Leslie Milburn" wrote:
>
> So leslie, please confirm to me that what you are suggesting is this:
> =====================KERNAL.h
> //...
>
> =====================KERNAL.c
> #include "KERNAL.h"
> long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <at top of a particular .c file!

Yes.

> =====================API.c
> #include "API.h" // API.h not showm !!!
> extern long MQ[10]; // Global Message queue variable
>

Yes

>
> However, my original sample would work aswell... to be noted here, MQ is
> my
> only global variable I need in my whole project. The rest of the
> application
> declares structures and preprocessor commands in header files.

Yes, what you wrote will work but you are placing what I consider to be code
in header files, not something I like.
Leslie.




From: Ron Francis on
"Robby" <Robby(a)discussions.microsoft.com> wrote in message
news:D7E4F275-613E-46DF-9736-09DFB6DB4BD2(a)microsoft.com...
> "Leslie Milburn" wrote:
>
>> My preferred approach is to declare the variable at the top of the .c module
>> in which it is first created and usually (but not always) populated. Then
>> for each .c module that requires access to the variable I place "extern
>> variable name" at the top of that module with a comment indicating why I am
>> using it. To make the code more readable I use the naming convention of
>> modulename_variablename to help identify where the declaration is located.
>> So in your case it would be api_MQ.
>
> So leslie, please confirm to me that what you are suggesting is this:
> =====================KERNAL.h
> //...
>
> =====================KERNAL.c
> #include "KERNAL.h"
> long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <at top of a particular .c file!
>
> =====================API.c
> #include "API.h" // API.h not showm !!!
> extern long MQ[10]; // Global Message queue variable
>
> void API_InsertMessage(enum enumKM m)
> { MQ[0] = m; }
> ===========================
>
> I have not tried the above code, but from the paragraph you wrote it seems
> to be what you are recommending! and its okay with me.
>
> However, my original sample would work aswell... to be noted here, MQ is my
> only global variable I need in my whole project. The rest of the application
> declares structures and preprocessor commands in header files.
>
> Thankyou all for your replies. Sorry for the delay!
>
> regards
> Robert

I tend to do the same as Leslie.
Probably not applicable in your case if you only have one global variable, but what I have done is
create a header file with a list of externals that gets included in any files that need them
=====================external.h
extern long MQ[10];
//add any externals that you need

In this way, if you need to create any more variables that you need to access from other files, you
can just add it to the one header rather than to each file.
It may be less obvious, but it can save some legwork if there are lots of files and you need to
change something.

Ron.

From: Robby on
Hello Leslie,

"Leslie Milburn" wrote:

>
> "Robby" <Robby(a)discussions.microsoft.com> wrote in message
> news:D7E4F275-613E-46DF-9736-09DFB6DB4BD2(a)microsoft.com...
> > "Leslie Milburn" wrote:
> >
> > So leslie, please confirm to me that what you are suggesting is this:
> > =====================KERNAL.h
> > //...
> >
> > =====================KERNAL.c
> > #include "KERNAL.h"
> > long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <at top of a particular .c file!
>
> Yes.
>
> > =====================API.c
> > #include "API.h" // API.h not showm !!!
> > extern long MQ[10]; // Global Message queue variable
> >
>
> Yes
>
> >
> > However, my original sample would work aswell... to be noted here, MQ is
> > my
> > only global variable I need in my whole project. The rest of the
> > application
> > declares structures and preprocessor commands in header files.
>
> Yes, what you wrote will work but you are placing what I consider to be code
> in header files, not something I like.
> Leslie.

In all due respect Leslie, I don't see where I am placing code in header
files.

=====================KERNAL.h
//...
=====================KERNAL.c
#include "KERNAL.h"
long MQ[10] = {1,2,3,4,5,6,7,8,9,0};
=====================API.c
#include "API.h" // API.h not showm !!!
extern long MQ[10];
=========================

???? confused ???

Rob