为什么函数的返回值可以赋值给?
考虑以下函数:
char *f()
{
char *s=malloc(8);
}
main()
{
printf("%c",*f()='A');
}
如果我注释行 char *s=malloc(8);
,我会收到一个错误,就像访问了赋值 *f()='A'
一样无效记忆。既然我从不返回任何变量,为什么上面的赋值起作用呢?
第二个问题:'A'
被分配给函数返回时创建的临时变量。那么为什么 ++a
等不能用作左值呢?
Consider the following function:
char *f()
{
char *s=malloc(8);
}
main()
{
printf("%c",*f()='A');
}
If I comment the line char *s=malloc(8);
I get an error as if the assignment *f()='A'
accessed invalid memory. Since I never return any variable why does above assignment work at all?
2nd question: 'A'
is assigned to temporary variable created on return of function . So why can't ++a
etc. be used as lvalue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
假设返回值在寄存器中传递,那么当从 f() 返回时,malloc 的返回值可能仍然存在。纯属偶然。
当分配给
*f()
时,您不是分配给临时内存,而是分配给从 malloc 返回的内存。分配给 ++a 是完全不同的。Assuming return values are passed in registers, the return value from malloc might still be there when returning from f(). By pure chance.
When assigning to
*f()
you are not assigning to a temporary but to the memory returned from malloc. Assigning to ++a is totally different.您的函数
f()
没有返回任何内容,您需要添加:但是,老实说,这只是您问题的开始。您还需要
free()
f()
的返回值。我不知道你为什么把这个问题标记为C++,这显然是C,所以我没有这样标记。
Your function
f()
is not returning anything, you need to add:But, in all honesty, this is just going to be the start of your problems. You also need to
free()
the return value off()
.I do not know why you have tagged this question C++, this is clearly C, and so I have untagged as such.
您必须使用
return
语句返回f()
中的指针,否则将返回非法指针:You have to return the pointer in
f()
with thereturn
statement, or else an illegal pointer will be returned:您的函数不返回任何内容。由于您使用返回类型
char *
声明该函数,因此不返回任何内容会导致未定义的行为,如当前 C++ 标准第 6.6.3.2 段中所定义:未定义的行为意味着任何事情都可能发生。要解决该问题,您的函数应如下所示:
Your function does not return anything. Since you declare the function with the return-type
char *
, not returning anything results in undefined behavior, as defined in paragraph 6.6.3.2 of the current C++ Standard:Undefined behavior means that anything can happen. To fix that problem, your function should look like this:
在你的函数中,你返回一个指针,你可以在其中分配东西...
++a
通过返回const
引用或实例来防止它。如果您有const char* f() { ... }
,则可以实现相同的行为。当然你也可以以不同的方式实现
++a
:)In your function, you return a pointer where you can assign stuff...
++a
prevents it by returningconst
reference or instance. you can have the same behaviour if you haveconst char* f() { ... }
.Of course you could also implement
++a
differently :)1/ f()返回的是一个未初始化的指针,但是存在。
*f() 返回未指定(随机)地址所指向的值。
在此地址写入是无效的内存访问,或者如果该地址“偶然”是一块可写的内存(堆栈或先前分配的堆),则可能不是无效的内存访问。
在 C 语言中,您有责任确保正确访问内存。
2/ 此处未将“A”分配给临时对象。
1/ The return of f() is an uninitialized pointer, but exists.
*f() return the value pointed by an unspecified (random) address.
Writing at this address is an invalid memory access, or maybe not if this address is by "chance" a writable piece of memory (stack or previously allocated heap).
In C, it is your responsibility to ensure that you properly access the memory.
2/ 'A' is not assigned here to a temporary.