delete dynamic memory

This is a discussion on delete dynamic memory within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to delete a character array in dynamic memory using Visual C++ .net, but I keep receiving ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Unhappy delete dynamic memory

    Hello,

    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.

    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
    Location
    Cape Town
    Posts
    788
    Try this :

    Code:
    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, 12: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, 11:10 AM
  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, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21