well this is the code i got going:
Code:string *temparray = new string[max_size*2]; for(int i = 0; i < max_size; i++ { temparray[i] = sentence[i]; } delete[] sentence; sentence = temparray; delete[] temparray; max_size = max_size*2;
well this is the code i got going:
Code:string *temparray = new string[max_size*2]; for(int i = 0; i < max_size; i++ { temparray[i] = sentence[i]; } delete[] sentence; sentence = temparray; delete[] temparray; max_size = max_size*2;
the code is set to a function so it can be called more than once.
So, why delete[] temparray? You still need it, after all, since that's where everything is. Once you do sentence = temparray, the two are forever joined (that is, they currently point at the same piece of memory) and so deleting one will in fact delete both.
well the function is part of a class and sentence is a private data
Yes, tabstop's point is that:
is equivalent to:Code:delete[] sentence; sentence = temparray; delete[] temparray;
Code:delete[] sentence; sentence = temparray; delete[] sentence;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
but dont i have to deallocate all memory that has been allocated? or can i do that at the end of my program?
You are misunderstand though.
Example
Does that illustrate tabstop's point more clearly? A pointer is referring to data at a given address. Think of it as if I told you where the mall is. Then I get hit by a bus. The knowledge of where the mall exists still exists through you regardless of my state.Code:int *x, *y; x = new int[5]; y = x; delete[] y;
So as seen above, deleting y also deletes x.
You would have a final delete[] sentence in the destructor.but dont i have to deallocate all memory that has been allocated? or can i do that at the end of my program?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
well okay i figured out that problem but... i have another problem and that is now my sentence array contains no data because the function is in the private section of a class.
If i copy the array over instead of setting sentence to point to temparray will this work?
You can use private member functions from within any member function of the class, so I am not sure what you are getting at.i have another problem and that is now my sentence array contains no data because the function is in the private section of a class.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
when i try to cout << sentence[i]; in a loop to output each of the words for some reason it displays nothing now....
And where do you do that? Since sentence is private, it can _only_ be accessed by member functions. If you are in a member function, then post your code so we have some idea what you're talking about.
Attempting to access private data outside of a class definition would produce a compile error.