Thread: delete problems visual 2008

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Question delete problems visual 2008

    Hi guys,

    I'm hoping you can help me with my problem. I'm using Visual C++ 2008 and i'm having _BLOCK_TYPE_IS_VALID Debug Assertion Failed! problem when i try to delete this array:

    char * file = new char[255];
    file = "this is a filename";
    delete [] file;
    if i don't delete the array however, it would cause a memory leak.

    Anyone knows what would be the problem with this?

    Thank you.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is probably because some OTHER data is overwriting the end of it's allocation. Unfortunately, this makes it hard to figure out where it is, but whatever was allocated shortly before "filename" is suspect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    char * file = new char[255];
    This line allocates a block of 255 characters and makes file point to this block.

    file = "this is a filename";
    This line tells the compiler to statically allocate a block of memory holding the string "this is a filename", and make file point to this block.

    delete [] file;
    This line tries to delete the block of memory file points to. Which block is that?

    The solution is to use strcpy(). A better solution is to use std::string.

    Moving this to C++ because it has nothing at all to do with VC++ and everything with the code being just wrong. You won't find a proper compiler where it works.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Trying to delete a string literal... Hmm.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    ahh I see what is wrong... thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-28-2008, 02:54 AM
  2. Visual Studio Express 2008 problems.
    By Normac in forum C++ Programming
    Replies: 2
    Last Post: 08-08-2007, 10:41 AM
  3. Allegro Problems with Visual C++ .net 2003
    By LiNeAr in forum Game Programming
    Replies: 12
    Last Post: 10-16-2005, 06:51 PM
  4. MS Visual C++ 6 wont include vector.h in a header file
    By bardsley99 in forum C++ Programming
    Replies: 9
    Last Post: 11-06-2003, 12:05 PM
  5. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM