CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2003
    Posts
    2

    Question Is there a problem using malloc and new together?

    Hi,
    I try to solve a problem in a C++ code I did not write:
    When I run the application I get a memory problem, and the message says that memory was allocated with new and ran over with malloc.
    Is this really the problem, or is it a simptom for something else?
    Is there a known problem with using new and malloc in the same application?
    Another note: the problem appears only when compiled with debug information.
    The application runs under OS/2 operating system.

    The relevant parts of the code:
    Code:
    void *d_area;
    int dsize=10;
    d_area = malloc (dsize);
    --------
    char *penv = getenv ("xxxx");
    char *grp;
    grp = new char (sizeof (penv) + 1);
    Please note that this is only the suspected lines, and the statement int dsize=10; and similar are only shown here for understanding the inportant lines (new and malloc)

    Thanks.
    Last edited by sapird; July 2nd, 2003 at 08:50 AM.

  2. #2
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484

    Re: Is there a problem using malloc and new together?

    Originally posted by sapird
    Code:
    void *d_area;
    int dsize=10;
    d_area = malloc (dsize);
    --------
    char *penv = getenv ("xxxx");
    char *grp;
    grp = new char (sizeof (penv) + 1);
    Not that this will fix your problem, but from just a quick glance I noticed that grp is not allocated correctly.....it should be:

    Code:
    grp = new char[sizeof(penv) + 1];

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is there a problem using malloc and new together?

    Originally posted by sapird
    Hi,
    I try to solve a problem in a C++ code I did not write:
    When I run the application I get a memory problem, and the message says that memory was allocated with new and ran over with malloc.
    Is this really the problem, or is it a simptom for something else?
    Is there a known problem with using new and malloc in the same application?
    Another note: the problem appears only when compiled with debug information.
    The application runs under OS/2 operating system.
    Well, you better fix the OS/2 version if you coded this:
    Code:
    grp = new char (sizeof (penv) + 1);
    This is incorrect. What you did here was allocate memory for a single character, and initialized it to sizeof(penv) + 1. The correct syntax is:
    Code:
    grp = new char [sizeof (penv) + 1];
    Be lucky that the OS/2 version hasn't crashed -- yet.

    Also, there is no problem with using new and malloc in the same application -- just don't mix up using malloc and delete, new with free. The rules are simple:

    Allocate with malloc, deallocate with free.
    Allocate with new, deallocate with delete.
    Allocate with new[], deallocate with delete[].

    My suggestion is to always use new or new[] for C++ programs. There is never a need to use malloc() for a C++ program. The reason why is that new calls the constructor for your object, so if your object was more complex than chars, malloc would just cause more potential problems.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 2nd, 2003 at 10:40 AM.

  4. #4
    Join Date
    Aug 2002
    Posts
    58
    Just to add to the discussion,

    Code:
    grp = new char [sizeof (penv) + 1];
    makes me think you really want to do:

    Code:
    grp = new char [strlen(penv) + 1];
    since unless I'm crazy, sizeof(penv) is always 4 (a.k.a. the size of a pointer on a 32-bit system). I could be wrong, though...

    Regards,
    Bassman

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Bassman
    Just to add to the discussion,

    Code:
    grp = new char [sizeof (penv) + 1];
    makes me think you really want to do:

    Code:
    grp = new char [strlen(penv) + 1];
    Yes, another bug. It should be strlen(penv) not sizeof(penv).

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Not to change anything anyone has said, but MFC sometimes forces you to use malloc because that is the way they have done things for whatever reason. In particular they use it for
    CMemfile, for dialog templates and for bitmap info header.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Jul 2003
    Posts
    2

    Smile

    Thanks, guys.
    The bug was in the line
    Code:
    grp = new char (sizeof (penv) + 1);
    which was changed to
    Code:
    grp = new char [strlen(penv) + 1];
    Who didn't I see that?

    Well, thank you Paul McKenzie and Bassman of noticing the strlen/sizeof problem and thank you Paul McKenzie and HeartBreakKid for noticing the perenthesis problem.

    I am very gratefull. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured