下面是我在eclipse上編譯好的代碼。 它功能齊全,只是打印內存地址的基本程序。Linux中的C++編譯錯誤
#include <stdio.h>
#include <stdlib.h>
int g0; /* global variable, uninitialized */
int g1 = 14; /* global variable, initialized */
int g2[1000]; /* global variable, uninitialized */
int g3 = 16; /* global variable, initialized */
int g4; /* global variable, uninitialized */
void proc1();
void proc2();
int main()
{
extern int etext[], edata[], end[];
int lc0; /* local variable, stored on stack */
int lc1 = 27; /* local variable, stored on stack; mix init and uninit */
int lc2; /* local variable, stored on stack */
static int ls0; /* local static variable, uninitialized data */
static int ls1 = 19; /* local static variable, initialized data */
int *pheap1;
int *pheap2;
pheap1 = (int *) malloc(sizeof(int));
pheap2 = (int *) malloc(sizeof(int));
printf("\n\n---------- main -------------------\n\n");
printf("%8p (%10lu): Last address\n",
0xffffffff, 0xffffffff);
printf("%8p (%10lu): Address etext\n",
etext, etext);
printf("%8p (%10lu): Address edata\n",
edata, edata);
printf("%8p (%10lu): Address end\n",
end, end);
printf("%8p (%10lu): Address of code for proc1\n",
&proc1, &proc1);
printf("%8p (%10lu): Address of code for proc2\n",
&proc2, &proc2);
printf("%8p (%10lu): Address of uninitialized global variable g0\n",
&g0, &g0);
printf("%8p (%10lu): Address of initialized global variable g1\n",
&g1, &g1);
printf("%8p (%10lu): Address of uninitialized global array g2\n",
&g2[0], &g2[0]);
printf("%8p (%10lu): Address of initialized global variable g3\n",
&g3, &g3);
printf("%8p (%10lu): Address of uninitialized global variable g4\n",
&g4, &g4);
printf("%8p (%10lu): Address heap1 in heap space\n",
pheap1, (int) pheap1);
printf("%8p (%10lu): Address heap2 in heap space\n",
pheap2, (int) pheap2);
printf("%8p (%10lu): Address of local variable lc0\n",
&lc0, &lc0);
printf("%8p (%10lu): Address of local variable lc1\n",
&lc1, &lc1);
printf("%8p (%10lu): Address of local variable lc2\n",
&lc2, &lc2);
printf("%8p (%10lu): Address of local uninitialized static var ls0\n",
&ls0, &ls0);
printf("%8p (%10lu): Address of local initialized static var ls1\n",
&ls1, &ls1);
proc1();
proc2();
return 0;
}
void proc1() {
int lc3;
int lc4 = 37;
printf("\n\n----------- proc1 ------------------\n\n");
printf("%8p (%10lu): Address of code for proc1\n",
&proc1, &proc1);
printf("%8p (%10lu): Address of global variable g0\n",
&g0, &g0);
printf("%8p (%10lu): Address of global variable g1\n",
&g1, &g1);
printf("%8p (%10lu): Address of global variable g2\n",
&g2[0], &g2[0]);
printf("%8p (%10lu): Address of global variable g3\n",
&g3, &g3);
printf("%8p (%10lu): Address of global variable g4\n",
&g4, &g4);
printf("%8p (%10lu): Address of local variable lc3\n",
&lc3, &lc3);
printf("%8p (%10lu): Address of local variable lc4\n",
&lc4, &lc4);
}
void proc2() {
int lc5;
int lc6 = 51;
static int ls2;
static int ls3 = 47;
printf("\n\n------------ proc2 -----------------\n\n");
printf("%8p (%10lu): Address of code for proc2\n",
&proc2, &proc2);
printf("%8p (%10lu): Address of global variable g0\n",
&g0, &g0);
printf("%8p (%10lu): Address of global variable g1\n",
&g1, &g1);
printf("%8p (%10lu): Address of global variable g2\n",
&g2[0], &g2[0]);
printf("%8p (%10lu): Address of global variable g3\n",
&g3, &g3);
printf("%8p (%10lu): Address of global variable g4\n",
&g4, &g4);
printf("%8p (%10lu): Address of local variable lc5\n",
&lc5, &lc5);
printf("%8p (%10lu): Address of local variable lc6\n",
&lc6, &lc6);
printf("%8p (%10lu): Address of local uninitialized static var ls2\n",
&ls2, &ls2);
printf("%8p (%10lu): Address of local initialized static var ls3\n",
&ls3, &ls3);
}
然而,當我試圖手動編譯:
gcc memory_segments.cpp
我得到噸的錯誤:
U91:~/Documents/workspace/memory_segments$ gcc memory_segments.cpp
memory_segments.cpp: In function ‘int main()’:
memory_segments.cpp:97: warning: format ‘%8p’ expects type ‘void*’, but argument 2 has type ‘unsigned int’
memory_segments.cpp:97: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘unsigned int’
memory_segments.cpp:100: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:102: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:104: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:107: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:109: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:112: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:114: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:116: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:118: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:120: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:123: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’
memory_segments.cpp:125: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’
memory_segments.cpp:128: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:130: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:132: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:135: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:137: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp: In function ‘void proc1()’:
memory_segments.cpp:150: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:152: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:154: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:156: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:158: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:160: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:162: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:164: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp: In function ‘void proc2()’:
memory_segments.cpp:175: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:177: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:179: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:181: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:183: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:185: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:187: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:189: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:191: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:193: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
/tmp/ccRXp46P.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[email protected]:~/Documents/workspace/memory_segments$ gcc memory_segments.cpp -lpthread
memory_segments.cpp: In function ‘int main()’:
memory_segments.cpp:97: warning: format ‘%8p’ expects type ‘void*’, but argument 2 has type ‘unsigned int’
memory_segments.cpp:97: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘unsigned int’
memory_segments.cpp:100: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:102: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:104: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:107: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:109: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:112: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:114: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:116: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:118: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:120: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:123: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’
memory_segments.cpp:125: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int’
memory_segments.cpp:128: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:130: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:132: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:135: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:137: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp: In function ‘void proc1()’:
memory_segments.cpp:150: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:152: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:154: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:156: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:158: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:160: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:162: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:164: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp: In function ‘void proc2()’:
memory_segments.cpp:175: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘void (*)()’
memory_segments.cpp:177: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:179: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:181: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:183: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:185: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:187: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:189: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:191: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
memory_segments.cpp:193: warning: format ‘%10lu’ expects type ‘long unsigned int’, but argument 3 has type ‘int*’
/tmp/ccJoHGm5.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
誰能告訴我什麼,我做錯了什麼?
沒有理由施放你的malloc結果。編譯器會隱式執行它。你也應該經常檢查你的malloc的返回值是否爲負數,如果出現錯誤,立即退出 –
你可以看到大多數錯誤實際上是易於理解和容易修復的警告(我希望)。你只是實際的錯誤對我來說是一個新的錯誤,但這個鏈接可能會有所幫助,http://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for。 – john
你真的應該輸入'man printf',看看'%10lu'究竟是什麼意思。你基本上假設一個指針與'long unsigned int'具有相同的大小。請嘗試使用'%p'。或者只是使用iostreams。 – kccqzy