按值传递结构 [英] Passing structs by value

查看:35
本文介绍了按值传递结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下程序是否显示未定义的行为?具体来说,

确实按值传递一个struct导致未定义的行为如果那个结构

作为一个成员已经传递给free()的指针?


#include< stdlib.h>


struct stype

{

int * foo;

};


void bar(struct stype foo)

{

}

int main(无效)

{

struct stype baz;

baz.foo = malloc(sizeof * baz.foo);

免费(baz.foo);

bar(baz); / * UB与否? * /

返回0;

}


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


Does the following program exhibit undefined behavior? Specifically,
does passing a struct by value cause undefined behavior if that struct
has as a member a pointer that has been passed to free()?

#include <stdlib.h>

struct stype
{
int *foo;
};

void bar( struct stype foo )
{
}

int main( void )
{
struct stype baz;
baz.foo=malloc( sizeof *baz.foo );
free( baz.foo );
bar( baz ); /* UB or not? */
return 0;
}

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

2005年12月5日星期一05:20:20 +0000(UTC),Christopher Benson-Manica

< at *** @ nospam.cyberspace.org>在comp.lang.c中写道:
On Mon, 5 Dec 2005 05:20:20 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote in comp.lang.c:

以下程序是否表现出未定义的行为?具体来说,
按值传递一个struct会导致未定义的行为,如果该结构
作为一个成员已经传递给free()的指针?


我不知道你为什么要区分由值传递的
结构中的指针和通过值传递的裸指针。另外

我不知道为什么你要区分具有

不确定值的指针,因为它未初始化,或者因为它指向

以前分配的存储已被释放。

#include< stdlib.h>

struct stype
{
int * foo ;
};

void bar(struct stype foo)
{
}
int main(void)
{
struct stype baz;
baz.foo = malloc(sizeof * baz.foo);
免费(baz.foo);
bar(baz); / * UB与否? * /
返回0;
}

Does the following program exhibit undefined behavior? Specifically,
does passing a struct by value cause undefined behavior if that struct
has as a member a pointer that has been passed to free()?
I''m not sure why you make a distinction between a pointer in a
structure passed by value, and a bare pointer passed by value. Also
I''m not sure why you make a distinction between a pointer that has an
indeterminate value because it is uninitialized, or because it points
to previously allocated storage that has been freed.
#include <stdlib.h>

struct stype
{
int *foo;
};

void bar( struct stype foo )
{
}

int main( void )
{
struct stype baz;
baz.foo=malloc( sizeof *baz.foo );
free( baz.foo );
bar( baz ); /* UB or not? */
return 0;
}




在几个地方,C标准引用函数调用参数

通过赋值存储到函数的参数中。所以将任何东西传递给C中的函数基本上与将函数分配给函数的本地参数值相同。


C中的赋值总是基于值,所以将任何带有

的值除了unsigned char之外的不确定值传递给函数是

未定义的行为,因为任何其他使用这种

对象的价值。具有不确定值的对象是否是结构的一部分没有区别,

值如何变得不确定也没有任何区别。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http:/ /www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a ... FAQ-acllc.html



In several places, the C standard refers to function call arguments
being stored into the function''s parameters by assignment. So passing
anything to a function in C is essentially the same as assigning it to
the function''s local parameter value.

Assignment in C is always based on value, so passing anything with
indeterminate value, other than an unsigned char, to a function is
undefined behavior, as is any other use of the value of such an
object. It makes no difference whether the object with indeterminate
value is part of a structure, nor does it make any difference how the
value became indeterminate.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jack Klein< ja ******* @ spamcop.net>写道:
Jack Klein <ja*******@spamcop.net> writes:
2005年12月5日星期一05:20:20 +0000(UTC),Christopher Benson-Manica
< at *** @ nospam.cyberspace.org>在comp.lang.c中写道:
On Mon, 5 Dec 2005 05:20:20 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote in comp.lang.c:
以下程序是否显示未定义的行为?具体来说,
按值传递一个struct导致未定义的行为,如果该结构
作为一个成员已经传递给free()的指针?
Does the following program exhibit undefined behavior? Specifically,
does passing a struct by value cause undefined behavior if that struct
has as a member a pointer that has been passed to free()?



