Jump to content
  • Advertisement
Sign in to follow this  
Degra

C++ Array Of Pointers

This topic is 4488 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

Hi, quick n00b question here: Is there a way to create an array of pointers to a class without using malloc? At the moment I've got: facet->vertices = (Triplet *)malloc(sizeof(Triplet *) * spaces); but sometimes this doesn't work and just returns 0; Can anyone help? Degra

Share this post


Link to post
Share on other sites
Advertisement
Try
facet->vertices = new Triplet[spaces];
malloc returns 0 if you are out of memory, while "new" - amongst other nice things such as returning the correct, calling constructors, etc. - in general throws an exception.
In your case I'd take a close look at the actual values of "spaces". If you're in debug mode, some compilers use magic values such as 0xcccccccc to indicate uninitialised variables. In case "spaces" is unsigned, this would be some huge amount of memory and result in an allocation error. The same happens if "spaces" gets assigned a negative value for some reason. Due to 2-complements notation of negative integers, an usnigned value becomes incredibly huge when assigned a negative number.

HTH,
Pat.

Share this post


Link to post
Share on other sites
Quote:
Original post by darookie
Try
facet->vertices = new Triplet[spaces];
malloc returns 0 if you are out of memory, while "new" - amongst other nice things such as returning the correct, calling constructors, etc. - in general throws an exception.
In your case I'd take a close look at the actual values of "spaces". If you're in debug mode, some compilers use magic values such as 0xcccccccc to indicate uninitialised variables. In case "spaces" is unsigned, this would be some huge amount of memory and result in an allocation error. The same happens if "spaces" gets assigned a negative value for some reason. Due to 2-complements notation of negative integers, an usnigned value becomes incredibly huge when assigned a negative number.

HTH,
Pat.


I've checked 'spaces', it is definitely signed and when malloc is called it has a value of 4.

I would do it by 'new Triplet[spaces]', but I do not want to create new Triplets. I have already got the Triplet classes set up as they are used by more than one facet's vertices array (if that makes sense). I just need an array of pointers that I can manually set to point to pre-existing Triplets.
I should have mentioned that in the OP.

Degra

Share this post


Link to post
Share on other sites
Just use a std::vector then:

typedef Triplet * TripletPtr;
typedef std::vector<TripletPtr> TripletList;

...

// "vertices" is of type "TripletList" - make sure there is enough room for the triplets
facet->vertices.resize( spaces );
// from here on, facet->vertices can be used just like an array of triplet pointers



HTH,
Pat.

Share this post


Link to post
Share on other sites
Triplet * * pointerArray = new Triplet * [spaces];

may also do what you're looking for.

e.g.:

pointerArray[0] = new Triplet();
pointerArray[1] = existingPointerToTriplet;
pointerArray[2] = &existingTriplet;

etc.


don't forget to call delete[] pointerArray as well as delete on the individual elements.

Share this post


Link to post
Share on other sites
I've just tried both methods, and immediately after setting a pointer in the array (by: facet->vertices = &vertices[j];),
the triplet's variables have changed. When hovering over 'facet->vertices' VS reports the values to be '3.674e-040#DEN', and when hovering over 'vertices[j]' theyre reported to be '1'.
Any ideas?

Degra

Edit: Never mind, just ignore this post. Both suggested methods work fine [smile]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!