Write a program to find the page size and offset for a given address (in decimal ) and for a given the page size in a 32-bit system


  • 0
    J

    A straight forward solution to this problem is :-

    Page Number = quotient of address / page size
    offset = remainder of address / page size

    A faster solution would involve bit manipulation.

    **Pure Storage interview.


  • 1
    S

    Here is an example:

    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/mman.h>
    #include <stdio.h>
    
    int main(void)
    {
        size_t  page;
        int a = 500;
        void *b = &a;
    
        page = (size_t)sysconf(_SC_PAGESIZE);
        printf("Page size %zu\n", page);
        printf("Address of a is %p\n",  b);
        printf("Page number of a in int is %lld, and in hex is %p\n", (long long)b/(int) page, (void *)((long long)b/(int)page));
        printf("Offset in int %lld, and hex is %p\n", (long long)b%(int)page, (void *)((long long)b%(int)page));
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.