malloc / new operator

This is a discussion on malloc / new operator within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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,211
    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
    22,265
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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, 10:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03: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-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21