CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2009
    Posts
    8

    Problem with delete operator

    Hello,

    I have a source code like this:

    void func()
    {
    char* str;
    str=new char[5*sizeof(char)];
    .
    .
    .
    delete line;
    }

    When the program get to the "delete line", It shows the following error:
    Debug assertion failed!
    expression: _BLOCK_TYPE_IS_VALID(phead->nblockuse)

    Why does I get the error message?
    As I understand, I can use the delete operator for every varabile that was allocated with the "new" operator...

    Thnk you,

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Problem with delete operator

    Match new with delete and new[] with delete[].

    On the other hand, if you are going to create an array of exactly 5 chars and destroy it at the end of the function, you might as well write:
    Code:
    char str[5];
    // ...
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2008
    Posts
    61

    Arrow Re: Problem with delete operator

    If you want an array with 5 chars, you should use:
    Code:
    char a[5]; // allocated on stack, no need delete
    or
    Code:
    char *a = new char[5]; // allocated on heap
    delete a;

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Problem with delete operator

    Quote Originally Posted by felixb View Post
    Code:
    char* str;
    str=new char[5*sizeof(char)];
    .
    .
    .
    delete line;
    Why does I get the error message?
    Because you new "str" and delete "line"?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Problem with delete operator

    Quote Originally Posted by Emerald214 View Post
    If you want an array with 5 chars, you should use:
    Code:
    char *a = new char[5]; // allocated on heap
    delete[] a;
    see laserlight's post

  6. #6
    Join Date
    Oct 2009
    Posts
    8

    Re: Problem with delete operator

    it's just an example, I haven't posted the full source.
    In the full source, it isn't "char[5*sizeof(...)" but it is: "char[i*sizeof(...)]", so I do need dynamic allocation here.

    I changed the source to this:
    void func()
    {
    char* str;
    str=new char[5*sizeof(char)];
    .
    .
    .
    delete[] str;
    }

    But I still receive the assertion error.

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Problem with delete operator

    then, as the usual requirement, post the minimal complete code to reproduce the problem.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Problem with delete operator

    Quote Originally Posted by felixb View Post
    it's just an example, I haven't posted the full source.
    In the full source, it isn't "char[5*sizeof(...)" but it is: "char[i*sizeof(...)]", so I do need dynamic allocation here.
    Is there some reason why a std::string or std::vector<char> won't work?

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem with delete operator

    Don't use "new" with "sizeof". new is not malloc. If you need 5 new objects, then new will create 5 of these objects, regardless of their size.

    for example:
    Code:
    int* integers = new int[5*sizeof(int)];
    Most likely, you will actually create 20 ints.

  10. #10
    Join Date
    Oct 2009
    Posts
    8

    Re: Problem with delete operator

    this is the full code:

    char** conv(int* size,char** a,char* b[])
    {
    char* line;
    int m=0,l=0;
    int curr,next;

    line=new char[m*sizeof(char)];
    a=new char*[l*sizeof(char)];

    for(int i=1;i<*size;i++)
    {
    while(i<*size && (strcmp(*(&b[i]),"end") != 0))
    {
    curr=strlen(line);
    next=strlen(b[i]);
    line=realloc_func(line,curr,curr+next);
    strcat(line,b[i]);
    i++;
    }
    line[strlen(line)+1]='\0';
    text=realloc_func(a,l);
    text[m]= new char[m+1];
    strcpy(a[l],line);
    l++;
    }

    *size=l;

    delete[] line;
    return a;
    }

    (realloc_func - is function that get some pointer to array and increase it, by creating bigger one and copying the old to the new)

    I get the error when I reach the "delete[] line" ...

  11. #11
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Problem with delete operator

    from what you posted here I'd say the problem is inside the realloc_func. What exactly does that do (in code)?

  12. #12
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Problem with delete operator

    Code:
    int m=0,l=0;
    int curr,next;
    
    line=new char[m*sizeof(char)];
    a=new char*[l*sizeof(char)];
    m and l are both zero.
    So you aren't creating memory, not even sure what the result of this is

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

    Re: Problem with delete operator

    Quote Originally Posted by felixb View Post
    this is the full code:
    You didn't answer a question that was asked of you in a previous post. Why not just use std::string (or std::vector<char>), instead of fooling around with new[], delete[], strcat(), char pointers, etc.?

    What exactly are you trying to accomplish with this code? In C++,, you have the std::string class that does everything that your code is trying to do.

    Regards,

    Paul McKenzie

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