|
-
Mar 1st, 2002, 01:04 PM
#1
Thread Starter
Monday Morning Lunatic
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;
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
-
Mar 1st, 2002, 01:21 PM
#2
transcendental analytic
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
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.
-
Mar 1st, 2002, 01:22 PM
#3
Member
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.
Code:
tempVars = Variables;
...
delete [] tempVars;
This is really frustrating..
-
Mar 1st, 2002, 01:24 PM
#4
transcendental analytic
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.
-
Mar 1st, 2002, 01:28 PM
#5
Member
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;
-
Mar 1st, 2002, 01:38 PM
#6
transcendental analytic
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..
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.
-
Mar 1st, 2002, 01:51 PM
#7
Member
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.
-
Mar 1st, 2002, 02:10 PM
#8
Thread Starter
Monday Morning Lunatic
Use vector, not apvector. The apvector is the random college version, vector is from the Standard C++ Library.
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
-
Mar 1st, 2002, 02:31 PM
#9
Member
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.
-
Mar 1st, 2002, 06:39 PM
#10
Fanatic Member
The standard classes have all the functions the AP classes have, and more. AP classes suck anyway.
Alcohol & calculus don't mix.
Never drink & derive.
-
Mar 1st, 2002, 06:41 PM
#11
Thread Starter
Monday Morning Lunatic
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
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
-
Mar 1st, 2002, 10:21 PM
#12
Member
point me to somewhere that explains the basics of the vector library and ill consider it.
-
Mar 2nd, 2002, 05:14 AM
#13
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. ...
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 2nd, 2002, 05:48 AM
#14
Thread Starter
Monday Morning Lunatic
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
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
|