what is the 'best' way of allocating memory, and what are the differences

This is a discussion on what is the 'best' way of allocating memory, and what are the differences within the C++ Programming forums, part of the General Programming Boards category; Okay, the two major ways I know of allocating memory are: Type *pPointer = new Type[NumberOfTypes]; and pPointer = (Type*)malloc(sizeof(Type)); ...

  1. #1
    Shadow12345
    Guest

    what is the 'best' way of allocating memory, and what are the differences

    Okay, the two major ways I know of allocating memory are:
    Type *pPointer = new Type[NumberOfTypes];
    and
    pPointer = (Type*)malloc(sizeof(Type));

    are there any pros, cons, major differences between the two? I ask because I would like to see something done the first way, but then turn around and do it the second way but achieve the same results

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    i vote for new and delete
    PHP and XML
    Let's talk about SAX

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I'd say it all depends on the situation. There are a few different ways to use new to get your memory (like placement new, or custom memory pooling) to system specific calls like GlobalAlloc() in Windows. I wouldn't go as far as saying one is better than another, most of them do different things.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >are there any pros, cons, major differences between the two?
    Wow, what a controversial subject. Well, here goes:

    new/delete: These not only allocate blocks of memory, they also call constructors and destructors. In C++ this is a Good Thing.

    malloc/calloc/realloc/free: These just work with uninitialized blocks of memory, you can get into more trouble more easily with these than new/delete (but not much).

    I follow the guideline that if I'm using C I'll use malloc and family, and only with C. With C++ I prefer new and delete for most of my memory needs and the allocator class for everything else. C++ is all about convenience, so I make use of it.

    -Prelude
    My best code is written with the delete key.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    I follow the guideline that if I'm using C I'll use malloc and family, and only with C. With C++ I prefer new and delete for most of my memory needs and the allocator class for everything else. C++ is all about convenience, so I make use of it.

    This is what you see in pretty much all the C++/C texts.

    C++ new/delete
    C - malloc/free
    Mr. C: Author and Instructor

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    i pretty much use new and delete.. it is easier.. and less of hasels when freeing up...

Popular pages Recent additions subscribe to a feed

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