×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

2 is wrong, while 1 and 3 are not so right

syntaxly , 2 is wrong, because it try to return memory on the stack, when v() is finished the the memory on stack is released. the returned pointer ref to invalid memory.
1 and 3 are exactly the same except in debug build 1 will alloc one pointer on stack, in optimized mode, these 2 function will all probably return a constant address,semanticly, 1&3 are all wrong. The string "result of function v()" is allocated by compiler in Initialized data segement, but, notice, logically, it should be constant,although u can modify it.
const char^ v()
{
return "result of function v()";
}
will be the best way
Report

Replies, comments and Discussions:

  • 工作学习 / 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?
    • maybe the second is right, the first and the end must be wrong.
      • Are you John?
        • but 1 and 3 always need add malloc lf the string's length > 5.
    • 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.
    • 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.
    • 2 is wrong, while 1 and 3 are not so right
      syntaxly , 2 is wrong, because it try to return memory on the stack, when v() is finished the the memory on stack is released. the returned pointer ref to invalid memory.
      1 and 3 are exactly the same except in debug build 1 will alloc one pointer on stack, in optimized mode, these 2 function will all probably return a constant address,semanticly, 1&3 are all wrong. The string "result of function v()" is allocated by compiler in Initialized data segement, but, notice, logically, it should be constant,although u can modify it.
      const char^ v()
      {
      return "result of function v()";
      }
      will be the best way
    • 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.
      • 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.
        • 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;
          }
          • Ya, I said that 3 is right. what do u want to say according to your last post?
            • what I want to say is: theoritically, everyone is wrong. But in practice, everyone can run with dangerous.
      • Do you really try with VC++?
        • Of course!
    • Who can answer my question?
    • UP! Help!!
      • 1,3正确,返回的是字符串在内存中头指针,我理解固定字符串类似于静态变量。2错误,局部变量。
      • 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.
    • 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.
      • 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.
      • 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.
    • 只有1是对的
    • 1 right, 2 right, 3 right -- all right