Connecting Tech Pros Worldwide Forums | Help | Site Map

How the Memory will be Allocated for the C programs ?

Newbie
 
Join Date: Apr 2007
Location: Chennai, INDIA
Posts: 25
#1: Apr 20 '07
How the Memory will be Allocated for the C programs ....,
C static & dynamic variables ? Who will allocate memory for these...?
physically where it will be ...?


Regards
Balaji

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: Apr 20 '07

re: How the Memory will be Allocated for the C programs ?


Depends on the platform.
Newbie
 
Join Date: Apr 2007
Location: Chennai, INDIA
Posts: 25
#3: Apr 20 '07

re: How the Memory will be Allocated for the C programs ?


can u explain me for any of one particular platform ?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Apr 20 '07

re: How the Memory will be Allocated for the C programs ?


Quote:

Originally Posted by ngpbalaji

can u explain me for any of one particular platform ?

This is for the Un*x platforms; it's a bit of a simplification but here goes:
when your linker links one or more compiled translation units (.o or .obj files)
it divides it all up in a bunch of segments:

1) the 'text' segment(s) where the actual machine code is stored; the loader
relocates all relative addresses to real memory addresses.

2) the 'data' segment; this segment has all the initialized global data in it; again
the loader takes care that every pointer or whatever points to the initialized stuff
points to the correct location in memory.

3) the 'rodata' segment (optionally): this segment contains all data that is supposed
to be 'ro' (read only).

4) the 'bss' segment (block started by symbol); this segment contains all
uninitialized data and logically speaking this segment contains all 0s (zeros).
This segment can be large and is usually not present in the linked file, i.e.
just a block of zeros in memory will be created by the loader.

5) the 'stack' segment; optionally the linked file specifies the size of the stack.
This was very important in the old days where memory was not present by the
giga bytes. Nowadays a stack can grow without limitation.

6) the 'reloc' (relocation) segments; these segments tell the loader which addresses
to adjust from relative addresses to real memory addresses. If the executable
for is dynamically linkable (.so or .dll file) these segments also contain the relative
addresses of the objects (data and code) that have exported linkage, (can be
called as a .so or .dll object.

7) the 'heap'; this segment is just like the stack not physically present in the
executable file. Just like the stack segment it can optionally specify the size
of the heap in memory.

All these segments (and a bit more) make up an executable file. The format of
those files is standardized as either a coff (common object file format) file or
an elf (extensible link format) file. I think (not sure though) that Microsoft uses
the coff format or their proprietary variation thereof.

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Location: Chennai, INDIA
Posts: 25
#5: Apr 20 '07

re: How the Memory will be Allocated for the C programs ?


Thanks for kind explanation Jos...

In Linux we can create .so files ,
like that, in windows Can we create .dll files by our C programs?

I think Application files creates one or more dll files at run time...
Then can we use .dll files inside functions in some other program/application?
like in Linux we can open a .so file dynamically by dlopen,and access by dlsym,...
Is it possible to open and access dll files in windows?

And one more doubt..
Windows & Linux working in Protected mode..
DOS working in real mode ... Memories respectively ...
is it correct?

Thanks...
Balaji
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Apr 20 '07

re: How the Memory will be Allocated for the C programs ?


Quote:

Originally Posted by ngpbalaji

Thanks for kind explanation Jos...

In Linux we can create .so files ,
like that, in windows Can we create .dll files by our C programs?

Yep, it's basically all the job of the linker: your give it your .o or .obj files and
tell the linker what to build from them: either a dynamic link library (shared object)
or an executable file.

Quote:
I think Application files creates one or more dll files at run time...
Then can we use .dll files inside functions in some other program/application?
like in Linux we can open a .so file dynamically by dlopen,and access by dlsym,...
Is it possible to open and access dll files in windows?
Dynamic link libraries are generated by the linker before any execution starts.
Those files can be use by more than one process, that's why they're "dynamically
linked in" with the processes.The OS keeps a list of currently loaded dlls (so files)
and when one process has dynamically linked it in it doesn't need to be loaded
in memory again when another process also wants to link it in.

Quote:
And one more doubt..
Windows & Linux working in Protected mode..
DOS working in real mode ... Memories respectively ...
is it correct?
That's a bit of Intel jargon but yes, it is correct. Most, if not all, nowadays
processors can run in at least two different modes. One mode for the OS and
the other mode for the user processes. Use processes can not directly access
all the memory nor all the devices they want, i.e. they need to do a system call
for that and the OS decides whether or not such an access is allowed.

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Location: Chennai, INDIA
Posts: 25
#7: Apr 21 '07

re: How the Memory will be Allocated for the C programs ?


Thanks Jos..

Is there any utility to find the Memory leak / corruption / Unfree locations in our C program in Linux..?


Thanks..

-Balaji
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: Apr 21 '07

re: How the Memory will be Allocated for the C programs ?


Quote:

Originally Posted by ngpbalaji

Is there any utility to find the Memory leak / corruption / Unfree locations in our C program in Linux..?

I used to use "purify" for SunOS/Solaris a lot; I think it's available for Linux too.
Google is your friend ;-)

kind regards,

Jos
Newbie
 
Join Date: Apr 2007
Location: Chennai, INDIA
Posts: 25
#9: Apr 21 '07

re: How the Memory will be Allocated for the C programs ?


Thanks for the kind answers..
-Balaji
Reply