0

I was trying to understand structure padding , so i wrote a simple program as written below and i executed it . just to make clear i made two copy of this program program1.c and program2.c and executed both together to see the member address but something strange came up Both program giving me same result , as like having exactly same addresses I want to know what exactly happening internally ?

Program1.c

    #include<stdio.h>
struct pad{
short int a[5];
int b;
char c;
} y;

int main()
{
    int size = sizeof(y); 
    printf (" size %d byte",size);

    y.b = 10;
    printf ("\n address of member 1 is  %d \n",&y.a);
    printf (" address of member 2 is  %d \n",&y.b);
    printf (" address of member 3 is  %d \n",&y.c);    


    getch ();    
}

Output of the program

size 20 byte
 address of member 1 is  4210784
 address of member 2 is  4210796
 address of member 3 is  4210800

This is program2.c

include<stdio.h>

struct pad{

short int a[5];
int b;
char c;
} x;

int main()
{
    int size = sizeof(x); 
    printf (" size %d byte",size);


    printf ("\n address of mem 1 is  %d \n",&x.a);
    printf (" address of mem 2 is  %d \n",&x.b);
    printf (" address of mem 3 is  %d \n",&x.c);
//    printf (" size %d byte",size);



    getch ();    
}

Output of second program

size 20 byte
 address of member 1 is  4210784
 address of member 2 is  4210796
 address of member 3 is  4210800

I am using windows 7 32 bit architecture , and Dev-c++

user143252
  • 31
  • 8
  • 1
    Further information: https://en.wikipedia.org/wiki/Address_space, http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/. http://programmers.stackexchange.com/a/325715/63202 – Erik Eidt Aug 30 '16 at 05:15

1 Answers1

8

This is the result of virtual memory. Roughly, virtual memory provides each running program with the illusion that all of the system's memory is at its disposal. It presents each program with an abstract virtual address space, and the operating system and the CPU map addresses in the abstract virtual memory space to locations in physical memory. The physical memory may be RAM or it may be slow storage like blocks on a disk drive. One side effect of this is that multiple copies of the same running program all seem to have the same addresses for their variables. You may want to read What Every Programmer Should Know About Memory for more detailed information.

Charles E. Grant
  • 16,612
  • 1
  • 46
  • 73
  • all address is unique . two program sharing same address ,means they overlap .. thanks for help , i am not satisfy with your answer , please elaborate – user143252 Aug 29 '16 at 23:36
  • 7
    No, you need to read about virtual memory. Two program can use the exact same addresses in virtual memory, but the OS and the CPU will map them to distinct locations in physical memory. The process by which virtual memory is mapped to physical memory is too complex to adequately describe in a couple of paragraphs, which is why I provided the reference. – Charles E. Grant Aug 29 '16 at 23:39
  • @user143252: No, they don't overlap. You might be in the living room at house number 36, and someone else is in the living room at house number 36, and you can't see them - because they live in a different street or town! The operating system tells you "as far as you are concerned, your struct is at address x" and could have said "and don't worry about what I tell others where their structs are; that's none of your business" and really didn't want to say "You think this doesn't work? Do you think your operating system is bloody stupid? I SAID don't worry about others. " – gnasher729 Aug 30 '16 at 13:12