Thread: malloc vs new

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    malloc vs new

    Hello

    I have an array of pointers. I want to take a pointer from this array and return this.
    If I use new I get a segmentation fault. But when I use malloc it is ok.


    Code:
    SegFault
    ClassEx* Example::doSomething()
    {
              ClassEx *array = new ClassEx[5];
              fill in
    
              return &array[2];
    }
    
    Ok
    ClassEx* Example::doSomething()
    {
              ClassEx *array = (ClassEx*) malloc(sizeof(ClassEx) * 5);
              fill in
    
              return &array[2];
    }
    Can someone explain me why malloc works here and new doesn't.

    Also how do I return a pointer to an element in an array using new?

    Thank you

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The difference is that malloc doesn't construct any objects. So if there's something wrong with the constructor...

    Or there is other undefined behavior in the program and this is just how it manifests.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Show the code for the constructor.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    Show the code for the constructor.
    .... of ClassEx.

    If the problem is not in that constructor, then the problem is somewhere else in your code. That may seem like stating the obvious, but one of the joys of program crashes is that they are not always immediate. The cause of the problem is typically in code executed before the crash occurs. Unfortunately a trap for new players - and quite a few experienced players - is insisting on only looking at the code where the crash occurs.
    Last edited by grumpy; 11-20-2010 at 06:05 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Actually I'm thinking now that there are actually several other things that are suspect here. For one, if it's returning the address of array element two, then how is the memory freed later?
    The question is not
    how do I return a pointer to an element in an array using new?
    but, why would you want to return a pointer to an element within an array that is dynamically allocated within that function?

    Secondly, whatever code is where you wrote "fill in" could also well be the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you for the responses.
    The problem was indeed in the constructor.

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. 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
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM