Functions and pointers.

This is a discussion on Functions and pointers. within the C++ Programming forums, part of the General Programming Boards category; I'm not sure if the following piece of code should work ok in all conditions so if someone can check ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Functions and pointers.

    I'm not sure if the following piece of code should work ok in all conditions so if someone can check the code and see if its right I would appreciate it very much.

    Code:
    float* returnFloatValue(int block, char *key)
    {
    	char *tmp = block_value(block, key);
    	float *tmpFloat;
    
    	if (strlen(tmp))
    	{
    		tmpFloat = atof(tmp);
    		return &tmpFloat;
    	}
    
    	return NULL;
    }
    PS: If you wonder why I ask, its because the only rule I remember for that kind of function is that you should never return a pointer to a local variable.

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Hulag
    I'm not sure if the following piece of code should work ok in all conditions so if someone can check the code and see if its right I would appreciate it very much.

    Code:
    float* returnFloatValue(int block, char *key)
    {
    	char *tmp = block_value(block, key);
    	float *tmpFloat;
    
    	if (strlen(tmp))
    	{
    		tmpFloat = atof(tmp);
    		return &tmpFloat;
    	}
    
    	return NULL;
    }
    PS: If you wonder why I ask, its because the only rule I remember for that kind of function is that you should never return a pointer to a local variable.
    function atof() returns double, not a pointer to float!!!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    So how should my function look like to work right?

    Here is my attempt:
    Code:
    float* returnFloatValue(int block, char *key)
    {
    	char *tmp = block_value(block, key);
    	float *tmpFloat = new float;
    
    	if (strlen(tmp))
    	{
    		*tmpFloat = (float) atof(tmp);
    		return tmpFloat;
    	}
    
    	return NULL;
    }
    Is it correct?
    Last edited by Hulag; 03-28-2005 at 09:06 AM.

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    No,
    I'm not sure what are you trying to do but you must know this:
    1. atof returns double and that means that variable on the left side of = must be double, for instance
    Code:
    double x = atof(...);
    2. You shouldn't return pointer to a local object, it's a disaster. Return variable by value.
    In that case function prototype should look like this:
    Code:
    double foo(...);
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by Micko
    1. atof returns double and that means that variable on the left side of = must be double, for instance
    Code:
    double x = atof(...);
    I don't need the extra presition given by atof so thats why I do the cast to float in my lastest attempt.

    Quote Originally Posted by Micko
    2. You shouldn't return pointer to a local object, it's a disaster. Return variable by value.
    In that case function prototype should look like this:
    Code:
    double foo(...);
    That's why I created tmpFloat, I allocate the momory for that variable and I will get rid of it at the end of the program to avoid a memory leak.

    Can you see any error in my lastest attempt?

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Sorry, I didn't see that you allocated memory on the heap. It's seems OK now. You allocate memory store value and then transfer ownership to caller. Now caller responsibility is to deallocate memory properly.
    I' d seriously consider other alternatives, but it's up to you!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    It's global variables that you shouldn't return pointers to, not local...

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by samGwilliam
    It's global variables that you shouldn't return pointers to, not local...
    From Bjarne Stroustrup's The C++ Programming Lenguage book:
    Each time a function is called, a new copy of its arguments and local (automatic) variables is created. The store is reused after the function returns, so a pointer to a local variable should never be returned. The contents of the location pointer to will change unpredictably:
    Code:
    int* fp() { int local = 1; /* ... */ return &local; } // bad
    This error is less common than the equivalent error using references:
    Code:
    int& fr() { int local = 1; /* ... */ return local; } // bad
    Fortunately, a compiler can easily warn about returning references to local variables.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    No... Returning pointers to global variables is fine, returning pointers to variables with local scope is a disaster because oncce the variable goes out of scope, you lose the memory for it, so your pointer points to memory you no longer have a right to access.

    As to the new code, note what happens in strlen(tmp) is 0. You never go into that block of code, and just return NULL. But, you have not deallocated the memory for the float you just created. Try initializing the pointer to NULL, and then creating a new float if the condition evaluates true.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Oh yeah, I got it wrong. I read somewhere though that you shouldn't return global variables....

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    well you don't have to return global variables. they're global!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

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