Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmentation of memory in the C language

Tags:

c

memory

I am reading some text about the C language at the url https://cs.senecac.on.ca/~btp100/pages/content/compu.html. In the section "Segmentation", it says: "One logical technique for managing the addressing of a large number of bytes is segmentation. Segmentation distinguishes certain regions of memory from other regions. For example, an operating system stores program information in dedicated segments. " enter image description here

I do not quite get it.

For example, if I have the following program:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int x = 4;
    int y = 5;
    printf("%d\n", x+y);
  system("PAUSE");  
  return 0;
}

So, what is stored in Segment Code, what in Segment Data, and what in Stack? Please.

Thanks a lot

like image 348
ipkiss Avatar asked Mar 03 '11 01:03

ipkiss


2 Answers

The stack is your local variables (such as x and y). The code segment is for the binary code that is actually executed. Finally, the data segment is for values that your program uses (such as the PAUSE string there).

like image 84
Andrew White Avatar answered Sep 29 '22 00:09

Andrew White


This is compiler dependent. but in general, and assuming you have an OS that actually uses segmenation, your local variables x and y are referenced to SS, your string literals "%d\n" and "PAUSE" are referenced to DS and your actual assembled code is reference to CS.

like image 28
ThomasMcLeod Avatar answered Sep 29 '22 00:09

ThomasMcLeod