Code Comments
Programming Forum and web based access to our favorite programming groups.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]$
Post Follow-up to this messageMr.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
Post Follow-up to this messagehi 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
Post Follow-up to this message"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."
Post Follow-up to this messageNote 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.
Post Follow-up to this message"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
Post Follow-up to this messageHubble wrote Maurizio Loretti wrote: > wrong. Yes, I meant b() before c(). Hubble.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.