Thread: modifiing pointer in other function

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

    modifiing pointer in other function

    hi,

    everytime i think i fully understand pointers it doesn't take a day for c++ to show me i'm not

    here's what I tried: main function decleares a pointer and the create-function should allocate memory and modify that pointer to point to that location.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void create(string* x)
    {
    	cout << "adr x before: " << x << endl; // 0xbfb4c2fc
    	x=new string("aaa"); 
    	cout << "adr x after: " << x << endl; // 0x804a008
    }
    
    int main()
    {
    	string s;
    	cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
    	create(&s);
    	cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
    	return 0;
    }
    I think I know why this can't work as wanted (because the given pointer-parameter can't be modified right?) I think it should work using the pointer as return, but let's assume the return of the create function is needed for error checking.
    The main question is: that is the way to make it work as wanted? do I have to pass a pointer to the pointer? but how? ...

    thank you for comments.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    string s - is some variable - you cannot change its address... only its value - you should pass it in the function by pointer or by reference to do it

    string *pStr - is pointer to string, you can modify its value...
    you can pass it also by pointer or by refeerence to modify it inside some function

    Code:
    void create(string** x)
    {
    	cout << "adr x before: " << *x << endl; //if it is not initialized - not a good idea to print it
    	*x=new string("aaa"); 
    	cout << "adr x after: " << *x << endl; // suppose new was successful
    }
    
    ...
    string* pStr = 0;
    create(&pStr);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    C++ has references for such problems
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void create(string *& x)
    {
    	cout << "adr x before: " << x << endl; // not initialized
    	x= new string("aaa"); 
    	cout << "adr x after: " <<  x << endl; 
    }
    
    int main()
    {
    	string *s = 0;          // a pointer to a string ( as in your description )
    	cout << "adr s bevore: " << s << endl; // was initialized to 0
    	create(s);
    	cout << "adr s after: " << s << " = " << *s << endl;
        delete s;   // dont forget about this
    	return 0;
    }
    Kurt
    EDIT: too late, but look at it as a variation of vart's solution
    Last edited by ZuK; 11-26-2006 at 07:34 AM.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by ZuK
    Code:
        delete s;   // dont forget about this
    }
    Thank you both! Isn't the destructor called automaticly while the variable *s is going out of scope with the "}" of the main function?

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yup. But the memory allocated will not be freed back to the system.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No. You have to delete anything that was allocated with new.
    Kurt
    EDIT: The pointer to the string will be destructed ( that's a nop ) but not the object it points to ( the string ).
    Last edited by ZuK; 11-26-2006 at 08:43 AM.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    so for
    Code:
    {
      ...
      string s;
      ...
    } //*
    the destructor is called at //*

    and for

    Code:
    {
      ...
      string* s = new string();
      ...
    }
    it is not and delete is required.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    right.

  9. #9
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    all the parameter is passed by value,look at the program below
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void create(string* x)
    {
    	string* _x = x;
    	cout << "adr x before: " << x << endl; // 0xbfb4c2fc
    	_x = new string("aaa"); 
    	cout << "adr x after: " << x << endl; // 0x804a008
    }
    
    int main()
    {
    	string s;
    	cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
    	create(&s);
    	cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
    	return 0;
    }
    this program is easy to understand, when the value of x assign to _x, x has no relation with the function create.
    your program is similar to it,and the compiler help you to do string* _x = x and _x = new string("aaa")
    remember, all the paramter is passed by value
    now , let's see another program
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void create(string** x)
    {
    	cout << "adr x before: " << x << endl; // 0xbfb4c2fc
    	*x = new string("aaa"); 
    	cout << "adr x after: " << x << endl; // 0x804a008
    }
    
    int main()
    {
    	string* s = NULL;
    	cout << "adr s bevore: " << &s << endl; // 0xbfb4c2fc
    	create(&s);
    	cout << "adr s after: " << &s << endl; // 0xbfb4c2fc
    	return 0;
    }
    this program work perfectly, why?let's modify the function as the compiler always help us 
    void create(string** x)
    {
            string** _x = x; 
            *_x = new string("aaa");
    }
    _x and x are point to the same area of the memory, so *_x is *x

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM