Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
Here is a piece of code
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:
 
char *got_str1()
{
   static char *test = "a test";
   return test;
}
will this OK?
static char *got_str2()
{
   char *test = "a test";
   return test;
}
 
will this OK?
 
[Edit Code-Tags]
Posted 23-Nov-12 19:28pm
Edited 23-Nov-12 19:39pm
Andy4112.6K
v2
Comments
Richard MacCutchan at 24-Nov-12 4:53am
   
The first two are OK, the third one could cause problems, and is not recommended.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
nv3 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Comments
Jochen Arndt at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 405
1 Maciej Los 285
2 Peter Leow 128
3 Kornfeld Eliyahu Peter 119
4 DamithSL 119


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid