- 论坛徽章:
- 0
|
看到大量的内容类似的基本指针问题日复一日的出现,总会让常来逛逛的人觉得心里变得有点“躁”。建议能不能集中一下,先扔块砖,看这个贴子命运如何...
译自《FreeBSD Developers' Handbook :2.4.1.9》
When my program dumped core, it said something about a “segmentation fault”. What is that?
<<我程序产生core dump的时候,它说类似“segmentation fault”之类的话,这是什么?>>
This basically means that your program tried to perform some sort of illegal operation on memory; UNIX is designed to protect the operating system and other programs from rogue programs.
Common causes for this are:
<<基本这意味着你的程序尝试对内存执行一些非法操作;UNIX设计为保护操作系统及其他程序免受不良程序侵害。
常有的原因为:>>
* Trying to write to a NULL pointer, eg
<<尝试写入一个NULL指针,例如>>
- char *foo = NULL;
- strcpy(foo, "bang!");
复制代码
*Using a pointer that has not been initialized, eg
<<使用未初始化指针,例如>>
- char *foo;
- strcpy(foo, "bang!");
复制代码
The pointer will have some random value that, with luck, will point into an area of memory that is not available to your program and the kernel will kill your program before it can do any damage. If you are unlucky, it will point somewhere inside your own program and corrupt one of your data structures, causing the program to fail mysteriously.
<<这个指针会有一个随机值,如果运气好的话,它将会指向一个你程序不可用的内存区域,那么内核将会在它造成什么伤害前干掉你的程序。如果你运气不好,那么它可能指向某个属于你程序的地方然后毁掉你的某个数据结构,导致程序莫名其妙的失败。>>
*Trying to access past the end of an array, eg
<<尝试超出数组末端的访问,例如>>
- int bar[20];
- bar[27] = 6;
复制代码
*Trying to store something in read-only memory, eg
<<尝试向只读(属性)内存内放入什么,例如>>
- char *foo = "My string";
- strcpy(foo, "bang!");
复制代码
UNIX compilers often put string literals like "My string" into read-only areas of memory.
UNIX的编译器常将“My sring”这类字符串常量放入内存中的只读(属性)区域。
*Doing naughty things with malloc() and free(), eg
<<对malloc(),free()做不适当操作,例如>>
or <<或>>
- char *foo = malloc(27);
- free(foo);
- free(foo);
复制代码
-----
Making one of these mistakes will not always lead to an error, but they are always bad practice. Some systems and compilers are more tolerant than others, which is why programs that ran well on one system can crash when you try them on an another.
<<犯其中的这些毛病并不一定总是导致错误,但这些始终都是坏习惯。一些系统及编译器比其他一些更宽容,这就是为什么有些程序曾在一些系统运行良好而当你在另一些系统尝试他们的时候会当掉。>>
---------end---------- |
|