This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / which one is right?char * v()
{
char * p="result of function v()";
return p;
}
char * v()
{
char p[]="result of function v()";
return p;
}
char * v()
{
return "result of function v()";
}
which one is right? Or all of them are false.
what is the diffirence?
-guest:guest;
2001-5-15
{269}
(#70734@0)
-
maybe the second is right, the first and the end must be wrong.
-billhuang(瞎跑);
2001-5-15
(#70822@0)
-
Are you John?
-willcome(WillCome);
2001-5-15
(#70884@0)
-
but 1 and 3 always need add malloc lf the string's length > 5.
-billhuang(瞎跑);
2001-5-16
(#71898@0)
-
Right answer is as follows: 1,3 are right, 2 is wrong. Since the second return a pointer which point to a temporary object. So when the function returns, the pointer becomes a suspending pointer.
-guest:whois;
2001-5-15
(#70838@0)
-
1,3 are correct but 2 is wrong.In 1), 3) the address of somewhere in the static data area will be returned.
But in 2), p is the address of the array, which is allocated on the stack and in one stack frame. After the function 'v()' is returned the stack frame
is removed (or become invalid), thus the address of the array (i.e. 'p') becomes an invalid address.
-guest:willcome;
2001-5-15
{334}
(#70872@0)
-
2 is wrong, while 1 and 3 are not so right
-blaise(blaise);
2001-5-15
{617}
(#70891@0)
-
To whois, blaise and willcome:
The problem is : When using the VC++ to compile them, 1, 3 are right without even a warning. 2 is right too with a warning.But from the article I have read: 3 is wrong too. I am really confused.
) Fairly Easy. What is wrong with the following function? Explain.
char* GetName()
{
return "Bill Clinton";
}
Answer: The string "Bill Clinton" is allocated on the stack and thus deallocated when the function returns. The function returns a pointer that cannot be trusted, aka a bad pointer.
-guest:guest;
2001-5-15
{387}
(#71255@0)
-
u r wrong. 3 is right. "Bill Clinton" will return an address which will exist after return. Do u try it? I mean, print out the result which pointer truly point.
-guest:whois;
2001-5-15
(#71801@0)
-
You can try it yourself. I compiled and debugged. No problem.#include "stdio.h"
char * v()
{
return "result of function v()";
}
main()
{
char * p=v();
printf("%s\n", p);
return;
}
-guest:guest;
2001-5-15
{143}
(#71815@0)
-
Ya, I said that 3 is right. what do u want to say according to your last post?
-guest:whois;
2001-5-16
(#72076@0)
-
what I want to say is: theoritically, everyone is wrong. But in practice, everyone can run with dangerous.
-guest:guest;
2001-5-16
(#72101@0)
-
Do you really try with VC++?
-willcome(WillCome);
2001-5-18
(#74563@0)
-
Of course!
-guest:guest;
2001-5-18
(#74808@0)
-
Who can answer my question?
-guest:guest;
2001-5-15
(#71751@0)
-
UP! Help!!
-guest:guest;
2001-5-16
(#72473@0)
-
1,3正确,返回的是字符串在内存中头指针,我理解固定字符串类似于静态变量。2错误,局部变量。
-foolzz(foolzz);
2001-5-16
(#72617@0)
-
The answer in the article you mentioned in your posting (#71255) was definitely wrong. The string in your example 1 and 3 are string constants. See #70891 by blaise.
-old(HH);
2001-5-26
(#81727@0)
-
In my opinion, 1 wrong. 2 wrong. 3 wrong.1 , The pointer P you defined in the function is a local variable , so it will return NULL. in this case.
2. The same as number 1.
3. The function needs you return pointer , but you give it a string value.
The solutions is either:
char* v(char* p)
{
char p[] = " result of function v()";
return p;
} This one is better
or
char* v()
{
static p[]="result of function v()";
return p;
} It is ok.
-mycli(cli);
2001-5-31
{425}
(#86423@0)
-
you are right. The reason that some people could compile and run it with some compilers is that the destroyed stacks haven't been overwritten with new data.
-numnum(numnum);
2001-5-31
(#86476@0)
-
It is not difficult to get the right answer if one know what is the string constants (sometimes it is called string literals) in C/C++. #70891 by blaise, has given a right answer.Sometimes a programmer can use options provided by some compilers to put the string constants into the code segment instead of the data segment.
-old(HH);
2001-6-2
{144}
(#88357@0)
-
只有1是对的
-im_stone(im_stone);
2001-6-1
(#87463@0)
-
1 right, 2 right, 3 right -- all right
-guest:ask your teacher;
2001-6-2
(#88366@0)