Advertisement

Problems with Pointers...

Started by May 07, 2002 12:56 AM
7 comments, last by Punx 22 years, 4 months ago
One of my classes (Class1) has an array of pointers filled with object of another class (Class2). It looks like this... //in Class1 blah ClASS2 *pTest[100]; Now whenever i try to access one of this arrays attributes (name for example) other than a method, the program crashes. I'm trying to access it like this. Class1Instance.Class2[0]->name; Can anyone help me? EDIT: oops HealItems is supposed to be Class2. [edited by - Punx on May 7, 2002 2:23:18 AM]
~punx
If you got an access violation, your array probably hasn''t been allocated (you do allocate the HealItems array, don''t you?)

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Advertisement
Like Oluseyi said, you have to allocate the array.
Class2 *pTest[100];  
declares an array of pointers, but does not allocate 100 valid Class2 objects (the pointers could point to anywhere). I suggest doing one of two things in the constructor:

  Class2 *pTest[100];// NULL them so that at least you can't mess around with memory that doesn't belong to youfor(int i = 0; i < 100; i++) pTest[i] = NULL; // OR create the desired objects with the new operatorfor(int i = 0; i < 100; i++) pTest[i] = new Class2(/* ... */);     

Hope those thoughts help.

[edited by - Miserable on May 7, 2002 10:18:10 AM]
Why not:

Class2 *pTest[100] = {0};

?

[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
What''s in Class2??

Like the others have said, all you''ve got is an array of 100 pointers. These pointers don''t actually point to anything, they "ready" if you like to recieve an address of a Class2.

I''m reall a C programmer so I won''t pretend to go into detail on C++ classes when I don''t know a whole hell of a lot about them. However what you''re essentially doing is no different than this eg:

---------------------------
typedef struct
{
int blah;
unsigned char blah2;
} MyStruct;

MyStruct *ptest[100];
---------------------------

all that does is say "make me an array that can hold the ADDRESS''s of 100 MyStruct''s"

you''de then have to go and allocate them like :

ptest[0] = (MyStruct *)malloc(sizeof(MyStruct));
ptest[1] = (MyStruct *)malloc(sizeof(MyStruct));
...etc

Any help?



GCoder
GCoder
You said when you call a method from CLASS2 it does NOT crash, but when you try and access a variable it DOES crash. This is a definite allocation problem. The reason you can call a method is because it''s a __thiscall() - taken from the class vtable[]. In other words, the instance pointer is passed to the method (regardless if it''s a valid pointer or not! When you access member data directly, you must dereference the instance pointer.

I imagine you never dereference this inside your method, which is why - most probably - the call never crashed.
People fear what they don''t understand, hate what they can''t conquer!
Advertisement
Maybe I should restate my question...

How would I initialize an array of pointers of a class type as a class member and then access the array''s members from main()?
~punx
quote: Original post by Punx
How would I initialize an array of pointers of a class type as a class member...

class SomeClass{public:  SomeClass( int n );  ~SomeClass();  SomeClass2 **PtrArray;}; SomeClass::SomeClass( int n ){  PtrArray = new SomeClass2 *[ n ]; // allocate n pointers  for( int m = 0; m < n; ++m )    PtrArray[ m ] = 0;} SomeClass::~SomeClass(){  delete [] PtrArray;} 


quote:
...and then access the array''s members from main?

int main( void ){  SomeClass * pSomeClass = new SomeClass( 6 );   // use the array of pointer by allocating objects  for( int n = 0; n < 6; ++n )    pSomeClass->PtrArray[ n ] = new SomeClass2;   // discard the array of pointers (deallocate objects)  for( int n = 0; n < 6; ++n )    delete pSomeClass->PtrArray[ n ];   return 0;} 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
OK thank you very much. I understand what pointers are it''s just putting them into place which confuses me. Thank you.
~punx

This topic is closed to new replies.

Advertisement