How Globals is handle in C.

U

Umesh

How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.
 
M

Michael Mair

Umesh said:
How Globals are handle in C since global variables can be acessed by
any function in that program.

I assume you mean "variable with file scope and external linkage"
by "global variable". State things like that clearly, as the C
standard does not mention a "global" variable property.
Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.

How this is done, belongs to the implementation (your platform/operating
system/compiler/linker combination, for example). C knows neither
heap nor stack.
If you want to know how "global" variables are realized for your
implementation, ask in a newsgroup where at least part of your
implementation is on-topic.


Cheers
Michael
 
N

Nick Keighley

Umesh said:
How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.

as was commented else-post your use of terminology is a bit off. C has
no
"heap". Some implementations use a heap data structure for malloc()ed
data.

So called global data is usually held in a separate memory area. The
major
areas are usually static, global, dynamic and the stack (I bet I missed
one).

In a sense I can't see your problem. Global data is simply data that is

accessable from anywhere. In a flat address space this is easier to
implement than local data. Perhaps I was exposed to FORTRAN too
early in life...


--
Nick Keighley

Programming should never be boring, because anything mundane and
repetitive should be done by the computer.
~Alan Turing
 
K

Keith Thompson

Nick Keighley said:
as was commented else-post your use of terminology is a bit off. C
has no "heap". Some implementations use a heap data structure for
malloc()ed data.

So called global data is usually held in a separate memory area. The
major areas are usually static, global, dynamic and the stack (I bet
I missed one).

In a sense I can't see your problem. Global data is simply data that
is accessable from anywhere. In a flat address space this is easier
to implement than local data. Perhaps I was exposed to FORTRAN too
early in life...

There are three *storage durations* in C: static, automatic, and
allocated. Static refers to both "global" variables and variables
declared within functions with the "static" keyword. Automatic refers
to local variables that aren't static (commonly stored on a stack).
Allocated refers to objects allocated with malloc() (commonly stored
on on a heap).

This is distinct from scope and visibility, which determine where a
symbol is visible.
 
R

raju

hi umesh
do you know how the all variables stored in a.out file

if we see at memory layout of a.out we are having fig like this

| argv,env |
|-------------------------- |
| stack |-----> all auto variable are
stored here
|_______________ |
| |
| heap |----> this is for dyanamic
allocations i.e, malloc,calloc
|---------------------------|
| bss |-------->all uninitialized
data are stored here
|---------------------------|
| data segments |------->all initialized global &
static variable are stored i.e,(int i=5)
|---------------------------| static int
j=2;
| text segments |---->all constants
|---------------------------|
 
R

raju

hi umesh
do you know how the all variables stored in a.out file

if we see at memory layout of a.out we are having fig like this

| argv,env |
|-------------------------- |
| stack |-----> all auto variable are
stored here
|_______________ |
| |
| heap |----> this is for dyanamic
allocations i.e, malloc,calloc
|---------------------------|
| bss |-------->all uninitialized
data are stored here
|---------------------------|
| data segments |------->all initialized global &
static variable are stored i.e,(int i=5)
|---------------------------| static int
j=2;
| text segments |---->all constants
|---------------------------|
 
P

pai

Hi raju ,

if i declare a pointer as

char *temp="Hello World";

Where this temp will be stored is it in the data segment or heap .

Pai....
 
V

Vladimir S. Oka

pai said:
Hi raju ,

Please quote what (and who) you're replying to, or start a new thread
if your questions does not relate to what you're replying to. If you're
struggling with Google, see advice in sig.
if i declare a pointer as

char *temp="Hello World";

Where this temp will be stored is it in the data segment or heap .

Your question, as well as the post you're referring to is off topic
here. C knows nothing about heaps, data segments, stacks, etc (as has
been pointed elsewhere).

<OT>
In the case you quote, an implementation is allowed to store your
string /constant/ anywhere. It being a /constant/, an embedded
implementation may even decide to put it into ROMable code, as writing
to it invokes undefined behaviour. For discussion see:

http://groups.google.com/group/comp...ring+constant+storage&rnum=3#dfd0cfa34c4c7f07

If the line above gets wrapped by Google, you'll have to stich it back
together maually. As a guide, use the fact that there should be no
spaces in it.
</OT>

Cheers

Vladimir
 
S

sant

"Hello World" string will be stored in the Initialized data segment. In
that it is stored in Read Only Memory.

temp will store the starting address of this Read Only string. I think
temp is stored in stack if you have not declared it as global.
 
S

santosh

Global variables are stored in Initialized data segment.


Following is the C memory layout.

Stack
 
V

Vladimir S. Oka

Vladimir said:
Please quote what (and who) you're replying to, or start a new thread
if your questions does not relate to what you're replying to. If you're
struggling with Google, see advice in sig.

Oh, and `temp` itself will have to be stored somewhere writeable, as it
holds a nonconstant /pointer/ to the constant string "Hello World".
Again, C doesn't care about where /physically/...
 
U

Umesh

raju said:
hi umesh
do you know how the all variables stored in a.out file

if we see at memory layout of a.out we are having fig like this

