No memory used in address space

Is unused memory in the process address space protected by simply having read permission, so writing to the location indicated by a single pointer, for example, always causes the OS to detect a page error? Or is this not the case, and each location of the memory, except for the code (which, of course, is granted read-only access), is granted write access?

I ask about this because my friend showed me his code where he did not initialize the pointer and did not write in the memory indicated in it, but still his program did not crash with the mingw gcc compiler for windows, but always crash with visual C + +, on mac or linux.

I think that the OS does not protect memory for unused areas, and failures are caused by the fact that in the code generated by the stream, the value of the random pointer pointed to some used area, such as a stack, heap or code, while in others cases, he pointed to some free zone. But if the OS really does not protect unused areas, could such errors, such as uninitialized pointers, be debugged?

I assume that it is therefore recommended that you always assign NULL to the pointer after the call, deleteor free, therefore, when something is opened with it, it really causes a visible failure.

+5
source share
5 answers

. , , ExecShield , , , ( NULL-), , .

+2

. , . , , , .

+3

, / ( , ) . , , , , , . "" - , 4 .

, , , - . , , ( ) , , , , , "". () , () , .

+3

c/++. , , . ptr , , , .

"Hello", gcc:   #include

char buffer[10];

void function1(void) {
    char * ptr = buffer;
    sprintf(buffer, "Hello");
    return;
}

void function2(void) {
    char * ptr2;
    printf("%s\n", ptr2);
}

int main(int argc, char * argv[]) {
    function1();
    function2();
}

( , Visual Studio ) , ptr2, .

C , .

+2

Simply, I assume that the answer is "No, unused memory in the address is not protected by space." C is not complicated enough to handle such instances.

+1
source

All Articles