Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

return value makes me sad.
I make simple function that return static char data type.
as you see, It's a simple.

I got proper(?) result  when first func call.
But, second func call, I got last func call's return value.
How can I solve this problem?



// code
char *getStr(int num)
{
static char str[30];

if( num == 1)
snprintf(str, sizeof(str), "Got 1");
else
snprintf(str, sizeof(str), "Got 0");

printf("before return: %s\n", str);
return str;
}

int main()
{
printf("[call each]\n");
printf("getStr(1) : %s\n", getStr(1));
printf("getStr(0) : %s\n", getStr(0));

printf("\n[call together]\n"); // <--
printf(
"getStr(1) : %s\n"
"getStr(0) : %s\n"
, getStr(1)
, getStr(0)
);

return 0;
}


[merong@ultrahypersuperdom test]$ make b
cc     b.c   -o b
[merong@ultrahypersuperdom test]$ ./b
[call each]
before return: Got 1
getStr(1) : Got 1
before return: Got 0
getStr(0) : Got 0

[call together]
before return: Got 0
before return: Got 1
getStr(1) : Got 1
getStr(0) : Got 1
[merong@ultrahypersuperdom test]$



Report this thread to moderator Post Follow-up to this message
Old Post
Mr.Babo
12-03-05 02:16 AM


Re: return value makes me sad.
Mr.Babo wrote:

>I make simple function that return static char data type.
>as you see, It's a simple.
>
>I got proper(?) result  when first func call.
>But, second func call, I got last func call's return value.
>How can I solve this problem?
>
>
>
>// code
>char *getStr(int num)
>{
> static char str[30];
>
> if( num == 1)
>  snprintf(str, sizeof(str), "Got 1");
> else
>  snprintf(str, sizeof(str), "Got 0");
>
> printf("before return: %s\n", str);
> return str;
>}
>
>int main()
>{
> printf("[call each]\n");
> printf("getStr(1) : %s\n", getStr(1));
> printf("getStr(0) : %s\n", getStr(0));
>
> printf("\n[call together]\n"); // <--
> printf(
>  "getStr(1) : %s\n"
>  "getStr(0) : %s\n"
>  , getStr(1)
>  , getStr(0)
> );
>
> return 0;
>}
>
>
>[merong@ultrahypersuperdom test]$ make b
>cc     b.c   -o b
>[merong@ultrahypersuperdom test]$ ./b
>[call each]
>before return: Got 1
>getStr(1) : Got 1
>before return: Got 0
>getStr(0) : Got 0
>
>[call together]
>before return: Got 0
>before return: Got 1
>getStr(1) : Got 1
>getStr(0) : Got 1
>[merong@ultrahypersuperdom test]$
>
>
>
>
I am getting the following result :

[call each]
before return: Got 1
getStr(1) : Got 1
before return: Got 0
getStr(0) : Got 0

[call together]
before return: Got 1
before return: Got 0
getStr(1) : Got 0
getStr(0) : Got 0

Regards,
Thobias Vakayil

Report this thread to moderator Post Follow-up to this message
Old Post
Thobias Vakayil
12-03-05 02:16 AM


Re: return value makes me sad.
hi
basically when you call printf along with functions then it will
execute both the function and put that returned string address at that
point since after calling the functions whichever executed lastly will
be saving into the static part ,that why you are getting this.
you can check this by calling separately


Report this thread to moderator Post Follow-up to this message
Old Post
ambrish.k.baranwal@gmail.com
12-03-05 02:16 AM


Re: return value makes me sad.
"Mr.Babo" <mrbabo2@com.ne.kr> writes:

> I make simple function that return static char data type.
> as you see, It's a simple.
>
> I got proper(?) result  when first func call.
> But, second func call, I got last func call's return value.
> How can I solve this problem?

Think about it!!!


What code will the C compiler generate ?

