Basic Question about passing pointer by reference

This is a discussion on Basic Question about passing pointer by reference within the C++ Programming forums, part of the General Programming Boards category; how can I pass a pointer by reference? I want this function to duplicate the string. I know only a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Basic Question about passing pointer by reference

    how can I pass a pointer by reference? I want this function to duplicate the string. I know only a copy of the pointer is passed, but how do I pass the pointer by reference? I tried having the method accept char*&, but it doesnt enter the method at all (made sure with breakpoints) [beforehand it was just char*].
    Thanks in advanced

    Code:
    void strdup (char*& str)
    	{		
    		int len=strlen(str);		
    		char* strDup = new char[2*len+1];
    		for (int i=0;i<len;i++)
    		{
    			strDup[i]=str[i];
    			strDup[i+len]=str[i];
    		}
    		str= strDup;
    	}

    the whole code:


    Code:
    #include <iostream>
    using namespace std; 
    
    namespace myStr {
    	
    	int strlen (char* str)
    	{
    		int counter=0;
    		char* ch = &str[0];
    		while (*ch!=0)
    		{
    			ch++;
    			counter++;
    		}
    		return counter;
    	}
    
    	void strdup (char*& str)
    	{		
    		int len=strlen(str);		
    		char* strDup = new char[2*len+1];
    		for (int i=0;i<len;i++)
    		{
    			strDup[i]=str[i];
    			strDup[i+len]=str[i];
    		}
    		str= strDup;
    	}
    }
    
    using namespace myStr;
    
    int main () {	
    	char str1[] = "Hello";
    	cout << str1 << endl;	
    	cout << strlen(str1) << endl;
    	strdup(str1);
    	cout << str1 << endl;
    	return 0;
    }
    Last edited by Vall3y; 10-04-2009 at 04:13 PM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    There are two problems. First of all, you forgot to null terminate the string after the copy. Second, str1 is an array, not a pointer. And if it were, you would need to delete the old memory before reassigning it. And anyway, it's just a bad design. Provide the reference as a second parameter if anything and leave the original string as is. Or better yet, just return the new string from the function. Or if you want to go all out and do the right thing, return a RAII-compliant container, such as std::string. Saves you a lot of headaches, and much easier, too.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose this is not going to work. You are passing an array to the function, and expect the array variable to turn magically into a pointer to dynamically allocated block of chars.

    Code:
    int main () {	
    	char str1[] = "Hello";
            char* p = str1;
    	cout << str1 << endl;	
    	cout << strlen(str1) << endl;
    	strdup(p);
    	cout << p<< endl;
    	return 0;
    }
    Still it all looks rather problematic. You expect the pointer that is passed in not to point to dynamically allocated buffer (or you'll leak it), but the pointer that the user gets back does point to dynamically allocated memory.

    A better idea:

    Code:
    //returns pointer to dynamically allocated char array, containing twice the contents of source
    char* strDup(const char* source);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    4
    thanks for your comments

    edit: I read the requirement as having the given string changed, and not returning anything. I now understand this is not accepted? I must have understood it wrongly
    Last edited by Vall3y; 10-04-2009 at 05:49 PM.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As you have it, it is extremely bad. In goes a pointer to the stack, out comes a pointer to the heap. How, for example, would you imagine calling the same function twice without leaking memory.

    If you really want to modify the same pointer, make it a requirement that it points to dynamically allocated memory to begin with and handle memory accordingly.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,652
    The problem with the reference parameter approach is that you are essentially promising to manage the caller's memory, which generally just leads to very nasty bugs. By returning the string directly, you are basically telling the caller "I'll let you figure out what to do with this", which is an infinitely better idea altogether.
    Last edited by Sebastiani; 10-05-2009 at 01:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference to functions, that CAN be found
    By Carola in forum C++ Programming
    Replies: 8
    Last Post: 08-27-2009, 05:59 AM
  2. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  3. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 03:21 AM
  4. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

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