Results 1 to 14 of 14

Thread: Dynamic array of a struct - error

  1. #1

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    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..

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    post the whole thing
    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.

  5. #5
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    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;

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  7. #7
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    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.

  8. #8

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    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.

  10. #10
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  11. #11

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  12. #12
    Member
    Join Date
    Dec 2001
    Location
    ny
    Posts
    38
    point me to somewhere that explains the basics of the vector library and ill consider it.

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  14. #14

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width