Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Array Of Pointers

Started by Degra Jul 2, 2006 at 10:01 AM 5 replies 2.4k views
Original Post
Degra
Degra
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
darookie
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.
Degra
Degra
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
darookie
darookie
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 tripletsfacet->vertices.resize( spaces );// from here on, facet->vertices can be used just like an array of triplet pointers


HTH,
Pat.
rileyriley
rileyriley
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.
--Riley
Degra
Degra
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.