> // code
> char *getStr(int num)
> {
>  static char str[30];

str = 30 byes starting from 0x1000

>  if( num == 1)
>   snprintf(str, sizeof(str), "Got 1");

Store  71  to 0x1000
Store  111 to 0x1001
Store  116 to 0x1002
Store   32 to 0x1003
Store   49 to 0x1004
Store    0 to 0x1005

>  else
>   snprintf(str, sizeof(str), "Got 0");

Store  71  to 0x1000
Store  111 to 0x1001
Store  116 to 0x1002
Store   32 to 0x1003
Store   48 to 0x1004
Store    0 to 0x1005

>
>  printf("before return: %s\n", str);
>  return str;
> }
>
> int main()
> {
>  printf("[call each]\n");
>  printf("getStr(1) : %s\n", getStr(1));
>  printf("getStr(0) : %s\n", getStr(0));
>
>  printf("\n[call together]\n"); // <--


>  printf(
>   "getStr(1) : %s\n"
>   "getStr(0) : %s\n"
>   , getStr(1)
>   , getStr(0)

Push 0 on the stack
Call getStr
Pop the arguments of getStr
Push the return value of getStr

Push 1 on the Stack
Call getStr
Pop the arguments of getStr
Push the return value of getStr

Push 0x2000 on the Stack  (The memory at 0x2000 will contain the bytes of:
"getStr(1) : %s\ngetStr(0) : %s\n")
Call printf
Pop the arguments of printf


So when it executes it:

Push 0 on the stack
Call getStr
Pop the arguments of getStr
Push the return value of getStr

The return value is the address of str = 0x1000
The bytes stored at 0x1000 .. 0x1005  are:  71 111 116 32 48 0
The stack contains: 0x1000

Push 1 on the Stack
Call getStr
Pop the arguments of getStr
Push the return value of getStr

The return value is the address of str = 0x1000
The bytes stored at 0x1000 .. 0x1005  are:  71 111 116 32 49 0
The stack contains: 0x1000 0x1000


Push 0x2000 on the Stack  (The memory at 0x2000 will contain the bytes of:
"getStr(1) : %s\ngetStr(0) : %s\n")

The stack contains: 0x2000 0x1000 0x1000 (top on the left)
The bytes stored at 0x1000 .. 0x1005  are:  71 111 116 32 49 0

Call printf
Pop the arguments of printf


So in effect you are calling: printf("getStr(1) : %s\ngetStr(0) : %s\n",str,
str);
and of course str contains only the last value stored in it.




That's why it's a bad idea to return pointers to static local
variables.  You'll have to malloc the result of getStr.  And of
course, you'll have to free it sometimes later.  So it becomes quite
messy, unless you use a garbage collector (try boemgc).

http://www.hpl.hp.com/personal/Hans_Boehm/gc/

Welcome to your long Greenspunning travel!

--
__Pascal Bourguignon__                     http://www.informatimago.com/

"I have challenged the entire quality assurance team to a Bat-Leth
contest.  They will not concern us again."

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
12-03-05 02:16 AM


Re: return value makes me sad.
Note that the C reference specifies that the order of parameter
evaluation is *unspecified*. So if you code something like

a(b(), c())

nothing guarantees that a() is called before b(). If you depend on the
sequence of calling functions b and c (by using static variables etc.)
the result is unspecified.

(Note: even when using a=b()-c(), the evaluation order of b and c is
*unspecified*.

Hubble.


Report this thread to moderator Post Follow-up to this message
Old Post
Hubble
12-03-05 02:16 AM


Re: return value makes me sad.
"Hubble" <reiner@huober.de> writes:

> Note that the C reference specifies that the order of parameter
> evaluation is *unspecified*. So if you code something like
>
>    a(b(), c())
>
> nothing guarantees that a() is called before b().

wrong.  the order of evaluation of the parameters is unspecified, b()
may be called before or after c(); but is guaranteed that, before a
function is called (here a()) all the parameters have been evaluated
and the side effects OK.

--
Maurizio Loreti                         [url]http://www.pd.infn.it/~loreti/mlo.html[/ur
l]
Dept. of Physics, Univ. of Padova, Italy              ROT13: ybergv@cq.vasa.
vg

Report this thread to moderator Post Follow-up to this message
Old Post
Maurizio Loreti
12-03-05 02:16 AM


Re: return value makes me sad.
Hubble wrote 

Maurizio Loretti wrote:
> wrong.

Yes, I meant b() before c().

Hubble.


Report this thread to moderator Post Follow-up to this message
Old Post
Hubble
12-13-05 01:14 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:39 PM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.