Code:jclass someclass;
jclass* classPointer;
//This makes the program crap out
*classPointer = someClass;
Printable View
Code:jclass someclass;
jclass* classPointer;
//This makes the program crap out
*classPointer = someClass;
classPointer doesn't point at anything. When you dereference it, you jump into the deepest hells of address space, to be consumed by the demons of undefined behaviour.
Perhaps you meant to do
? This make classPointer point at the someClass variable.Code:classPointer = &someClass;
>Why doesn't this pointer assignment work?
You haven't allocated memory for classPointer. You can either do this:
Or if you want to make a copy:Code:jclass someclass;
jclass* classPointer;
classPointer = &someclass;
Code:jclass someclass;
jclass* classPointer;
classPointer = new jclass;
//This makes the program crap out
*classPointer = someClass;
Yes, thanks but now I'm getting a weird thing. My pointer is in another class' method and it gets set (using the & operator like you specified) in a different method it calls. In that method, it properly sets it and is not null, but then once it exits, it's null again. How do I set it so it doesn't become null when it exits that method?
Code:void meth()
{
jclass* mypointer;
load( mypointer );
//now it's null!
if ( mypointer == NULL ) ...
}
void load(jclass* mypointer)
{
jclass c = getJClass();
//It's not null here but will be when it returns
mypointer = &c;
}
You have to pass the pointer by reference if you want the pointer to be updated. You're passing the pointer by value now so a copy of the pointer is made for the function. That copy is changed to point to a new location, but the original still points to nowhere (if it points to null you're lucky, it is uninitialized in that code).
You should consider starting from the beginning of Accelerated C++ to learn the language from the perspective of a C++ programmer and not a Java programmer. That book is accelerated enough that you'll probably get to where you're going faster by slowing down and reading it.
http://cpwiki.sf.net/A_pointer_on_pointers
You might want to take a look. It explains some concepts about pointers...
Thanks. I changed the method to take a pointer to a pointer and passed it by reference but when it comes back from the method it's still null! I am not getting this stuff at all. Please help!
Code:void meth()
{
jclass* mypointer;
load( &mypointer );
//Still null!
if ( mypointer == NULL ) ...
}
void load(jclass** mypointer)
{
jclass c = getJClass();
//It's not null here but will be when it returns
*mypointer = &c;
//If I do this: **mypointer = c;
//it'll also be null when it returns
}
No, it's not null. However, it points to an object that no longer exists, namely the local variable in load(). That's worse than being null.
First, it won't necessarily be null everywhere. It should be garbage.
Second, this time you created a copy of the return value of getJClass named c. The copy named c will go out of scope and be destroyed when the function ends, so mypointer will be pointing at an already deleted location (i.e. garbage).
If you did this:or this:Code:jclass c = getJClass();
*mypointer = &(getJClass());
or this:Code:jclass& c = getJClass();
*mypointer = &c;
then it would work.Code:jclass* c = getJClass();
*mypointer = c;