Thread: help with pointers

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    help with pointers

    Hi i am having some trouble with pointers:
    Code:
    char *input = NULL; /* The character array to put the input line in */
    
    getTheLine(input); /* discard first line */
    
    void CdistanceFilter::getTheLine(char *output)
    {
    	std::string inputString = ""; /*A string to put file lines*/
    	//char *input;
    
    	std::getline(inputFile,inputString); /* get third line */
    	
    	output = new char[inputString.length()]; /*get the length of the string*/
    	
    	memcpy(output, inputString.c_str(), inputString.length()); /*Copy the string removing the \0*/
    }
    NOTE: the first two lines are in a different function. They are not global

    The thing is that when the getTheLine function returns, the pointer input gets 0x000000 but just before the function ends, the pointer output was pointing to the correct location so what gives. Thanks
    Amish

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    output is a local variable of the function. It is destroyed when the function ends--leaving the address stored in input unchanged. Anything you do to the address stored in output has no effect on the address stored in input.

    You can use output to change the value where output is pointing to, e.g.:

    *output = "hello";

    Since input points to the same location, the value input points to will also change. However, the address stored in input will not change.

    When you call the function with input as the argument, the first thing that happens is the parameter variable output is created, and then a copy of input's address is stored in output.
    Last edited by 7stud; 03-06-2006 at 06:29 PM.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    there are three ways to do what you're tring to accomplish

    1. pointer to a pointer (most painful)
    Code:
    char *input = NULL;
    getTheLine(&input);
    void CdistanceFilter::getTheLine(char **output)
    {
    	// snip
    	*output = new char[inputString.length()]; /*get the length of the string*/
    }
    2. reference to a pointer (better, but still ugly)
    Code:
    char *input = NULL;
    getTheLine(input);
    void CdistanceFilter::getTheLine(char *&output)
    {
    	// snip
    	output = new char[inputString.length()]; /*get the length of the string*/
    }
    3. forget the the whole pointer thing and use a reference to a std::string (yay!!)
    Code:
    std::string input;
    getTheLine(input);
    void CdistanceFilter::getTheLine(std::string& output)
    {
    	// snip
    	output = "whatever the hell you want";
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    ...and maybe easier to understand but not as efficient as option 3:
    Code:
    string input = getTheLine();
    ....
    ....
    
    string CdistanceFilter::getTheLine()
    {
    	string inputString;
    	getline(inputFile, inputString);
    	
    	return inputString;
    }

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    The double pointer worked fine but when I tried to deallocate the memory using delete [] input outside of getTheLine, the program crashed. Any clues
    Amish

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by axr0284
    The double pointer worked fine but when I tried to deallocate the memory using delete [] input outside of getTheLine, the program crashed. Any clues
    Amish
    post code!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    here it is
    Code:
    int CdistanceFilter::initializeArrays(void)
    {
    	int errorCode = 0; /* This is the error code that is returned  */
    	
    	char *input = NULL; /* The character array to put the input line in */
    ...
    getTheLine(&input); /* discard first line */
    delete [] input;
    ...
    }
    
    void CdistanceFilter::getTheLine(char **output)
    {
    	std::string inputString = ""; /*A string to put file lines*/
    	//char *input;
    
    	std::getline(inputFile,inputString); /* get third line */
    	
    	*output = new char[inputString.length()]; /*get the length of the string*/
    	
    	memcpy(output, inputString.c_str(), inputString.length()); /*Copy the string removing the \0*/
    }
    I get an exception error when i try to delete it. Again thks for the help. I really appreciate it
    Amish

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > memcpy(output, inputString.c_str(), inputString.length());
    Should have been
    memcpy(*output, inputString.c_str(), inputString.length());

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Thanks everybody for your help, it works now. I must have been staring at the code for too long not to notice that last mistake
    Amish

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by axr0284
    Thanks everybody for your help, it works now. I must have been staring at the code for too long not to notice that last mistake
    Amish
    this is why I pointers to pointers (2 star programming) is Not A Good Thing(tm) and why I discouraged you from using it. You're lucky in that it was a simple bug and (relatively) easy to spot. With a string reference this problem would never have come up.
    Last edited by ChaosEngine; 03-07-2006 at 02:29 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM