Need help in Global variable in c


Ad

Advertisements

M

Mike Wahler

Hi,

I read this article about global variable in c:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html

But I have a few questions
1. how can I declare the global variable so that it can be set/get by
other files?

/* glob.c */
int global = 99; /* defines 'global' */

/* glob.h */
extern int global; /* declares that 'global' */
/* exists elsewhere */


/* file1.c */
#include <stdio.h>
#include "glob.h"

int main()
{
global = 42;
printf("%d\n", global);
return 0;
}

In the interest of error prevention and maintainability,
you should avoid globals if possible.
2. If I have multiple instance of the same program running in the same
machine (linux), will each program instance has its own of global
variable?

That depends entirely upon your platform (and possibly compiler
settings). The C language does not specify program behavior in
a multi-process environment.

-Mike
 
R

Richard

Mike Wahler said:
/* glob.c */
int global = 99; /* defines 'global' */

/* glob.h */
extern int global; /* declares that 'global' */
/* exists elsewhere */


/* file1.c */
#include <stdio.h>
#include "glob.h"

int main()
{
global = 42;
printf("%d\n", global);
return 0;
}

In the interest of error prevention and maintainability,
you should avoid globals if possible.


That depends entirely upon your platform (and possibly compiler
settings). The C language does not specify program behavior in
a multi-process environment.

There is a platform where different instances share the same
"globals" as demonstrated above?

To the OP : Multiple instances all have their own data unless some
platform specific sharing mechanism is used. The code above is process
private in the real world.

I would be seriously interested to hear of cases where is not the case
as I have never seen it before.
 
J

jacob navia

Hi,

I read this article about global variable in c:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/glo_int_vars.html

But I have a few questions
1. how can I declare the global variable so that it can be set/get by
other files?

1) You declare the variable in one file. By default variables are
visible in other files
File f1.c

int n;

In other files you just use them declaring them extern:

File f2.c

extern int n;

....
n = 67;

2. If I have multiple instance of the same program running in the same
machine (linux), will each program instance has its own of global
variable?

If you have different processes, they all will have different
versions of the variable since they run in different address spaces.

If you have several THREADS, that's another question. In each thread the
variable will be seen by all threads and can be modified by all
threads.

Note that when you fork(), the child process is a *process* i.e.
the address space is differeent, and the variable will receive the value
it had before the fork() but afterwards both process will be decoupled
and each one will NOT see the modifications of the other.
 
J

jacob navia

Richard said:
There is a platform where different instances share the same
"globals" as demonstrated above?

Windows thread model.

Each thread under windows (I think is the same with pthreads)
sees all global variables from all threads.
To the OP : Multiple instances all have their own data unless some
platform specific sharing mechanism is used. The code above is process
private in the real world.

I would be seriously interested to hear of cases where is not the case
as I have never seen it before.

Other primitive platforms where there is no OS, can have a global
address space shared by all proceses.

MSDOS TSR's were one example, Macintosh applets were another,
and even if today those "OS"es are rare, they may still
exist somewhere.
 
R

Richard

jacob navia said:
Windows thread model.

Each thread under windows (I think is the same with pthreads)
sees all global variables from all threads.

That is not the same as multiple instances. Not by a long shot. Or?
Other primitive platforms where there is no OS, can have a global
address space shared by all proceses.

MSDOS TSR's were one example, Macintosh applets were another,
and even if today those "OS"es are rare, they may still
exist somewhere.

Hmm. Yes. Well. Possibly I was wrong to thing we were talking multiple
instances on a modern multi tasking OS where the chance of different
*instances* of user applications sharing global datam as defined
earlier, is highly unlikely.
 
Ad

Advertisements

M

Mike Wahler

Richard said:
There is a platform where different instances share the same
"globals" as demonstrated above?

What is 'demonstrated' above has nothing to do with 'instances'
of a program. C does not address any such concept.
How platform memory models differ is beyond the scope of comp.lang.c.
To the OP : Multiple instances all have their own data unless some
platform specific sharing mechanism is used. The code above is process
private in the real world.

To the OP: Please note that the topic here is the C language itself,
which does not include OS capabilities and behavior. So you have no
assurance that answers about such things posted here are correct.

You'll have a far better chance of getting accurate information about
a particular platform by asking in a group dedicated to that platform.
I would be seriously interested to hear of cases where is not the case
as I have never seen it before.

Please don't ask for nontopical information here. Thank you.

-Mike
 
R

Richard

Mike Wahler said:
What is 'demonstrated' above has nothing to do with 'instances'
of a program. C does not address any such concept.
How platform memory models differ is beyond the scope of comp.lang.c.

Oh please. For one minute try to apply some common sense. There is a
damn good chance the poster is a new C programmer who wants to know
about how programs share memory. The code above is not it on
99.99999999999999% of platforms known to man.
To the OP: Please note that the topic here is the C language itself,
which does not include OS capabilities and behavior. So you have no
assurance that answers about such things posted here are correct.

No, in the same way you have no assurances that that aircraft is not
going to crash. But statistics are .....
You'll have a far better chance of getting accurate information about
a particular platform by asking in a group dedicated to that platform.

before or after he understands what a "global variable" is in C?
Please don't ask for nontopical information here. Thank you.

He didn't even know what a global was or how to restrict access in a
normal program. Cut some slack and apply some common sense.
 
Ad

Advertisements

K

Kenny McCormack

Richard said:
Oh please. For one minute try to apply some common sense. There is a

Common sense is OT in clc. Everybody knows that.

I thought you of all people would be clear on that.
 

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

Top