For this question, just heard before, and did not study. When you recently programmed in OJ, there was no problem running on VS, and a segment error occurred during the commit. Pay attention to this problem for this.
Give a definition of the noun on a wiki
A segmentation fault occurs when a program attempts to access a memory location that it's not allowed to access, or Attem PTS to access a memory location in a A-allowed (for example, attempting to write to a read-onlylocation, or To overwrite part of the operating system).
The so-called segment error means that the memory accessed exceeds the memory space given to the program by the system, usually this value is saved by GDTR, which is a 48-bit register, where 32 bits are saved by the GDT that it points to, and the last 13 bits are stored accordingly to the GDT, The last 3 bits include whether the program is in memory and the program's operating level in the CPU, and the GDT that points to is a table of 64 bits in a single unit, in this table It saves the code snippet that the program runs and the starting address of the data segment, as well as information about the corresponding segment limit and page Exchange, the program run level, and the granularity of the memory. Once a program has an out-of-bounds access, the CPU generates corresponding exception protection, so segmentation fault appears (source network)
About some theories of knowledge in this, do not describe, and finally will give a blog link, introduction.
Next, use small code experiments to illustrate some of the problems
One
#include<stdio.h>intchar"hello"; s[1]=‘h‘return0;}
This piece of code is obviously wrong, but the syntax is fine. Compile through vs.
However, this can cause illegal writing, where the string is neither stored on the stack nor in the heap, in the global zone, or in the code snippet, the memory is not writable,
VS at run time the following error occurred
Unhandled exception at 0x002f1398 in segdefault.exe:0xc0000005:access violation writing location 0x002f573d.
Two
#include<stdio.h>intint* s=(int*)0xd000001; *s=1return0;}
This code will be no problem from the syntax, vs compile through, but the obvious problem is that the pointer to the unknown memory, or this is a wild pointer, this memory may not be assigned to the current process of the operating system, so the operating system also produces an exception. The following error occurred with the VS run
Unhandled exception at 0x00b21398 in segdefault.exe:0xc0000005:access violation writing location 0x0d000001.
Three
#include<stdio.h>intint a=1return &a;}intintprintf("%d\n",*preturn0;}
For this piece of code, VS has a warning at compile time
Warning C4172:returning address of local variable or temporary
That is to return the local variable address, that is, the stack of F function will be released at the end of the function, if the contents of the stack is returned, the actual is not desirable,
Look at the results of VS run
There is nothing wrong with the result, or the pointer is pointing to an area outside the current stack space, and there is a stack overflow, but there is a question of how the stack is recycled when a function ends.
If you see that the Assembly knows, it just modifies the ESP pointer, so the next time the space stack is not used, the contents are still there.
Four
The following code is seen online for testing the heap
#include <stdio.h> #include <stdlib.h> #define K 1024x768 int Main () {char * C; int i=0 ; c= (char *) malloc ( 1 ); while (1 ) {c+=k; *c= ' a ' ; printf ( "overflow%dk\n" , i); i++; } return 0 ;}
This code runs on vs is not passed and has the following error
Unhandled exception at 0X010C33F3 in segdefault.exe:0xc0000005:access violation writing location 0x00575208.
And under the Linux GCC platform
The compilation passes and can be run with the following results
That is, in the heap, if the overflow, when the 130k will produce a segment error, this is related to the memory allocation of the OS, later on to the Linux memory allocation in the specific find out why.
Five
#include <stdio.h>#include <stdlib.h>#define K-1024x768intMain () {char* C;intI=0; C= (Char*)malloc1);*c=' A ';printf("%p\ n", c);printf("%c\ n",*c); Free (c);printf("%p\ n", c);printf("%c\ n",*c);return 0;}
This code actually tests what the free,free implementation is,
In Linux
That is, after free, the address that points to the pointer does not change, or points to the heap space, but the contents change,
Prints the contents of the heap as integers
This means that free allocates the heap space after zero.
Six
#include<stdio.h>#include<stdlib.h>#include <string.h>voidchar c=‘a‘memset(&c,0x55,128);}intprintf("return ?\n"return0;}
The problem is that the contents of the stack space are modified in function f, because before entering F, main will press the address of the instruction to be executed after F returns, thus modifying the address content so that it returns a wild pointer.
VS appears the following error.
Unhandled exception at 0x55555555 in segdefault.exe:0xc0000005:access violation.
All of the above is actually thought to be avoided as much as possible,
On the internet to see someone summed up how to be careful to avoid
1.
Unlike other programmers, you have a thorough understanding of your program.
means to achieve precise control. Especially in the allocation and release of memory. Before you manipulate each pointer, you should be aware of what it
To the source of memory (stack, heap, global Zone), and to know the lifetime of this memory. Only by understanding the use of memory, can
The production. S I G S E G V to maximize avoidance.
2.
Use assert extensively to check
3, pay more attention to the warning generated at compile time
Data link
http://blog.csdn.net/jk110333/article/details/19685127 (section error debugging)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Re-recognize segment fault