memset怀疑 [英] memset doubt

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

问题描述




有人可以解释一个明确的类型转换是否是必需的,而

为一个结构调用memset函数?比如下面的代码

快照.....

struct some_structure x;

memset((some_structure *)& x,0 ,sizeof(some_structure));

将memset(& x,0,sizeof(some_structure));引起一些问题?


提前致谢

Srivatsan

解决方案

srivatsan_b写道:



有人可以解释在为结构调用memset函数时是否必须使用显式类型转换?比如下面的代码
快照.....
struct some_structure x;
memset((some_structure *)& x,0,sizeof(some_structure));
将memset的(安培; X,0,的sizeof(SOME_STRUCTURE));导致一些问题?




稍微好一点是... memset(& x,0,sizeof x);


在C中,有一个问题,但这不是你要问的问题。


在C中,任何对象指针都可以隐式转换为指针

无效[受限于cv-qualifiers。]所以你问题的答案

是没有的。 C ++是不同的,但你应该在C ++组中询问是否实际使用的是C ++编译器。


你可能遇到的问题是设置所有

结构内容的位为零,可能无法做到你想要的。如果您的

结构包含例如指针,那么将位设置为

0不需要将它们设置为空指针。


如果你想初始化一个结构(或者实际上任何其他的
对象类型),那么你只需要做...


struct some_structure x = {0};


不需要memset。


-

彼得


" srivatsan_b" < SR ********* @ gmail.com>写道:

有人可以解释在为结构调用memset函数时是否必须使用显式类型转换?比如下面的代码
快照.....
struct some_structure x;
memset((some_structure *)& x,0,sizeof(some_structure));
将memset的(安培; X,0,的sizeof(SOME_STRUCTURE));引起一些问题?




假设你已经记住了#include< string.h>,它宣布了

memset( )功能,演员是不必要的。任何对象指针

类型都可以隐式转换为void *,反之亦然。


您的代码存在一些问题,一个是严重的,一个是

化妆品。


如果声明struct some_structure,则没有类型

被称为some_structure; struct关键字是必要的,除非你已将
声明为typedef。 (有人会认为

结构类型的typedef风格很差。)


< OT>

注意规则对于C ++来说是不同的。如果我没记错的话,

声明struct some_structure使some_structure可见

作为类型名称,并且没有隐式转换为void *。

除非你使用的是C ++编译器,否则不应该是相关的 - 如果你是b $ b,那么你就是错误的新闻组。 />
< / OT>


另外,使用对象的大小比使用

类型的大小更清晰。例如:


struct some_structure x;

memset(& x,0,sizeof x);


这不仅短,而且如果x的类型改变,它可以避免错误。


-

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

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

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


2005年8月30日22:43:54 -0700," srivatsan_b" < sr ********* @ gmail.com>

写道:



可以有人解释在为结构调用memset函数时是否必须使用显式类型转换?比如下面的代码
快照.....
struct some_structure x;
memset((some_structure *)& x,0,sizeof(some_structure));
将memset的(安培; X,0,的sizeof(SOME_STRUCTURE));导致一些问题?




首先,& x已经有了指向struct some_structure的类型指针。

因此,将任何值转换为相同的类型它已经有了

定义多余。


其次,memset期望第一个参数的类型指针为

void。如果你想要施放价值,那就是你要把它投给它。


第三,指向无效的指针是兼容的。与任何其他不合格的

对象指针类型。这意味着编译器可以隐式地将
转换为另一个,而不需要

演员。


你的最后一个问题唯一的问题是,对于C89编译器来说,演员必须包含关键字struct。使用没有关键字的结构

标签在C99之前没有成为标准,并且

很少使用该等级的编译器。

<<删除电子邮件的del>>


Hi,

Can somebody explain whether an explicit typecast is mandatory while
calling memset function for a structure? like in the following code
snapshot.....
struct some_structure x;
memset((some_structure*)&x,0,sizeof(some_structure ));
Will memset(&x,0,sizeof(some_structure)); cause some issues?

Thanks in advance
Srivatsan

解决方案

srivatsan_b wrote:

Hi,

Can somebody explain whether an explicit typecast is mandatory while
calling memset function for a structure? like in the following code
snapshot.....
struct some_structure x;
memset((some_structure*)&x,0,sizeof(some_structure ));
Will memset(&x,0,sizeof(some_structure)); cause some issues?



Slightly better is... memset(&x, 0, sizeof x);

In C, there is an issue, but it''s not the issue you''re asking about.

In C, any object pointer can be implicitly converted to a pointer
to void [subject to cv-qualifiers.] So the answer to your question
is no. C++ is different, but you should ask in a C++ group if you
ere actually using a C++ compiler.

Where you may have problems is that setting all the bits of the
structure contents to zero, may not do what you want. If your
structure contains pointers for instance, then setting the bits to
0 need not set them to null pointers.

If you want to zero initialise a structure (or indeed any other
object type), then you simply need to do...

struct some_structure x = { 0 };

No memset is required.

--
Peter


"srivatsan_b" <sr*********@gmail.com> writes:

Can somebody explain whether an explicit typecast is mandatory while
calling memset function for a structure? like in the following code
snapshot.....
struct some_structure x;
memset((some_structure*)&x,0,sizeof(some_structure ));
Will memset(&x,0,sizeof(some_structure)); cause some issues?



Assuming that you''ve remembered to #include <string.h>, which declares
the memset() function, the cast is unnecessary. Any object pointer
type can be implicitly converted to void*, and vice versa.

There are a couple of problems with your code, one serious and one
cosmetic.

Given a declaration of "struct some_structure", there is no type
called "some_structure"; the struct keyword is necessary unless you''ve
also declared it as a typedef. (Some would argue that typedefs for
struct types are poor style.)

<OT>
Note that the rules are different for C++. If I recall correctly, a
declaration of "struct some_structure" makes "some_structure" visible
as a type name, and there is no implicit conversion to void*. That
shouldn''t be relevant unless you''re using a C++ compiler -- and if you
are, you''re in the wrong newsgroup.
</OT>

Also, using the size of the object makes for cleaner code than using
the size of the type. For example:

struct some_structure x;
memset(&x, 0, sizeof x);

Not only is this shorter, it avoid errors if the type of x is changed.

--
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.


On 30 Aug 2005 22:43:54 -0700, "srivatsan_b" <sr*********@gmail.com>
wrote:

Hi,

Can somebody explain whether an explicit typecast is mandatory while
calling memset function for a structure? like in the following code
snapshot.....
struct some_structure x;
memset((some_structure*)&x,0,sizeof(some_structure ));
Will memset(&x,0,sizeof(some_structure)); cause some issues?



Firstly, &x already has type pointer to struct some_structure.
Therefore, casting any value to the same type it already has is by
definition redundant.

Secondly, memset expects the first argument to have type pointer to
void. If you were going to cast the value, that is what you would
cast it to.

Thirdly, pointer to void is "compatible" with any other unqualified
object pointer type. This means that the compiler can implicitly
convert one to the other whenever it needs to without requiring a
cast.

The only problem with your final question is that for C89 compilers
the cast must include the keyword struct. The use of the structure
tag without the keyword did not become standard until C99 and there
are very few compilers of that grade in use.
<<Remove the del for email>>


这篇关于memset怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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