- 論壇徽章:
- 0
|
看到大量的內(nèi)容類似的基本指針問題日復(fù)一日的出現(xiàn),總會讓常來逛逛的人覺得心里變得有點(diǎn)“躁”。建議能不能集中一下,先扔塊磚,看這個貼子命運(yùn)如何...
譯自《FreeBSD Developers' Handbook :2.4.1.9》
When my program dumped core, it said something about a “segmentation fault”. What is that?
<<我程序產(chǎn)生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:
<<基本這意味著你的程序嘗試對內(nèi)存執(zhí)行一些非法操作;UNIX設(shè)計(jì)為保護(hù)操作系統(tǒng)及其他程序免受不良程序侵害。
常有的原因?yàn)椋?gt;>
* Trying to write to a NULL pointer, eg
<<嘗試寫入一個NULL指針,例如>>
- char *foo = NULL;
- strcpy(foo, "bang!");
復(fù)制代碼
*Using a pointer that has not been initialized, eg
<<使用未初始化指針,例如>>
- char *foo;
- strcpy(foo, "bang!");
復(fù)制代碼
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.
<<這個指針會有一個隨機(jī)值,如果運(yùn)氣好的話,它將會指向一個你程序不可用的內(nèi)存區(qū)域,那么內(nèi)核將會在它造成什么傷害前干掉你的程序。如果你運(yùn)氣不好,那么它可能指向某個屬于你程序的地方然后毀掉你的某個數(shù)據(jù)結(jié)構(gòu),導(dǎo)致程序莫名其妙的失敗。>>
*Trying to access past the end of an array, eg
<<嘗試超出數(shù)組末端的訪問,例如>>
- int bar[20];
- bar[27] = 6;
復(fù)制代碼
*Trying to store something in read-only memory, eg
<<嘗試向只讀(屬性)內(nèi)存內(nèi)放入什么,例如>>
- char *foo = "My string";
- strcpy(foo, "bang!");
復(fù)制代碼
UNIX compilers often put string literals like "My string" into read-only areas of memory.
UNIX的編譯器常將“My sring”這類字符串常量放入內(nèi)存中的只讀(屬性)區(qū)域。
*Doing naughty things with malloc() and free(), eg
<<對malloc(),free()做不適當(dāng)操作,例如>>
or <<或>>
- char *foo = malloc(27);
- free(foo);
- free(foo);
復(fù)制代碼
-----
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.
<<犯其中的這些毛病并不一定總是導(dǎo)致錯誤,但這些始終都是壞習(xí)慣。一些系統(tǒng)及編譯器比其他一些更寬容,這就是為什么有些程序曾在一些系統(tǒng)運(yùn)行良好而當(dāng)你在另一些系統(tǒng)嘗試他們的時候會當(dāng)?shù)簟?gt;>
---------end---------- |
|