• RSS
  • Facebook
  • Twitter

Knowledge is Power.

  • Who you are ?

    Working on machines without understanding them ? Then you should be here..

  • Where you are ?

    Geographical location should not become a barrier to Share our knowledge.

  • What do you do ?

    Puzzles and Interview question are intended to be discussed here.

    Sunday, March 28, 2010

    In file1.c
    we have static int i = 5;
    and int *ptr = &i;

    now value of ptr is written in some file say abc.txt

    now file2.c (another program) opens abc.txt
    read the value

    and print the value loacted at dat adress say
    what will be the o/p????


    example i = 5;
    ptr = &i; ptr has now 0xABFF

    0xABFF is written in abc.txt

    file2.c opens the abc.txt and read the value 0xABFF

    now print the value present at this address
    waht it will print????



    ANSWER:

    A program has nothing to do with RAM but an address space available to it. That is, total amount of memory it can index (on 32 bit systems 2^32). Program is written at the lower level to hold addresses (any of these 2^32 minus some reserved for kernel) within this range irrespective of the amount of RAM you have. Program assume it has and can have whole addresses only for itself. OS and CPU help to map this wide range of addresses to the amount of available RAM in your computer. Thus, the address your pointer holds is an address in this address space (called virtual address).

    Even if the same program runs twice your pointer may not be the same (with same value) next time due to various environmental (and your program logic) reasons.

    There are programs that store addresses of objects in virtual memory and the links between these objects to a file. But prior to writing they change these addresses to file offsets (thinking now this file to be memory.) Converting memory pointers (addresses) to file offsets and fro is termed 'pointer swizzling.' These programs are in general called 'object stores', and object-oriented databases are on genre of object stores.

    The variable being static makes no difference, the code produced by your program may be relinkable and reloadable (relocatable code.)

    0 comments:

    Post a Comment