Thread: Why doesn't this pointer assignment work?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Why doesn't this pointer assignment work?

    Code:
    jclass someclass;
    jclass* classPointer;
    
    //This makes the program crap out
    *classPointer = someClass;

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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
    Code:
    classPointer = &someClass;
    ? This make classPointer point at the someClass variable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Why doesn't this pointer assignment work?
    You haven't allocated memory for classPointer. You can either do this:
    Code:
    jclass someclass;
    jclass* classPointer;
    
    classPointer = &someclass;
    Or if you want to make a copy:
    Code:
    jclass someclass;
    jclass* classPointer;
    
    classPointer = new jclass;
    //This makes the program crap out
    *classPointer = someClass;

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by CornedBee View Post
    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
    Code:
    classPointer = &someClass;
    ? This make classPointer point at the someClass variable.
    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;
    
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sf.net/A_pointer_on_pointers
    You might want to take a look. It explains some concepts about pointers...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by Elysia View Post
    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
    
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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:
    Code:
    jclass c = getJClass();
    
    *mypointer = &(getJClass());
    or this:
    Code:
    jclass& c = getJClass();
    
    *mypointer = &c;
    or this:
    Code:
    jclass* c = getJClass();
    
    *mypointer = c;
    then it would work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. One pointer that does not work as expected
    By h3ro in forum C++ Programming
    Replies: 13
    Last Post: 07-16-2008, 07:58 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. values assignment to matrix pointer
    By ronenk in forum C Programming
    Replies: 8
    Last Post: 03-01-2005, 11:30 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM