|
-
Aug 12th, 2001, 07:20 AM
#1
Thread Starter
Addicted Member
deconstructing with a vector
I have created a vector of objects that I am constantly changing, but I want to have a deconstructor to deallocate a string in each of the objects when I am done using the vector. I wrote a deconstructor but I noticed that it is getting called all over the place- even when I am not done using the memory. Is there something special about vectors that can account for this, and if so how do I deallocate the memory used in the string for each object in a vector?
Any suggestions would be great!
-
Aug 12th, 2001, 08:16 AM
#2
Monday Morning Lunatic
The destructor will be called every time the object is destroyed, but the vector will be doing its own internal memory management, causing your objects to be constructed/destructed more than you expect.
If you have a copy-constructor (operator=) then you can make sure that it's all kept safe
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 12th, 2001, 08:31 AM
#3
Member
BTW, destructors never take arguments and follow the format void ~classname().
-
Aug 12th, 2001, 08:33 AM
#4
Monday Morning Lunatic
They can't return values either.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 12th, 2001, 09:57 AM
#5
Thread Starter
Addicted Member
hummm
some of that makes sense to me, I can understand how the vector controls its own state as it needs to recopy itself each time its size changes, but are you saying then that I shouldn't worry about deallocating at all? What about when I am finished with the vector, how will I deallocate- or do I need to?
-
Aug 12th, 2001, 10:57 AM
#6
Member
Theoretically, in most cases, unless you are using pointers, you should never have to make a destructor.
-
Aug 12th, 2001, 10:58 AM
#7
Member
Originally posted by parksie
They can't return values either.
That's why I said "void".
-
Aug 12th, 2001, 12:14 PM
#8
Monday Morning Lunatic
No, it doesn't even have a return type
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 12th, 2001, 12:22 PM
#9
PowerPoster
Deconstructors have ~ to show that that they dont have any return type (yes Parskie, Not even void )
-
Aug 12th, 2001, 12:28 PM
#10
Monday Morning Lunatic
Quibbling again 
Destructors have ~ because that's how you tell it's a destructor and not a constructor. Destructors take no arguments whatsoever (other than the implicit this), and return no values.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 12th, 2001, 01:12 PM
#11
Member
Originally posted by abdul
Deconstructors have ~ to show that that they dont have any return type (yes Parskie, Not even void )
Oh, you're right. I was saying void meaning it didn't return anything, but then void is kinda different.
-
Aug 12th, 2001, 01:53 PM
#12
PowerPoster
Is there anything else than Deconstructor that does not return anything - not even void ?
-
Aug 12th, 2001, 03:53 PM
#13
Monday Morning Lunatic
Other than the destructor, only constructors cannot return values.
Well, actually some functions cannot return values because they get interrupted before they reach their exit code (or none in the case of naked functions, which I haven't found much of a use for yet).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 12th, 2001, 04:02 PM
#14
Thread Starter
Addicted Member
hummm
the objects in my vector do pointer elements in them, so it seems to me that they would need to be deallocated when they are finished being used... correct?
-
Aug 12th, 2001, 05:08 PM
#15
transcendental analytic
Yes, in the destructor you deallocate all memory allocated in the class. Note you don't deallocate objects that was passed to it (that's undermining object orientation) On the pointers holding them you do
delete varname
or
delete[] varname
if varname is a pointer to an array.
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.
-
Aug 12th, 2001, 05:08 PM
#16
Member
You might want to use the apvector class. It is templated and works fine for me. (I can't believe I'm actually recommending that AP crap )
http://www.collegeboard.org/ap/compu...l/classes.html
-
Aug 12th, 2001, 06:25 PM
#17
Thread Starter
Addicted Member
k but....
THis all being said I am brought back to my original question then... If I have a vector of objects that have as a component say a *char I need to deallocate the memory used by this *char if I use the new operation to allocate memory. But, as the vector is constantly allocating and deallocating to increase its size, how do I perform a final deallocation once I am finished using the vector?
-
Aug 12th, 2001, 07:56 PM
#18
transcendental analytic
Is the vector on the heap?
if your vector was newed from the beginning you can deallocate it with delete. If not it will be deallocated when it runs out of scope. The vector will of course delete it's objects before it deletes itself, that is handled in the vectors destructor.
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.
-
Aug 12th, 2001, 08:32 PM
#19
Dazed Member
Now i know why i program in Java.
-
Aug 12th, 2001, 09:40 PM
#20
transcendental analytic
Hey! It's not as bad as it looks Once you get how it is and the benefits of them you can screw Java and be happy
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.
-
Aug 13th, 2001, 03:58 AM
#21
Monday Morning Lunatic
Re: k but....
Originally posted by bob323
THis all being said I am brought back to my original question then... If I have a vector of objects that have as a component say a *char I need to deallocate the memory used by this *char if I use the new operation to allocate memory. But, as the vector is constantly allocating and deallocating to increase its size, how do I perform a final deallocation once I am finished using the vector?
Code:
for(i = 0; i < 5; i++) {
char *ptr = new char[10 * i];
myvector.push_back(ptr);
}
// Use the vector, which will handle allocating the variables
// holding the pointers, not your data itself
for(i = 0; i < 5; i++) {
delete[] myvector[i];
}
myvector.clear(); // Remove invalid pointers
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 13th, 2001, 05:13 AM
#22
transcendental analytic
Hmm, if that's how it is, you should use a string class that knows how to deallocate itself. If you like lowlevel stuff it's of course ok but otherways it's against object orientation in terms of encapsulation.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|