| argv,env |
|-------------------------- |
| stack |-----> all auto variable are
stored here
|_______________ |
| |
| heap |----> this is for dyanamic
allocations i.e, malloc,calloc
|---------------------------|
| bss |-------->all uninitialized
data are stored here
|---------------------------|
| data segments |------->all initialized global &
static variable are stored i.e,(int i=5)
|---------------------------| static int
j=2;
| text segments |---->all constants
|---------------------------|


No Raj.

Let you explain clearly .
Is that globals variable will be local to main fuction or how their
differ from local variable.
 
R

REH

santosh said:
Global variables are stored in Initialized data segment.


Following is the C memory layout.

Stack

"The C Memory Layout." I must have missed that section in the
standard.

REH
 
J

Jordan Abel

hi umesh
do you know how the all variables stored in a.out file

if we see at memory layout of a.out we are having fig like this

First of all - if you're going to make an ascii diagram, use a
fixed-width font.
| argv,env |
|---------------|
| stack |---> all auto variable are stored here
|_______________|
| |
| heap |--> this is for dyanamic allocations i.e, malloc,calloc
|---------------|
| bss |---->all uninitialized data are stored here
|---------------|
| data segments |---->all initialized global & static variable are stored
|---------------| i.e, (int i=5) static int j=2;
| text segments |-->all constants
|---------------|

"constants" are as likely to go in the rodata sectio as text. "stack"
and "heap" do not necessarily grow towards each other, argv/env are
certainly not stored in a.out and neither are stack and heap themselves.
in the a.out file itself, the bss "segment" doesn't actually store
anything [since it's all zero bytes]. You seem to be talking about the
layout of the process memory, not of the a.out file.
 
K

Keith Thompson

sant said:
"Hello World" string will be stored in the Initialized data segment. In
that it is stored in Read Only Memory.

temp will store the starting address of this Read Only string. I think
temp is stored in stack if you have not declared it as global.

As we've said here hundreds or thousands of times, *please* provide
some context when you post a followup. Not everyone can easily see
the article to which you're replying. group.google.com makes this
gratuitously difficult, but there is a workaround. See
<http://cfaj.freeshell.org/google/> for details.

The C language says nothing about an "initialized data segment" or
"read only memory". It refers to three distinct "storage durations":
static, automatic, and allocated.

A string literal may or may not be stored in some kind of read-only
memory. A portable program can't tell the difference, since any
attempt to modify a string literal invokes undefined behavior.

Again, *please* read <http://cfaj.freeshell.org/google/>.
 
K

Keith Thompson

santosh said:
Global variables are stored in Initialized data segment.


Following is the C memory layout.

Stack

Some implementations may use that layout (and if so, they'll probably
use the same layout for programs written in any compiled language).
Others won't. The specific memory layout has nothing to do with the C
language.
 
J

Joe Wright

Umesh said:
How Globals are handle in C since global variables can be acessed by
any function in that program.

Is it they stored in heap so they can be acessed across fuction.OR how
they handle in c.
It seems you are asking how they are accessed rather than how they are
stored. The C langage doesn't mention 'global' in any context. I assume
you mean a variable in one file which can be accessed by statements and
functions in another file. If not, please ignore the rest of this.

Assume I have a program consisting to two .c translation units and a
single .h header, foo.c, bar.c and foo.h.

The header will look like this..

extern int x;

...simply declaring that there is an int called x out there somewhere.

There is bar.c which looks like..

#include "foo.h"
void bar(void) {
x = 2;
}

The main TU, foo.c, looks like this..

#include <stdio.h>
#include "foo.h"
int x = 1;
int main(void) {
printf("%d\n", x);
bar()
printf("%d\n", x);
return 0;
}

In foo.c the variable 'int x' is declared and defined outside any
function and therefore has 'external' linkage. This means the linker
will be able to 'see' its name and address at link time. foo.h provides
the declaration of x as 'int x' but does not provide an address.
Including foo.h in foo.c is not useful in this case, but harmless. There
may be other declarations in foo.h that foo.c needs.

I use gcc so I'll demonstrate that. Compile the two .c TU's to object
files..

gcc -c foo.c

...creates foo.o and..

gcc -c bar.c

...creates bar.o. Then..

gcc foo.o bar.o -o foo.exe

...will create the executable. Because x has external linkage, foo.o will
provide the linker with its address. Then as bar.o is looking for x, the
linker will assign its address in bar.o such that the assignment in
bar() is treating the variable defined in foo.c.

I didn't know this would take so long. :)
 
C

CBFalconer

Keith said:
As we've said here hundreds or thousands of times, *please*
provide some context when you post a followup. Not everyone can
easily see the article to which you're replying.
group.google.com makes this gratuitously difficult, but there is
a workaround. See <http://cfaj.freeshell.org/google/> for details.

Sigh. The work of civilizing the googolian barbarian hordes is
never done. After we get past the first problem we have to worry
about topposting, and after that about snipping. Of course
topicality comes in also. Once in a while we run into one that
catches on immediately, which makes it worthwhile.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
 

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,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top