what type of string returns are allowed

E

Eric Moors

If have a question with respect to the code below:

#################################################################
#include <stdio.h>

char *ret_ok(void)
{
static char str[] = "explicit static";

return str;
}

char *ret_maybe(void)
{
return "implicit static";
}

char *ret_fails(void)
{
char str[] = "out-of-scope";

return str;
}

int main(void)
{
fprintf(stderr, "%s\n", ret_ok());
fprintf(stderr, "%s\n", ret_maybe());
fprintf(stderr, "%s\n", ret_fails());
return 0;
}
#################################################################

If I compile/run it, the following occurs.

#################################################################
/home/me>gcc -Wall -W -pedantic ret.c -o ret
ret.c: In function `ret_fails':
ret.c:19: warning: function returns address of local variable

/home/me>./ret
explicit static
implicit static
lÑ@ˆøÿ¿•À¹@pøÿ¿¨øÿ¿ËY@
##################################################################

I know that ret_fails() is incorrect, undefined behaviour.

from ret_ok() I know it is correct (but may have side-effects
when called more then once)

My question is about ret_maybe(). Is this defined behaviour or not?

I couldn't find it in the FAQ (the other two were in there) nor by
searching clc with google.

Eric
 
J

Joona I Palaste

Eric Moors said:
If have a question with respect to the code below:
#################################################################
#include <stdio.h>
char *ret_ok(void)
{
static char str[] = "explicit static";
return str;
}
OK.

char *ret_maybe(void)
{
return "implicit static";
}

Also OK.
char *ret_fails(void)
{
char str[] = "out-of-scope";

return str;
}

Not OK at all.
int main(void)
{
fprintf(stderr, "%s\n", ret_ok());
fprintf(stderr, "%s\n", ret_maybe());
fprintf(stderr, "%s\n", ret_fails());
return 0;
}
#################################################################
If I compile/run it, the following occurs.
#################################################################
/home/me>gcc -Wall -W -pedantic ret.c -o ret
ret.c: In function `ret_fails':
ret.c:19: warning: function returns address of local variable
True.

/home/me>./ret
explicit static
implicit static
lÑ@ˆøÿ¿•À¹@pøÿ¿¨øÿ¿ËY@
##################################################################
I know that ret_fails() is incorrect, undefined behaviour.
True.

from ret_ok() I know it is correct (but may have side-effects
when called more then once)
True.

My question is about ret_maybe(). Is this defined behaviour or not?

Defined behaviour. Completely safe as you have coded it. Trying to
modify the returned string would be undefined behaviour.
I couldn't find it in the FAQ (the other two were in there) nor by
searching clc with google.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am lying."
- Anon
 
E

Eric Moors

My question is about ret_maybe(). Is this defined behaviour or not?
Defined behaviour. Completely safe as you have coded it. Trying to
modify the returned string would be undefined behaviour.

I wouldn't try to change it, just print it. I wasn't sure
if the memory would be freed, after leaving such a function.
The string is out of scope at that moment, isn't it?

Eric
 
J

Joona I Palaste

I wouldn't try to change it, just print it. I wasn't sure
if the memory would be freed, after leaving such a function.
The string is out of scope at that moment, isn't it?

It's not out of scope, and it isn't freed. String literals occupy
special memory that persists for the duration of the entire program.
It is *always* safe to read them, and *never* safe to write to them.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
 
J

Joe Wright

Joona said:
Eric Moors said:
If have a question with respect to the code below:
#################################################################
#include <stdio.h>
char *ret_ok(void)
{
static char str[] = "explicit static";
return str;
}
OK.


from ret_ok() I know it is correct (but may have side-effects
when called more then once)

True.
[snip]

Pray tell what side effects ret_ok() might have. I say none. The
assignment to str[] is done at compile time, not at run time. Or did I
miss something?
 
J

Jack Klein

It's not out of scope, and it isn't freed. String literals occupy
special memory that persists for the duration of the entire program.
It is *always* safe to read them, and *never* safe to write to them.

It is not out of scope because it doesn't have scope, and it was never
in scope.

Scope refers to the portion of a translation unit in which the same
symbol refers to the same thing, whether it be the name of an object,
function, type definition, structure or enumeration type, or even a
macro.

Scope only applies to symbols, it does not apply to objects at all.
Objects have storage duration, not scope.

A string literal has static storage duration, but as it doesn't have a
name there is no symbol associated with it at all. So it has NO
SCOPE.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
E

Eric Moors

Joona said:
Eric Moors said:
If have a question with respect to the code below:
#################################################################
#include <stdio.h>
char *ret_ok(void)
{
static char str[] = "explicit static";
return str;
}
OK.


from ret_ok() I know it is correct (but may have side-effects
when called more then once)

True.
[snip]

Pray tell what side effects ret_ok() might have. I say none. The
assignment to str[] is done at compile time, not at run time. Or did I
miss something?

No, you are right. I just stripped the ret_ok() too far to have any
side effects. Originally, it contained several strcpy's to a
static array. That's where the side-effects remark stems from.

Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top