Thread: C++ newbie struggling with malloc

  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
    28,413
    This might be more likely:
    Code:
    char* b = new char[l+1];
    But then you might just use std::string instead.
    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

  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, 05: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, 12:26 PM
  4. Newbie question: Problem with malloc
    By Ikim in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 10:11 PM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM