Results 1 to 6 of 6

Thread: deleting objects

  1. #1

    Thread Starter
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551

    deleting objects

    Hello,

    I've declared the following class:
    Code:
    class person
    {
    public:
    	int age;
    	char *name;
    
    	person(char *p_name, int p_age);
    	~person() { 
    		delete[] name;
    	}
    };
    In my main program I've declared an array of persons. At the end of my program I'm deleting those objects as following:
    Code:
    for(i = 0; i < 10; i++) delete persons[i];
    Somehow I'm getting the following error:
    DAMAGE: after Normal block (#43) at 0x00431AF0.
    I'm getting the same error when I'm deleting the name of a person by
    Code:
    delete name;
    (without the [])

    Can someone tell me what the error means, how I can avoid it and how I can delete the names in the object destructor? (Maybe it isn't necessary at all, but it seems to me, since I'm using a pointer.)

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    are you sure name isn't a wild pointer? for each delete you should have a new.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Zaei
    Guest
    In the class constructor, set name to NULL. Then, in the destructor, check to see if name == NULL. If its not, delete it. you may also get the DAMAGE: warning if you attempt to write out of bounds of your allocation:
    Code:
    char* n = NULL;
    n = new char[10];
    n[10] = NULL;
    ...
    delete [] name;
    On the delete, you will get that warning. Check any string modifications that you do to make sure that you dont write out of bounds.

    Z.

  4. #4

    Thread Starter
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    The constructor is defined like this:
    Code:
    person::person(char *p_name, int p_age): age(p_age)
    {
    	int len;
    
    	len = strlen(p_name);
    	name = new char[len];
    	strcpy(name, p_name);
    }
    I think it's allright.
    Do I actually have to delete strings in the destructor of a class, or are they cleared automatically?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    a) are the persons allocated dynamically (with new)

    b) why don't you use the string class?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    c) why don't you use vector or list
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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