okay this is all within the class
okay this is all within the class
Or you still haven't resolved the delete[] issue properly and each string's destructor has already destroyed the data.
Can you please post your entire code?
this function is declared in the private section of a class and i have a print function that is declared in the public class that consists of the following:Code:void extend() { string *temparray = new string[max_size*2]; for(int i = 0; i < max_size; i++) { temparray[i] = sentence[i]; } delete[] sentence; max_size = (max_size*2); sentence = new string[max_size*2]; for(int i = 0; i < max_size; i++) { sentence[i] = temparray[i]; } }
so for a driver program or soemthing i used a constructor to set the size of sentence to 3 then i asked for user input to put in strings and if the user enters more strings than the size of the array it increases the size of the array by running extend();Code:for(int i = 0; i < max_size; i++) { cout << sentence[i] << " "; }
the private data members are:
string *sentence;
int max_size;
Hold up bud, you are making two copies of your array. That is grossly unnecessary.
Example:
Code:void extend() { string *temparray = new string[max_size*2]; for(int i = 0; i < max_size; i++) { temparray[i] = sentence[i]; } delete[] sentence; max_size = (max_size*2); sentence = temparray; /* This is all very dubious including the fact you are also making an array that grows exponentially, instead of linearly. * * sentence = new string[max_size*2]; * for(int i = 0; i < max_size; i++) * { * sentence[i] = temparray[i]; * } */ }
dotz02x, this is the algorithm you are implementing:
1. Create a new array, twice the size, pointed to by temparray.
2. Copy from sentence to temparray.
3. Delete[] sentence.
4. Create a new array, twice the size, pointed to by sentence.
5. Copy from temparray to sentence.
You have a memory leak since you fail to delete[] temparray. Another problem is that max_size tracks the capacity of the array, but what variable tracks how many elements are actually in use?
Okay, maybe I should not be doing this, but here's an implementation of the algorithm I outlined earlier:
Code:void extend() { max_size += max_size; // Assume max_size is positive. string* temparray = new string[max_size]; // Create a new array twice the size. // Copy the old array to the new array. // Assume the member variable size keeps track of how many elements are in use. for (unsigned int i = 0; i < size; ++i) { temparray[i] = sentence[i]; } // Deallocate the old array. (Not strictly true, but anyway.) delete[] sentence; // Point the old array to the new array. sentence = temparray; }That's fine. In fact, std::vector's push_back() is required to double the size of the vector, so that insertion at the end happens in amortized constant time.Originally Posted by master5001
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Fixed.
And in regard to the exponential growth, I am more pointing out the fact that I think he meant for it to double not grow by powers of four. But who am I to say what he means. It would probably be best if it grew by a fixed size though, in my opinion (back to my whole linear point). If your buffer was theoretically 100MB and you call extend, it will jump to 400MB. Or 200MB if you use either of our code.
[Edit]Ah, I just noticed your comment laser.. I completely ignored them prior. Perhaps he isn't using a variable to track the used size of the array. Again if the OP posted their code in its entirety as I had asked, we'd know.[/Edit]
Last edited by master5001; 04-25-2008 at 01:26 PM.
No, that's wrong. size should be a member variable.Fixed.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The key word is "factor". If it grew linearly instead, then it would not amortize out, e.g., consider the worst case of increasing the capacity by 1 everytime an element was pushed to the back.Actually, it amortizes out no matter what factor you expand it by, although the constant of amortization becomes bigger.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
okay thank you i believe it is now fixed
all i have now is a double free error at the end of my program when it asks if the user wants to end the program and if he answers true then it does but i get a double free or corruption error and i will try to work this out but i think its caused possibly by the deconstructor
A factor and exponent are certainly different. Vectors grow by a set factor, his original code was growing by 4 to the nth power.
okay so i get a double free fault at the end of my program when i use the sentence.~Setence();
destructor in my program and the destructor contains the following code:
both of them are arrays that have been initialized in the original constructorsCode:Setence::~Sentence() { delete[] sentence; delete[] bad_words; }
And can you guarantee that bad_words and sentence don't end up pointing at the same thing? For that matter, can you guarantee that both point to anything valid at the end of the day?