If you don't allocate the memory for it properly you won't be able to use it properly.
How about using a vector?Code:#include <vector>
// ...
vector<Variable> myvars;
Printable View
If you don't allocate the memory for it properly you won't be able to use it properly.
How about using a vector?Code:#include <vector>
// ...
vector<Variable> myvars;
classic fence post error, the 15'th element in an array is array[14], in other words you should have varCount-1, don't forget to delete[] the allocated memory afterwards.
use the vector if you aren't doing some kind of assignment of course
Well I figured out the problem.
But it crashes for another reason now...
I have these 2 lines of code, if I leave both, it crashes, if i comment out one or the other, it doesnt.
This is really frustrating..Code:tempVars = Variables;
...
delete [] tempVars;
post the whole thing
I was hoping I wouldn't have to, to save face :)
Code:// Create temp array
++varCount;
Variable* tempVars = new Variable[varCount];
// Copy content over
tempVars = Variables;
//memcpy(&tempVars, &Variables, sizeof(Variables));
// delete old array
delete [] Variables;
// Add new variable
tempVars[varCount - 1].type = nType;
tempVars[varCount - 1].name = new char[strlen(szName) + 1];
strcpy(tempVars[varCount - 1].name, szName);
tempVars[varCount - 1].value = new char[strlen(szValue) + 1];
strcpy(tempVars[varCount - 1].value, szValue);
// Recreate old array and copy content back
Variable* Variables = new Variable[varCount];
Variables = tempVars;
// Delete temp array
delete [] tempVars;
this doesn't copy back the arrays, if thats what you think:
Variables = tempVars;
you'd have to iterate trough the arrays.
I don't know for sure what creates your error though, unless you post all variable declarations and things..
Ah well, I decided to just give in and use apvector instead. Makes resizing much easier and is working perfect so far. Thanks for the help.
Use vector, not apvector. The apvector is the random college version, vector is from the Standard C++ Library.
You're telling me not to use it because it's not standard? The reason I'm using apvector is because it has a resize method which will save me some coding. I see no reason to use vectory instead of apvector though.
The standard classes have all the functions the AP classes have, and more. AP classes suck anyway.
The whole point of a vector anyway is that it resizes...Quote:
Originally posted by vcv
You're telling me not to use it because it's not standard? The reason I'm using apvector is because it has a resize method which will save me some coding. I see no reason to use vectory instead of apvector though.
...and yes, I'm saying don't use it because it's not standard ;)
point me to somewhere that explains the basics of the vector library and ill consider it.
Code:#include <vector>
using namespace std;
int main()
{
vector<int> myIntVector(10); // 10 elements initially
// iterate through the vector
vector<int>::iterator it = myIntVector.begin();
for(;it != myIntVector.end(); it++)
{
*it = rand();
}
myIntVector.insert(myIntVector.end(), 3, 5); // append 3 times 5 to the end
// access random elements
cout << myIntVector[4] << endl;
// etc. etc. ...
}