Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is a piece of code
C++
FILE *got_file(char *str)
{
   FILE *fp = fopen(str, "r");
   return fp;
}

will this be OK? the fp is a pointer points to FILE struct which stores in stack, I think. Any problems here?

Another one:

C++
char *got_str1()
{
   static char *test = "a test";
   return test;
}

will this OK?
C++
static char *got_str2()
{
   char *test = "a test";
   return test;
}


will this OK?

[Edit Code-Tags]
Posted
Updated 23-Nov-12 19:39pm
v2
Comments
Richard MacCutchan 24-Nov-12 4:53am    
The first two are OK, the third one could cause problems, and is not recommended.

Quote:
FILE *got_file(char *str)
{
FILE *fp = fopen(str, "r");
return fp;
}
will this be OK? the fp is a pointer points to FILE struct which stores in stack, I think. Any problems here?
This is OK. The FILE struct is NOT (a temporary) on the stack, its pointer is (but the pointer value, that is the address is copied as return value).

Quote:
char *got_str1()
{
static char *test = "a test";
return test;
}

Yes, it is OK, static variables are not temporaries.


Quote:
static char *got_str2()
{
char *test = "a test";
return test;
}
will this OK?

Ugly as it stands (I won't use it, if you ask me), it should be OK too. Because string literals are not temporaries.
 
Share this answer
 
Comments
nv3 24-Nov-12 6:22am    
Fully agree. A "const char*" return would have made the third one clear as well.

To the questioner: The "static char *got_str2()" does not make return type static, but declares the function as being of file scope, thus only visible in its own source file. Here the keyword static takes a different meaning than in a variable declaration.
Are these ok?
Well, no, they aren't, and will probably cause problems.

The hassle is that they are all stack based variables, as you have said. So when the method they are declared in ends, they go out of scope. Worse, the stack space is reclaimed, and will be used by the next function you call. These are called "dangling pointers" and wiki explains them better than I would: http://en.wikipedia.org/wiki/Dangling_pointer[^]

It isn't a good idea to do this - it stores up problems for later when they are really difficult to spot!
 
Share this answer
 
Comments
Jochen Arndt 24-Nov-12 3:34am    
You should rethink your answer. The first example is OK. The others would be OK when returning const char *.
OriginalGriff 24-Nov-12 3:42am    
Depends on the compiler - got_str1 should be ok in all cases since it returns a static value (which is not stack based) but it presents other problems, namely that it is re-used each time.
got_str2 will on some compilers return a copy of the constant string on the stack.
I didn't want to complicate the answer by bringing these up when the OP is clearing struggling with the basics. :laugh:
Jochen Arndt 24-Nov-12 3:57am    
You are right regarding the strings (using const is just a hint to resolve these issues). But the main question is about the FILE * return value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900