函数返回局部变量

一般的来说,函数是可以返回局部变量的。 局部变量的作用域只在函数内部,在函数返回后,局部变量的内存已经释放了。因此,如果函数返回的是局部变量的值,不涉及地址,程序不会出错。但是如果返回的是局部变量的地址(指针)的话,程序运行后会出错。因为函数只是把指针复制后返回了,但是指针指向的内容已经被释放了,这样指针指向的内容就是不可预料的内容,调用就会出错。准确的来说,函数不能通过返回指向栈内存的指针(注意这里指的是栈,返回指向堆内存的指针是可以的)。
1. 返回局部变量的值

可以有两种情况:返回局部自动变量和局部静态变量,比如,

int func()
    <div class="line number2 index1 alt1" style="font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace;white-space:normal;background-color:#FFFFFF;line-height:2em !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;border:0px !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;margin:0px !important;outline:#000000 !important;overflow:visible !important;padding:0px 1em !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:auto !important;">
        {
    </div>
    <div class="line number3 index2 alt2" style="font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace;white-space:normal;line-height:2em !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;background-color:#F8F8F8 !important;border:0px !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;margin:0px !important;outline:#000000 !important;overflow:visible !important;padding:0px 1em !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:auto !important;">
        &nbsp;&nbsp;&nbsp;&nbsp;int temp = 0;&nbsp;&nbsp; // 返回局部自动变量的值
    </div>
    <div class="line number4 index3 alt1" style="font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace;white-space:normal;background-color:#FFFFFF;line-height:2em !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;border:0px !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;margin:0px !important;outline:#000000 !important;overflow:visible !important;padding:0px 1em !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:auto !important;">
        &nbsp;&nbsp;&nbsp;&nbsp;return temp;
    </div>
    <div class="line number5 index4 alt2" style="font-family:'Courier New', Consolas, 'Bitstream Vera Sans Mono', Courier, monospace;white-space:normal;line-height:2em !important;border-top-left-radius:0px !important;border-top-right-radius:0px !important;border-bottom-right-radius:0px !important;border-bottom-left-radius:0px !important;background-image:none !important;background-color:#F8F8F8 !important;border:0px !important;bottom:auto !important;float:none !important;height:auto !important;left:auto !important;margin:0px !important;outline:#000000 !important;overflow:visible !important;padding:0px 1em !important;position:static !important;right:auto !important;top:auto !important;vertical-align:baseline !important;width:auto !important;box-sizing:content-box !important;min-height:auto !important;">
        }
    </div>
    <p>
        <br>
    </p>
    <div>
        <div id="highlighter_148137" class="syntaxhighlighter  html." style="width:1020px;margin:1em 0px !important;position:relative !important;overflow:auto !important;font-size:1em !important;">
        </div>
    </div>
    <p style="line-height:1.4;margin:10px auto;">
        局部变量temp存储在栈中,函数返回时会自动复制一份temp的copy给调用者,没有问题。<br>



int func()


{


    static int a = 1;   // 返回局部静态变量的值


    return a;


}



局部变量a存储在静态(全局)存储区中,从初始化后一直有效直到程序结束,仅分配一次内存,并且函数返回后,变量不会销毁,没有问题。



vector func()


{


    vector v;


    v.push_back(0);


    return v;


}





返回的是v的值拷贝,没有问题。



Person func()


{


    Person p1;


    p1.name = “test”;


    return p1;


}







?




返回的也是值拷贝,会调用Person类的拷贝构造函数,没有问题。



2. 返回局部变量的指针



int* func()


{


    int temp = 0;   // 返回局部变量的地址


    return &temp;


}







?




前面讨论过,局部变量temp存储在栈中,函数返回时将已销毁变量的地址返回给调用者,结果将是不可预知的。



int* func()


{


    static int temp = 1;


    return &temp;


}







?




局部变量temp存储在静态存储区,返回指向静态存储区变量的指针是可行的。



char* func()


{


    char *p = “test”;


    return p;   // 返回指向常量字符串的指针


}







?




对于字符串的特殊情况,由于字符串test存储在常量存储区(不是静态存储区),因此函数返回一个指向常量的字符串指针是可行的。



char* func()


{


    char str[] = “test”;


    return str; // 返回局部字符串的指针


}







?




这种情况下,str被初始化为字符串局部变量,因此函数返回一个已销毁的局部变量是不可行的。解决办法就是将字符串str声明为static。



char* func()


{


    char str = (char )malloc(sizeof(char) * BUFFER_SIZE);


    strcpy(str, “test”);


    return str;


}







?




这种情况下,函数返回一个指向堆内存的指针,由于堆存储区由程序员手动管理,因此这种做法是可行的,但是要防止出现内存泄露,函数调用完后需要手动释放内存。这里的sizeof作用于指针返回的是指针类型的长度1byte,而如果作用于数组返回的则是数组的长度。



char *temp = NULL;
temp = func();


// some operation…


free(temp);







?




3. 返回局部变量的引用



int& func()


{


    int temp = 0;   // 返回局部变量的引用


    return temp;


}









由引用的概念可知,函数返回的是temp本身,而temp在函数返回后已销毁,结果将是不可预知的。



补充:静态全局变量和全局变量的区别



静态全局变量只在当前文件中可用,全局变量在其他文件中也可用,需要用extern声明。



全局变量和静态变量如果没有手动初始化,则默认由编译器初始化为0。



1:





[cpp] 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值