-2

Below is my code which compiles fine on eclipse. It is fully functional, just a basic program that prints memory addresses.

#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);
}

however when I tried to manually compile it:

gcc memory_segments.cpp

i get tons of errors:

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

Can someone tell me what I'm doing wrong?

NewFile
  • 501
  • 5
  • 10
  • 16
  • 1
    no reason to cast your malloc results. compiler does it implicitly. Also you should always check your return value from malloc for negative and exit immediately if there is an error – Stelios Savva Nov 24 '13 at 14:21
  • 2
    Well you can see that most of the errors are actually warnings which are easily understood and easily fixed (I hope). You're only actual error is a new one to me, but this link might help, http://stackoverflow.com/questions/329059/what-is-gxx-personality-v0-for. – john Nov 24 '13 at 14:22
  • 2
    You really should type `man printf` and see what exactly is meant by `%10lu`. You are basically assuming a pointer has the same size as a `long unsigned int`. Try `%p` instead. Or just use iostreams. – kccqzy Nov 24 '13 at 14:23
  • 1
    @Stelios: title and tags say this is C++ (hard to believe though). So the cast is necessary. – Mat Nov 24 '13 at 14:27

2 Answers2

2

The file extension is CPP, so GCC thinks that it is a C++ file. However you're trying to link with GCC, which doesn't know how to link c++ programs. Use g++ for the commandline, or rename the file to a .c file.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 1
    Are you sure? All those warnings will persist, but the actual errors are link errors that should be fixed. – Ernest Friedman-Hill Nov 24 '13 at 14:26
  • 1
    what do you mean by link errors? theres only one file, so theres nothing else to link onto... right? – NewFile Nov 24 '13 at 14:27
  • 2
    @NewFile: wrong. The only error in what you posted above is a linker (`ld`) error. Doesn't matter that there's only one source file. – Mat Nov 24 '13 at 14:32
1

You want to print memory addresses, they are pointers. The printf formatting tokens expect integers. You should cast them to integers, so extend &pointerVariable to (unsigned long int)&pointerVariable.

peterh
  • 11,875
  • 18
  • 85
  • 108