下面是我在eclipse上编译好的代码。 它功能齐全,只是一个打印内存地址的基本程序。
#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
devon@D3V-U91:~/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
有人能告诉我我做错了吗?
答案 0 :(得分:2)
文件扩展名为CPP,因此GCC认为它是C ++文件。但是,您正在尝试与GCC链接,GCC不知道如何链接c ++程序。将g ++用于命令行,或将文件重命名为.c文件。
答案 1 :(得分:1)
您想要打印内存地址,它们是指针。 printf格式化标记期望整数。你应该将它们转换为整数,所以将&amp; pointerVariable扩展为(unsigned long int)&amp; pointerVariable。