Jump to content
  • Advertisement
Sign in to follow this  
zoser

class pointer to class problem

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

i have 2 clases; class A{ private: class B *target; }*actual; class B{ private: public: int x; }; when i use actual->target->x; the program crash; any idea?

Share this post


Link to post
Share on other sites
Advertisement
Have you allocated memory for *actual and *target?

(Or make it point to valid data)?

If not, thats most likely the reason.

Share this post


Link to post
Share on other sites
yea points to a valid data

the program crash when i run the .exe outside devc++

when i run directly when i compile (compile and run)

works perfectly sometimes...

ist very weird when i put a system("pause"); in the begining works perfectly

without it program cash

Share this post


Link to post
Share on other sites
If adding/subtracting random pieces of code affects the runtime that suggests that there is some sort of memory corruption going on. Double and triple check that all the pointers are being pointed at the correct place and that you aren't accessing memory that has been freed etc.

If the code isn't huge stepping through with a debugger is probably a good idea.

Share this post


Link to post
Share on other sites
In the given code, you never create any objects, only pointers to objects. These pointers don't point to a valid object (since there is none anyway), which causes trouble.

Rewrite:

class A
{
public:
// In our constructor, create a new B instance on the heap memory,
// using the 'new' keyword
A()
{
target = new B();
}

// Everything that we 'new' must be 'deleted', or we'll get memory leaks
~A()
{
delete target;
}

private:
B* target;
}

// Class B doesn't have a constructor - so the compiler will create a default one
// for us. In this case, nothing special needs to happen so that's ok.
class B
{
public:
int x;
}

int main()
{
// Here, we create an instance of A on the heap memory - now, actual
// points to a valid A object, and since A's constructor creates a B
// object, we can safely use actual->target as well.
A* actual = new A();
actual->target->x = 5;

// Don't forget to delete actual!
delete actual;

return 0;
}


It's important that you understand that there's two things at work here: first, you're using pointers, things that aren't objects themselves but point to objects instead. Second, in my example, we're creating objects on the heap, rather than on the stack. Objects that you create on the stack are removed as soon as they go out of scope, e.g. when the function in which they were created ends. Objects created on the heap are your responsibility: you must delete them yourself, unless you're happy with memory leaks.

Since manual memory management gets tedious fast, and is error-prone, you'd better not use pointers when you don't need them.

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!