函数会返回一个“局部”的char * VS从函数返回一个'本地'廉政*

编程入门 行业动态 更新时间:2024-10-15 00:25:33
本文介绍了函数会返回一个“局部”的char * VS从函数返回一个'本地'廉政* - 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述   

可能重复:结果  Can一个局部变量的内存是它的范围之外访问?

下面是一个简单的code,其中在3个不同功能的 [localStrPtr,localIntPtr,localCharPtr] 返回一个指向自己的局部变量的 [字符串,整数,字符] 在各自的功能。

code:

的#include<&stdio.h中GT;字符* localStrPtr(字符*);为int * localIntPtr(INT,INT);字符* localCharPtr(焦);主要(){    INT *品脱;    字符* PCHAR;    的printf(localStrPtr =%S \\ n,l​​ocalStrPtr(ABCD));    品脱=为(int *)localIntPtr(3,5);    的printf(localIntPtr =%d个\\ N,*品脱);    PCHAR =(字符*)localCharPtr('Y');    的printf(localCharPtr =%C \\ n,* PCHAR);}字符* localStrPtr(字符*的申辩){    焦炭海峡[20];    //字符*海峡=(字符*)malloc的(20);    的strcpy(STR,申辩后);    返回海峡;}为int * localIntPtr(INT argu1,诠释argu2){    INT地方;    当地= argu1 + argu2;    返回(安培;地方);}字符* localCharPtr(字符的申辩){    局部烧焦;    当地=的申辩;    返回(安培;地方);}

COMPILE LOG:

stringManip.c:在功能`localStrPtr:stringManip.c:27:警告:函数返回局部变量的地址stringManip.c:在功能`localIntPtr:stringManip.c:34:警告:​​函数返回局部变量的地址stringManip.c:在功能`localCharPtr:stringManip.c:41:警告:函数返回局部变量的地址

运行日志:

localStrPtr =localIntPtr = 8localCharPtr = Y

你可以在日志文件中看到,localStrPtr返回一些垃圾,而localIntPtr和localCharPtr回归期待的价值观。

不过,在功能上的 localStrPtr ,如果我改变[字符海峡[20] - 到 - >的char *海峡=(字符* )的malloc(20)],localStrPtr返回字符串ABCD正确。这里是运行日志,一旦上述更改。

新的运行日志:

localStrPtr = ABCDlocalIntPtr = 8localCharPtr = Y

问题:

  • 在功能localIntPtr和localCharPtr,返回局部变量的地址内容的工作,但对于功能localStrPtr,返回正确的值只有使用malloc,但不会与本地字符海峡[20]。为什么它不通过str工作[20]

  • 为什么我们在编译日志中看到,该线下方的所有3个功能?

    • stringManip.c:27:警告:函数返回局部变量的地址
    • stringManip.c:34:警告:​​函数返回局部变量的地址
    • stringManip.c:41:警告:函数返回局部变量的地址
  • 解决方案

    未定义的行为,在所有3例。这未定义包括它可能工作的可能性。或者看起来像它的工作。有时。

    您正在返回一个指向一个局部变量,这是在栈上分配。该内存不再保留该变量,当它超出范围,这将是当函数返回。无论是否获得内容改变,而出现这种情况的时候,是归因于运气。由于你的情况发生时,你有一对夫妇的情况下很幸运,而不是其他。在不同的一天,编译器可能已经取得了一些不同的选择内部,它的行为不同。或者,也许(可能)的数据被覆盖你打喷嚏的下一次。

    不要这么做。

    Possible Duplicate: Can a local variable’s memory be accessed outside its scope?

    Here is a simple code, where in 3 different functions [ localStrPtr, localIntPtr, localCharPtr] return a pointer to their local variables [string, integer, char] in their respective functions.

    CODE:

    #include <stdio.h> char* localStrPtr (char*); int* localIntPtr (int, int); char* localCharPtr (char); main() { int *pInt; char *pChar; printf( "localStrPtr = %s\n", localStrPtr("abcd") ); pInt = (int*) localIntPtr(3, 5); printf( "localIntPtr = %d\n", *pInt ); pChar = (char*) localCharPtr('y'); printf( "localCharPtr = %c\n", *pChar ); } char* localStrPtr(char* argu) { char str[20]; // char* str = (char*) malloc (20); strcpy (str, argu); return str; } int* localIntPtr (int argu1, int argu2) { int local; local = argu1 + argu2; return (&local); } char* localCharPtr (char argu) { char local; local = argu; return (&local); }

    COMPILE LOG:

    stringManip.c: In function `localStrPtr': stringManip.c:27: warning: function returns address of local variable stringManip.c: In function `localIntPtr': stringManip.c:34: warning: function returns address of local variable stringManip.c: In function `localCharPtr': stringManip.c:41: warning: function returns address of local variable

    RUN LOG:

    localStrPtr = localIntPtr = 8 localCharPtr = y

    As you can see in the log file, localStrPtr returns "some garbage", whereas localIntPtr and localCharPtr return "expected" values.

    But, in the function localStrPtr, if I change [ "char str[20]" -to-> "char* str = (char*) malloc (20)" ], localStrPtr returns the string "abcd" correctly. Here is the RUN LOG, once the above change is made.

    NEW RUN LOG:

    localStrPtr = abcd localIntPtr = 8 localCharPtr = y

    QUESTIONS:

  • In functions localIntPtr and localCharPtr, contents of the returned local variable addresses WORKED, but for the function localStrPtr, correct value is returned "only" with malloc, but will not with local char str[20]. Why doesn't it work with str[20] ?

  • Why do we see in the COMPILE LOG, the lines below for all the 3 functions ?

    • stringManip.c:27: warning: function returns address of local variable
    • stringManip.c:34: warning: function returns address of local variable
    • stringManip.c:41: warning: function returns address of local variable
  • 解决方案

    Undefined behaviour, in all 3 cases. That 'undefined' includes the possibility that it might work. Or look like it's working. Sometimes.

    You're returning a pointer to a local variable, which is allocated on the stack. That memory is no longer reserved for that variable when it goes out of scope, which will be when the function returns. Whether or not the contents get altered, and when that happens, is down to luck. As it happens in your case, you got lucky with a couple of cases, but not the other. On a different day, the compiler might have made some different choice internally, and it behaves differently. Or maybe (probably) that data gets overwritten the next time you sneeze.

    Don't do it.

    更多推荐

    函数会返回一个“局部”的char * VS从函数返回一个'本地'廉政*

    本文发布于:2023-10-24 21:12:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1525021.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:函数   局部   char   廉政

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!