[an error occurred while processing this directive]
[an error occurred while processing this directive]int main(){ int A[3]; printf("a"); A[3] = 1; return 0; }±êðÀs·éÆAÊÍPÉSegmentation FaultÆo龯ÅADZª¨©µ¢©í©çÈ¢B ±êð
printf("a\n");Í
printf("a"); fflush(stdout);Æ·éÆA¿áñÆað\¦µ½ãÅ¿ÄêéB
#define OUT(A) printf("%s = %d\n",#A, A)Æ¢¤}Nðè`µÄ¨ÆA
int main(){ int i = 3; OUT(i); }Ʒ龯ÅA
i = 3ݽ¢Èoͪ¾çêéB±êÍ%d©çª©éæ¤É®Éµ©g¦È¢¯ÇAC++gÁÄéÈç
#include<iostream> #define OUT(A) std::cout << #A << '='<<A << std::endl; int main(){ int i = 3; OUT(i); }Æ·éÆAintÅàdoubleÅàstringÅàg¦Ü·B
#include <assert.h> ±êÍæªÉ
...
assert(x != 0);
±¤·éÆAàµxª0ÉÈÁ½Æ«É¿å¤Ç»±Å¿ÄêÜ·B
ÌgdbÆgÝí¹éÆAfobOÌ¢¡ûÉÈèÜ·B
$ gcc test.c -g -o testÅAgdbÉHí¹éB
$ gdb test (gdb)±±ÅA?ÆÅÂÆg¦éR}hÌêªoéB
#include <stdio.h> void init(int *A, int size){ int i; for(i = 0; i < size; i++) A[i] = i * i; } void print(int *A, int size){ int i; for(i = 0; i < size; i++) printf("A[%d] = %d\n", i, A[i]); } int main(void){ int i; int size = 4; int A[4]; init(A, size); A[1000] = 10; print(A, size); }Àsµ½çA½ªA[1000] = 10;ÌsÅ¿Ü·B »±Å±êðgdbÅÀsB
$ gdb test (gdb) run (Æè ¦¸Às) Program received signal SIGSEGV, Segmentation fault. main () at test.c:22 (gdb) where (¡Ç±É¢é©ð\¦) #0 main () at test.c:22 (gdb) list (¡¢é ½èÌ\[Xð\¦) 21 init(A, size); 22 A[1000] = 10; 23 print(A, size); (gdb) print A[0] (A[0]Ìgð\¦) $1 = 0 (gdb) call print(A, size) (printÁÄÖðÄñÅÝé) A[0] = 0 A[1] = 1 A[2] = 4 A[3] = 9 (gdb)±ñÈ´¶ÅAAÏgðÈPÉ̼¯éB
(gdb) break init
Æ·éÆAinit()ÉüÁ½Æ±Å~Üé©çA»±©çstepÅês¸Âiß½èÅ«éB
#include<stdio.h> typedef struct kei_{ int i; } kei; kei *new(){ kei *k; k = (kei *)malloc(sizeof(kei)); k->i = 23; /* ±±Éreturn kªÈ¢ */ } int main(){ kei *k; k = new(); printf("%d\n", k->i); }±êðÀs·éÆAlÌ«ÅÍ23Æ\¦³êܵ½ªARpCIvVð-O3ÉϦéÆ®©ÈÈèܵ½B ±¤¢¤½¿Ì«¢oOðh®ÉÍA
% gcc -g -Wall test.cƵÄRpC·éÆA
warning: control reaches end of non-void functionÆxðoµÄêÜ·B
gcc test.c -lefenceƷ龯ŷBo½oCiÍÊÉÀsµÄº³¢B ÀsÉ"Electric FenceÈñÆ©"ÁÄbZ[WªoêοáñÆN³êÄÜ·B
#ifdef DEBUG #define DPRINT(s...) fprintf(stderr, s) #else #define DPRINT(s...) #endifR[hÌ٤ͱñÈÓ¤ÉB
DPRINT("debug message %d\n", i);»Ì¤¦ÅAdebug µ½¢Æ«Í #define DEBUG ·éÆAbZ[Wª\¦³êéB ÀèµÄ«½çAdefine DEBUG ð͸¹Îæ¢B
void err_printf(const char *format, ...){ va_list arg; char buf[MAX_LEN]; va_start(arg, format); vsprintf(buf, format, arg); va_end(arg); fprintf(FP, buf); }