dereferencing a char* always gives segmentation fault

-2

I am trying to go over the memory addresses and see different accessibility of the contiguous memory areas, which are: read-only, read&write and no accessibility. I also have a signal handler that takes care of segmentation fault so the program can continue running.


char* currentAdd;
char readBuf;

for (unsigned int i = 0; i < 0xffffffff, i+=PAGE_SIZE){
    currentAdd = (char*)i;
    jmpVar = sigsetjmp(env,1); // don't worry about this, it's handled properly

    if(jmpVar == 0){
    currentMode = MEM_RO;
    readBuf = *currentAdd; // always a segmentation fault
    
    // it can never try writing!!
    currentMode = MEM_RW;
    *currentAdd = readBuf;
    }
    else{
    ...
    }

}

my program never goes past (readBuf = *currentAdd;), which is sometimes expected but not always. Did I do something wrong?

I tried to start from i=1, to prevent what some people suggested but still it keeps giving me the segmentation fault.

c
pointers
operating-system
asked on Stack Overflow Feb 5, 2021 by saji • edited Feb 5, 2021 by saji

1 Answer

0

well you start your loop at i = 0, and then give currentAdd the address of a char at the address i, so currentAdd is basically (void*)0, aka NULL, so you are deferencing NULL, and no computer really like that.

answered on Stack Overflow Feb 5, 2021 by Bamontan

User contributions licensed under CC BY-SA 3.0