我''我不确定为什么要区分由值传递的
结构中的指针和通过值传递的裸指针。另外
我不确定为什么要区分具有
不确定值的指针,因为它未初始化,或者因为它指向已释放的先前分配的存储。



I''m not sure why you make a distinction between a pointer in a
structure passed by value, and a bare pointer passed by value. Also
I''m not sure why you make a distinction between a pointer that has an
indeterminate value because it is uninitialized, or because it points
to previously allocated storage that has been freed.

#include< stdlib.h>

struct stype
{
int * foo;
};

void bar(struct stype foo)
{
}
int main(void)
{
struct stype baz;
baz.foo = malloc(sizeof * baz.foo);
免费(baz.foo);
bar(baz); / * UB与否? * /
返回0;
}
#include <stdlib.h>

struct stype
{
int *foo;
};

void bar( struct stype foo )
{
}

int main( void )
{
struct stype baz;
baz.foo=malloc( sizeof *baz.foo );
free( baz.foo );
bar( baz ); /* UB or not? */
return 0;
}



在几个地方,C标准引用函数调用参数
存储到函数中分配参数。因此将任何内容传递给C中的函数与将函数分配给函数的本地参数值基本相同。

C中的赋值始终基于值,因此,除了unsigned char之外,将任何带有
不确定值的任何东西传递给函数是未定义的行为,就像使用这样一个
对象的值一样。具有不确定值的对象是否是结构的一部分没有区别,
值如何变得不确定也没有任何区别。



In several places, the C standard refers to function call arguments
being stored into the function''s parameters by assignment. So passing
anything to a function in C is essentially the same as assigning it to
the function''s local parameter value.

Assignment in C is always based on value, so passing anything with
indeterminate value, other than an unsigned char, to a function is
undefined behavior, as is any other use of the value of such an
object. It makes no difference whether the object with indeterminate
value is part of a structure, nor does it make any difference how the
value became indeterminate.




但DR#222< http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_222.htm>

说:


结构或联合对象的值永远不是陷阱

表示,即使结构成员的值或

联合对象可能是一个陷阱代表。


这是在TC2和N1124中发布的。


-

Keith Thompson (The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



But DR #222 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_222.htm>
says:

The value of a struct or union object is never a trap
representation, even though the value of a member of a struct or
union object may be a trap representation.

This was published in TC2 and in N1124.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson写道:
Keith Thompson wrote:

Jack Klein< ja ******* @ spamcop.net>写道:

Jack Klein <ja*******@spamcop.net> writes:


C中的赋值总是基于值,所以传递任何带有
不确定值的东西,而不是unsigned char,一个函数是未定义的行为,就像任何其他使用这样一个
对象的值一样。
具有不确定值的对象是否是一部分没有区别一个结构,
也没有任何区别
值如何变得不确定。
Assignment in C is always based on value, so passing anything with
indeterminate value, other than an unsigned char, to a function is
undefined behavior, as is any other use of the value of such an
object.
It makes no difference whether the object with indeterminate
value is part of a structure,
nor does it make any difference how the
value became indeterminate.



但DR#222< http://www.open- std.org/jtc1/sc22/wg14/www/docs/dr_222.htm>
说:

结构或联合对象的值永远不是一个陷阱
表示,即使结构或
联合对象的成员的值可能是陷阱表示。

这是在TC2和N1124中发布的。



But DR #222 <http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_222.htm>
says:

The value of a struct or union object is never a trap
representation, even though the value of a member of a struct or
union object may be a trap representation.

This was published in TC2 and in N1124.




我不认为与访问释放的指针值相关的未定义行为



与陷阱有任何关系。


-

pete



I don''t think that the undefined behavior
associated with accessing freed pointer values
has anything to do with traps.

--
pete


这篇关于按值传递结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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