• C

code segment

Can anybody tell me what the code segment? Please give me some example.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:

Hi John,

When you compile and link a program, some parts can automatically be "grouped" together by the compilation and linkage process.  Typically, the separated items are placed in a memory "segment" to isolate code (the executable portion of your program) from data.

The code segment is the executable code that is produced by the compiler.

Back when PCs were smaller, slower, and simpler, they only had 640K of memory and the term "memory segment" had a somewhat different meaning than it does today.

But the principal still holds true.

john2789Author Commented:
Still, I don't get it. Could you explain it more detail?
For example, if you declare some global variables they would go into one separate section of memory other than that which is used to store the code. Some constants might be stored separately from these segments as well (sometimes following the code before getting moved to ROM etc.). The individual stacks could also have their own section. And so on. As Kent said, these things happens during the linking process of your program. You would be able to understand it better if you've seen some assembly code.
Need More Insight Into What’s Killing Your Network

Flow data analysis from SolarWinds NetFlow Traffic Analyzer (NTA), along with Network Performance Monitor (NPM), can give you deeper visibility into your network’s traffic.

john2789Author Commented:
could you show me what  the code segment looks like on some programming?
Kent OlsenDBACommented:

If you compile and link a short program so that you write a loadmap this is pretty easy to see.  Each of the segments will be labelled on the loadmap file.

There are many reasons for separating different items by their functionality.  There are also some good reasons for NOT separating these items, but for the most part the reasons for separating them win out.

Consider the following program:

#include <stdlib.h>

int GlobalVariable = 0;

main ()
  printf ("The value of the global variable is %d\n", GlobalVariable);

Pretty simple, huh?  Well the compiler and linker can team up to separate items of similar usage.  In this case we have code (the printf() statement will be executed), global data (the integer GlobalVariable), and a constant (the string "The value of the global varibles is %d\n").

On such a small program, separating these items into their own region (or segment) is a bit wasteful, but on a very large program you would probably want all of your variables organized in one place and all of your code in another place.  One of the things that this does is make the amount of memory required for the executable code smaller since variables aren't mixed in with it.  This can help the program run faster by not voiding the cache as often.

Constants don't usually get their own "segment", but they are usually grouped together in a region that the compiler treats as "read only" memory.  This way the contents are never changed and never require page faulting back to disk should the page where they reside ever need to be "swapped out".

john2789Author Commented:
so, code segment is

printf(  );? If we have 3 printf ( ) statements, are they also code segments?
Kent OlsenDBACommented:

All three will end up in the code segment (there's only one code segment per program).  Along with all of the if, while, for, switch, and return statements.  Plus all of the assignment statements.  They all reduce to executable code (instructions) so they all get placed in the code segment.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The only place you can "see" the code or data segments is in the map file created by the linker.  Compile your program with the option to generate a map file.   Study the map file.  You'll see the names of the functions are assigned addresses encompassed by the code segment, often named "_TEXT".  The names of your variables wil end up in a data segment, often named "_DATA" for preset variables, or or "_BSS" for unset or value=0  variables.

When it comes time for your program to run, the operating system will create an area of memory for the code and labels it as "Executable".  It will also creates a right size segment for data and labels it as "read/write" memory.  Then the OS will jump to the starting address in the code segment.  That's about it for segments.

On the older real-mode PC's, segments had other meaning as well, there segments were a half-arsed way of accessing more than 64K of memory.

All below is about 16-bit mode.

Segment - is a part of memory that can be accessed without change of segment register value.
Because of registers are 16 bit we can address 64k of memory.
"Code Segment" - is a segment that contain code of a program
"Data Segment" - is a segment that contain data of a program

But it rather conditionally, because of code segment can contain data and on the contrary.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.