Sign in to follow this  

dynamic array giving errors?

This topic is 4859 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
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
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 [b]new[/i] 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

This topic is 4859 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this