modifiing pointer in other function

This is a discussion on modifiing pointer in other function within the C++ Programming forums, part of the General Programming Boards category; hi, everytime i think i fully understand pointers it doesn't take a day for c++ to show me i'm not ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    ZuK
    ZuK is offline
    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
    Portugal
    Posts
    7,581
    Yup. But the memory allocated will not be freed back to the system.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    ZuK
    ZuK is offline
    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
    ZuK
    ZuK is offline
    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, 01: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, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21