Thread: malloc / new operator

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Norway
    Posts
    3

    malloc / new operator

    I'm reading a tutorial from learncpp.com now, and I'm trying to create a variable(/pointer) with malloc/new, print it, and delete it.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int &argc, char **argv) {
        int *pnValue = (int*) malloc(7);
        *pnValue = 7;
        printf("Pointer adress: &#37;p\n", pnValue);
        free(pnValue);
    }
    but, I got this:

    Code:
    mikalv@mikalv-laptop:~/Projects/CExamples$ gcc testalloc.cpp -o kake
    /tmp/ccm9ZMkq.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    and I also tried this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int &argc, char **argv) {
        int *pnValue = new int;
        *pnValue = 7;
        printf("Pointer adress: %p\n", pnValue);
        delete pnValue;
    }
    but this also fail, but with another message.
    Code:
    /tmp/cc0FszjM.o: In function `main':
    dynamicallocation.cpp:(.text+0x19): undefined reference to `operator new(unsigned int)'
    dynamicallocation.cpp:(.text+0x43): undefined reference to `operator delete(void*)'
    /tmp/cc0FszjM.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Can someone tell me what I'm doing wrong? and why I get these messages, so I can learn to next time..

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Offhand, I see three mistakes:

    1. argc shouldn't be declared as being a reference, but just a straight int.... ie. int argc.
    2. malloc() needs an argument as to how many bytes to allocate. While it's a possibiliy an int may be seven bytes in size, that is unlikely. Allocate sizeof(int) or perhaps more preferably, sizeof(*pnValue).
    3. You should be using the C++ header file equivalents of the C header files: ie. cstdio instead of stdio.h and cstdlib instead of stdlib.h.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    They both look like they should (or at least could) work, though the first one is weird because you allocate 7 bytes for an int, but an int is more likely 4 or 8 bytes. Also, note that main's first argument is usually passed by value, not by reference.

    I suggest that you try:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char* argv[]) {
        int* pnValue = (int*)malloc(sizeof *pnValue);
        *pnValue = 7;
        printf("Pointer adress: &#37;p\n", (void*)pnValue);
        free(pnValue);
    }
    or for a more C++ish approach:
    Code:
    #include <iostream>
    
    int main(int argc, char* argv[]) {
        int* pnValue = new int;
        *pnValue = 7;
        std::cout << "Pointer adress: " << pnValue << '\n';
        delete pnValue;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    These errors mean that you're not linking against the C++ runtime. Compile with g++, not gcc.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM