C++ newbie struggling with malloc

This is a discussion on C++ newbie struggling with malloc within the C++ Programming forums, part of the General Programming Boards category; I am looking at some legacy C code. char * b = (char *)malloc(l+1) ; If i were to use ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    20

    C++ newbie struggling with malloc

    I am looking at some legacy C code.

    char * b = (char *)malloc(l+1) ;

    If i were to use new operator, it will be declared as

    char * b = new b ;

    Would that be correct?

    Thx
    Lawina

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    This might be more likely:
    Code:
    char* b = new char[l+1];
    But then you might just use std::string instead.
    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

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    14
    and be sure to use delete in place os the corresponding free().

    Code:
    char* b = new char[l+1];
    delete [] b;

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    20
    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. newbie: need help using malloc in a loop
    By happyclown in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 04:59 PM
  3. 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
  4. Newbie question: Problem with malloc
    By Ikim in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 09:11 PM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

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