在函数中声明的返回数组返回C中局部变量的地址? [英] Returning array declared in function returns address of local variable in C?

查看:137
本文介绍了在函数中声明的返回数组返回C中局部变量的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到,在函数调用中声明的变量将被压入堆栈,并且一旦函数到达其末尾,将弹出在堆栈中声明的局部变量并进入lala land.

I realize that variables declared in a function call are pushed onto the stack and once the function reaches its end the local variables declared onto the stack are popped off and go into lala land.

我不了解的是,如果我在函数中声明了一个指针,则可以返回该指针而无须编译器投诉,而使用数组确实可以给我一个警告.

The thing I don't undertstand is that if I declare a pointer in a function I can return the pointer with no compiler complaint, whereas with an array it does give me a warning.

以下是我所指的示例:

char * getStringArray();
char * getStringPointer();

int main(){
        char * temp1 = getStringPointer();
        char * temp2 = getStringArray();
        return 0;
}
char * getStringPointer(){
        char * retString = "Fred";
        return retString;
}
char * getStringArray(){
        char retString[5] = {'F', 'r','e','d','\0'};
        return retString;
}

在编译期间,它将引发有关getStringArray()的局部变量的返回地址"警告.令我感到困惑的是,有人告诉我,仅通过名称来引用数组(例如retString,no [])是指其在内存中的地址,就像指针一样.

During compilation it throws a "returning address of local variable" warning about getStringArray(). What confuses me is that I've been told that referencing an array solely by its name(like retString, no[])refers to its address in memory, acting like a pointer.

数组可以派上用场,很多时候我想在函数中使用数组并返回它,这有什么快速的方法吗?

Arrays can be handy and there are many times I would like to use an array in a function and return it, is there any quick way around this?

正如我在评论中提到的那样,这行得通吗?我猜malloc分配给堆,但它很好.我仍然不清楚静态数据和堆上数据之间的区别.

As I referred to in a comment, will this work? I'm guessing malloc allocates to heap but so its fine. I'm still a little unclear of the difference between static data and data on the heap.

char * getStringPointer(){
        char * retString = (char *)malloc(sizeof(char)*4+1);
        *(retString+0) = 'F';
        *(retString+1) = 'r';
        *(retString+2) = 'e';
        *(retString+3) = 'd';
        *(retString+4) = '\0';
        return retString;
}

推荐答案

getStringPointer在堆栈上分配一个指针,然后返回该指针,该指针指向可执行文件中某个位置的字符串"Fred \ 0"(而不是堆栈). getStringArray在堆栈上为5个字符分配空间,将它们分配为'F''r''e''d'和'\ 0',然后返回指向堆栈上'F'地址的指针(因此在函数返回后无效).

getStringPointer is allocating a pointer on the stack, then returning that pointer, which points to the string 'Fred\0' somewhere in your executable (not on the stack). getStringArray allocates space for 5 chars on the stack, assigns them 'F' 'r' 'e' 'd' and '\0', then returns a pointer to the address of 'F', which is on the stack (and therefore invalid after the function returns).

有两种解决方法:要么可以malloc为堆上的数组提供一些空间,要么可以创建包含适当大小的数组的struct并将其返回.您可以从函数中返回数字,指针和结构类型,但不能从数组中返回.

There are two ways round it: either you can malloc some space for your array on the heap, or you can make a struct that contains an appropriately sized array and return that. You can return numerical, pointer and struct types from functions, but not arrays.

这篇关于在函数中声明的返回数组返回C中局部变量的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