Thread: delete dynamic memory

  1. #1
    Registered User
    Join Date
    Jun 2002

    Unhappy delete dynamic memory


    I am trying to delete a character array in dynamic memory using Visual C++ .net, but I keep receiving error messages during runtime for the delete line of code.

    #include <iostream.h>
    #include <string.h>
    int main()
    	//write the county directory to the axl directory
    	char * newMunic = new char [strlen("Hello")];
    	strcpy(newMunic, "Hello");
    	//delete the dynamic memory after the traversal
    	delete [] newMunic;
    	return 0;
    }	//end main

    How do I properly delete the memory associated with newMunic?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Cape Town
    Try this :

    char * newMunic = new char [strlen("Hello") + 1 ];
    Account for the NULL terminator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Working with dynamic memory in C++
    By IndioDoido in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2007, 03:58 PM
  3. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. Dynamic Memory Allocation for fstream (binary)
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2001, 10:52 AM