Thread: delete Assertion Failure

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    delete Assertion Failure

    Hi, All.

    I'm trying to discover the ins and outs of the character type, and now I'm stumped on something I have no idea how to solve.

    Here's the code.

    Code:
    	char* k;
    	k = new char[30];
    	k = "dkdk";
    	delete [] k;
    Executing the above code creates a "debug assertion failure" in VC++ 2003 in line 4.

    Why can't I deallocate the variable k?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you can't delete it becase you tossed out the allocated memory when you reassigned the pointer to the constant string literal. The pointer now points to the memory location occupied by "dkdk" instead of the memory allocated by the new operator.

    use strcpy() to copy the string. the assignment operator = only works with std::string objects.
    Code:
    strcpy(k,"dkdk");

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. Never knew c++ would do something like that.

    So I tried a few other things.

    Code:
    	char* k = "dkdk";
    	k = new char[5];
    	k = "dkdk";
    	k = new char[30];
    	delete [] k;
    I checked the addresses of k each time, and it was changing line by line.

    So if you don't mind answering another question:
    What happens to each previous address of k if k is assigned a new address?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    it is tossed into the bit bucket never to be seen again. That is called a "memory leek" because there is no way to recover that memory until the program terminates. Do that often enough and your program will crash because it will eventually run out of memory.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Holy cow.

    Glad to have discovered that while I'm still a noob.

    Thanks.

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by cunnus88
    What happens to each previous address of k if k is assigned a new address?
    k's address never changes. Rather, k's value changes. Since k is a pointer, that value represents an address. On each line, you assign a different value to k. The old values are tossed out, and are lost forever. Since some of those value's represent memory locations that were dynamically allocated, all reference to that memory is lost, but the memory is still allocated - ie, a memory leak.
    Edit: Beaten. (Twice.)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Cactus_Hugger
    , a memory leak.
    Yes, that' what I meant -- a "leek" is a garden herb.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Ancient Dragon
    you can't delete it becase you tossed out the allocated memory when you reassigned the pointer to the constant string literal. The pointer now points to the memory location occupied by "dkdk" instead of the memory allocated by the new operator.

    use strcpy() to copy the string. the assignment operator = only works with std::string objects.
    Code:
    strcpy(k,"dkdk");
    Why do you say that? You can assign both the address of a char array to a char pointer and the address of a string literal to a char pointer:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	char* k;
    	k = new char[30];
    	delete [] k;
    
    	k = "dkdk";
    	cout<<k<<endl;
    
    	return 0;
    }
    cunnus88,

    The compiler takes a string literal(i.e. anything enclosed in double quotes) and slaps a '\0' character onto the end and stores it in memory somewhere and returns it's address, which is assigned to your pointer variable. The compiler also makes the string literal in memory a const, so you can't change it. You may wonder how the const value in memory can be assigned to your non-constant pointer: char* k. Normally, a non-const pointer allows you to change the value the pointer points to. But, that isn't the case with string literals, and if you try to use k to change the value of the string literal in memory, your program will crash at runtime. Here is an example:
    Code:
    char* k = "some text";
    k[0] = 'a';
    Compiles fine, crashes when you run it. The reason the compiler won't flag that as an error is for backwards compatibility for code that was written before the C++ standard made string literals constant. Therefore, it's good practice and may prevent nasty runtime errors if you declare your pointer variable const when you are going to assign it a string literal:
    Code:
    const char* k = "some text";
    That way if the code you write tries to change the string literal, the compiler will flag it as an error instead of letting it go, causing your program to experience a mysterious runtime crash.
    Code:
    const char* k = "some text";
    k[0] = 'a'; //error
    When the compiler notifies you of an error, you can correct it. Note how this is one way to write C++ code that uses the compiler to help you catch errors. If the errors are caught by the compiler, then you can fix them. Otherwise, you might not discover them, and when you or a client is running your program it will mysteriously crash.
    Last edited by 7stud; 11-12-2005 at 11:59 PM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe I did not make myself clear. The = operator only assigns the address of the string literal, it does not copy the string into the buffer that was previously allocated with the new operator. With std::string, however, the = operator will make a duplicate copy of the string literal and NOT just copy its address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 05:50 PM
  5. "delete []" in destructor causes assertion failure
    By TerranFury in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2001, 11:47 AM