Correct usage of malloc()

This is a discussion on Correct usage of malloc() within the C++ Programming forums, part of the General Programming Boards category; How do you correctly use malloc()? I.E. without casts. I thought this was how: Code: int* data = malloc(sizeof(int) * ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Correct usage of malloc()

    How do you correctly use malloc()? I.E. without casts. I thought this was how:

    Code:
    int* data = malloc(sizeof(int) * 10);
    But my compiler complains that it 'cannot convert void* to int*', so I'm forced to use a '(int*)' cast.

    Oh yeah, and when do I use each of reinterpret_cast, static_cast and const_cast?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    malloc returns void*, so you will have to cast. Since you posted this is the c++ forum and not the c forum, I am curious to why you aren't using new instead?

    int* data = new int[10];
    Last edited by Darryl; 07-24-2005 at 04:57 AM.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I was just wondering really. Is malloc() considered standard C++?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes std::malloc() is a standard function. Its a holdover from C and is generally nowhere near as useful as the new operator but it does have its moments.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    That's good to know. What about reinterpret_cast? Is it the same as a standard C cast?

    Code:
    int* data = (int*) malloc(10); 
     
    int* data = reinterpret_cast<int*> (malloc(10));
    ??
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    New calls constructors, malloc does not.
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class foo {
      int bar;
      public:
        foo();
    };
    
    foo::foo ( ) {
      cout << "Made a foo at address " << reinterpret_cast<void*>(this) << endl;
    }
    
    int main ( void ) {
      foo one;
      foo *bad = (foo*)malloc ( sizeof *bad );
      foo *good = new foo;
      cout << "One  is at address " << reinterpret_cast<void*>(&one) << endl;
      cout << "Bad  is at address " << reinterpret_cast<void*>(bad) << endl;
      cout << "Good is at address " << reinterpret_cast<void*>(good) << endl;
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by ahluka
    That's good to know. What about reinterpret_cast? Is it the same as a standard C cast?

    Code:
    int* data = (int*) malloc(10); 
     
    int* data = reinterpret_cast<int*> (malloc(10));
    ??
    static_cast is same as C cast
    taken from MSDN:
    The reinterpret_cast operator allows any pointer to be converted into any other pointer type. It also allows any integral type to be converted into any pointer type and vice versa

    The static_cast operator converts expression to the [casted type] based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.r

    The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.

    [and for completeneess]

    The expression dynamic_cast<type-id>( expression ) converts the operand expression to an object of type type-id. The type-id must be a pointer or a reference to a previously defined class type or a pointer to void.
    There is a more detail explanation on each specific MSDN page and I'd encourage you to read them fully.

    http://msdn.microsoft.com/library/de...express_71.asp

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks for all your replies
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    none of the casts are exactly the same as C style.
    Each cast operator has its rules about what it can cast to what. reinterpret_cast is the one that is least portable. It is implementation defined how this one works. In general it is just a reinterpretation of the bits but this is not necessarily so.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Instead of malloc you can use the std::allocator to allocate memory without constructing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  3. quick malloc question
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 10-15-2005, 05:41 AM
  4. Simple Question on Correct Usage of memset
    By deadpoet in forum C++ Programming
    Replies: 2
    Last Post: 03-16-2004, 07:58 AM

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