Jump to content
  • Advertisement
Sign in to follow this  
Tigra7

dynamic array giving errors?

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Better expained with code
RECT *Squares;
Squares = new RECT[1];

Squares[1].left = 0;

Won't work.
RECT Square[1];

Square[1].left = 0;

Will work... Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
arrays are zero based:


int myArray[3];

//first element
myArray[0];

//second element
myArray[1];

//third element
myArray[2];

//this line will crash
myArray[3];





your second block of code shouldn't work, but if it does you're not actually reading the element in the array, you're reading one element beyond the end of your array.

-me

Share this post


Link to post
Share on other sites

Assuming that's C/C++ code, arrays are always zero-based. You would want to use:


RECT *Squares;
Squares = new RECT[1];

Squares[0].left = 0;

Share this post


Link to post
Share on other sites
I'm still getting execution errors. The declaration and the assigning of 0 are in two different functions. Could that be a problem?

Share this post


Link to post
Share on other sites
If they are in two different functions, then yes, you may have a problem, as the functions might be making those variables locals, as in they only work inside that function. Try putting the declaration outside the function, near the top outside functions with say your declares or includes etc. Then I believe you have to do the assign 0 inside a function, it cannot be outside, but try either.

Share this post


Link to post
Share on other sites
Is RECT a type of your own design?
Does it have a default constructor?
If not, then the problem will probably be caused by this, since a call to new for arrays of objects requires that an appropriate default constructor exists, since the objects must be creatable without input parameters.

If the above is not the problem then does RECT also have an appropriate operator[] function that returns a reference to RECT object?

Finally, if it's none of these, test the following code snippet:

RECT *Squares;
Squares = new RECT[1];
(*Squares)[0].left = 0;

Does this work?

Cheers,

Timkin

Share this post


Link to post
Share on other sites
My problem is that I have a class that contains the RECT, and I can assign values to each element in one member function but not in any other.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!