473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

what type of string returns are allowed

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
Nov 13 '05 #1
6 2005
Eric Moors <sc********@oz.land> scribbled the following:
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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2
>> 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
Nov 13 '05 #3
Eric Moors <sc********@oz.land> scribbled the following:
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?


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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #4
Joona I Palaste wrote:

Eric Moors <sc********@oz.land> scribbled the following:
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?
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #5
On 28 Aug 2003 13:30:05 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote in comp.lang.c:
Eric Moors <sc********@oz.land> scribbled the following:
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?


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
Nov 13 '05 #6
On Fri, 29 Aug 2003 04:55:13 +0200, Joe Wright wrote:
Joona I Palaste wrote:

Eric Moors <sc********@oz.land> scribbled the following:
> 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
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: anonieko | last post by:
This applies to javascript dynamic textbox onkey > > > Newsgroups: comp.lang.javascript From: Lasse Reichstein Nielsen <l...@hotpop.com> - Find messages by this author Date: Fri, 15 Jul 2005...
17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
11
by: Demorsy | last post by:
I want to convert a string (or object) to a primitive type. But I don't know the type to convert to at run time. For example l have variable (lets say its an int): int unknownType = 0; And a...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
8
by: werner | last post by:
Hi! I don't want to use eval() in order to parse a user-supplied formula. What alternatives do I have? PHP has no standard functionality for tokenizing or parsing expressions in this regard. ...
5
by: Ankit Aneja | last post by:
This is my problem. I am using Visual 2003, .Net framework 1.1 and the Application Block I configured the DAAB using the Enterprise Library Configuration now this is the error which is coming ...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.