PDA

Click to See Complete Forum and Search --> : Dynamic array of a struct - error


parksie
Mar 1st, 2002, 12:04 PM
If you don't allocate the memory for it properly you won't be able to use it properly.

How about using a vector?#include <vector>

// ...

vector<Variable> myvars;

kedaman
Mar 1st, 2002, 12:21 PM
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

vcv
Mar 1st, 2002, 12:22 PM
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.

tempVars = Variables;
...
delete [] tempVars;

This is really frustrating..

kedaman
Mar 1st, 2002, 12:24 PM
post the whole thing

vcv
Mar 1st, 2002, 12:28 PM
I was hoping I wouldn't have to, to save face :)

// 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;

kedaman
Mar 1st, 2002, 12:38 PM
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..

vcv
Mar 1st, 2002, 12:51 PM
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.

parksie
Mar 1st, 2002, 01:10 PM
Use vector, not apvector. The apvector is the random college version, vector is from the Standard C++ Library.

vcv
Mar 1st, 2002, 01:31 PM
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.

Wynd
Mar 1st, 2002, 05:39 PM
The standard classes have all the functions the AP classes have, and more. AP classes suck anyway.

parksie
Mar 1st, 2002, 05:41 PM
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. The whole point of a vector anyway is that it resizes...


...and yes, I'm saying don't use it because it's not standard ;)

vcv
Mar 1st, 2002, 09:21 PM
point me to somewhere that explains the basics of the vector library and ill consider it.

CornedBee
Mar 2nd, 2002, 04:14 AM
#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. ...
}

parksie
Mar 2nd, 2002, 04:48 AM
www.cplusplus.com
www.dinkumware.com