Thread: Having trouble using the keyword new inside of a function when passing a pointer in.

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    Having trouble using the keyword new inside of a function when passing a pointer in.

    I am having trouble using the keyword new inside of a function when passing a pointer in to it. Please tell me what I am doing wrong.

    Code:
     
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct person_details{
        char name[100];
        char phone[100];
    };
    
    void getDetails( person_details * pd ){
    
        pd = new person_details[1];
        strcpy( pd[ 0 ].name, "John Doe" );		
    }
    
    int main(int argc, char *argv[])
    {
    
        person_details  *pDetails = 0;
        getDetails( pDetails );
    
        cout << "pDetails[ 0 ].name " << pDetails[ 0 ].name << endl;
    
        delete pDetails;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    
    }
    Last edited by klanglie; 06-15-2005 at 01:25 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Pass the pointer by reference.
    Code:
    void getDetails( person_details * &pd ){
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Other alternatives...

    #2 - Pass a pointer-to-a-pointer:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct person_details{
        char name[100];
        char phone[100];
    };
    
    void getDetails( person_details ** pd ){
    
        *pd = new person_details;
        strcpy( (*pd)->name, "John Doe" );		
    }
    
    int main(int argc, char *argv[])
    {
    
        person_details  *pDetails = 0;
        getDetails( &pDetails );
    
        cout << "pDetails->name " << pDetails->name << endl;
    
        delete pDetails;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    
    }
    #3 - Return a pointer from the getDetails function:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    struct person_details{
        char name[100];
        char phone[100];
    };
    
    person_details * getDetails()
    {
        person_details * pd = new person_details;
        strcpy( pd->name, "John Doe" );
        return pd;
    }
    
    int main(int argc, char *argv[])
    {
    
        person_details  *pDetails = getDetails();
    
        cout << "pDetails->name " << pDetails->name << endl;
    
        delete pDetails;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    int main(int argc, char *argv[])
    1)If you aren't going to enter command line arguments(or if you don't know what that means), use this format instead:
    Code:
    int main()
    {
    
    	return 0;
    }
    2) You might want to describe what you are trying to do.

    a) What's the point of creating an array of length 1?

    b) It seems to me, inside a function you would use the operator 'new' to pass a pointer out of the function when the pointer variable was declared inside the function. In other words, 'new' creates a pointer with no scope limitations, and the only thing that will make the pointer go out of scope is delete. For instance,
    Code:
    #include <iostream>
    using namespace std;
    
    int* myFunc(int num)
    {
    	int* ptr = new int(num);
    	return ptr;
    }
    
    int main ()
    {
       int* myPtr = 0;
    
       myPtr = myFunc(10);
       cout<<*myPtr<<endl;
    
       delete myPtr;
    
       return 0;
    }
    (oops...I see now that is the same as hk_mp5kpdw's example #3)

    The problem with your code is that despite what you may have read, pointers are "passed by value"(in fact everything is passed by value). When you pass a pointer to a function, a copy of the pointer is made for the function, and the function uses the copy. You are seeing the consequences of that fact inside your function. Inside your function, you are using 'new' to grab some memory, and you are assigning the address of that memory to the copy of the pointer. When the function ends, the copy of the pointer is destroyed, and the original pointer in main() is left unaffected.

    The reason books will say that pointers allow "passing by reference" is that the object the pointer points to is "passed by reference", i.e. the object the pointer points to is not copied for the function. However, the pointer you pass to the function is copied for the function. Since a copy of a pointer still points to the same object as the original pointer, the function can use the copy of the pointer to change the original object. However, in your function, you are not trying to change the original object --you are trying to change the original pointer, which is not going to work.

    There is a solution if you want to stick with what you are doing. Just like with an object, you can pass a pointer by reference if you want. If you treat your pointer as an "object", how would you pass the "object" by reference? You would either use a pointer to the "object" or a reference to the "object". If you use a pointer, the pointer will be copied for the function just like before, but a copy of the pointer still points to the original "object", so you can use the copy of the pointer to change the original "object".
    Last edited by 7stud; 06-15-2005 at 03:37 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    2
    Thanks for the quick response, guys! This information ought to help me out a great deal. I am definitely finding out that using pointers and pointers to pointers is a little confusing, but you all have helped me to clear up some issues. I will probably do what hk_mp5kpdw and 7stud suggested and return a new object instead of passing in a pointer. Also, in my original code snippet, I used delete pDetails instead of delete [] pDetails. Since I will be dynamically creating an array of person_details bigger than one dimension, I should probably be using the later. Again, thanks a lot. cprogramming.com rocks!!!
    Last edited by klanglie; 06-15-2005 at 08:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  3. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  4. pointer assignment inside a function ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 05-04-2005, 11:30 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM