一个'memset'问题: [英] A 'memset' question:

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

问题描述

我遇到了一个关于memset的问题,现在不知道。任何人都可以给b $ b一个线索吗?谢谢


memset有时用于初始化构造函数中的数据,如下面的

示例。初始化这种方式有什么好处?在这个例子中是否可以使用
?它一般有效吗?这是一个好主意吗

一般?


A级{

公开:

A ();

私人:

int a;

浮动f;

char str [35];

长* lp;

};


A :: A()

{

memset(this,0,sizeof(* this));

}

I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks

memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it
work in this example? Does it work in general ? Is it a good idea in
general?

class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};

A::A()
{
memset(this, 0, sizeof(*this));
}

推荐答案

在文章< 11 ********************** @ m79g2000cwm.googlegroups .com> ;,
vo ******** @ gmail.com 写道:
In article <11**********************@m79g2000cwm.googlegroups .com>,
vo********@gmail.com wrote:

I遇到了关于memset的问题,现在不知道。任何人都可以给b $ b一个线索吗?谢谢


memset有时用于初始化构造函数中的数据,如下面的

示例。初始化这种方式有什么好处?在这个例子中是否可以使用
?它一般有效吗?这是一个好主意吗

一般?


A级{

公开:

A ();

私人:

int a;

浮动f;

char str [35];

长* lp;

};


A :: A()

{

memset(this,0,sizeof(* this));

}
I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks

memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it
work in this example? Does it work in general ? Is it a good idea in
general?

class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};

A::A()
{
memset(this, 0, sizeof(*this));
}



这些都是好问题。根据您对memset的理解,了解答案。


例如:


void foo(int * array,int length){

for(int i = 0; i< length; ++ i)

array [i] = 0;

}


void foo(int * array,int length){

memset(array,0,length); < br $>
}


在这里使用memset有什么好处?它有效吗?

Those are good questions. Take a guess at the answers based on your
understanding on what memset does.

For example:

void foo( int* array, int length ) {
for ( int i = 0; i < length; ++i )
array[i] = 0;
}

void foo( int* array, int length ) {
memset( array, 0, length );
}

What is the benefit of using memset here? Does it work?




< vo ******** @ gmail.comskrev i meddelandet

news:11 ********************** @ m79g2000cwm.googlegr oups.com ...

<vo********@gmail.comskrev i meddelandet
news:11**********************@m79g2000cwm.googlegr oups.com...

>我遇到了一个关于memset的问题,现在不知道。可以
任何人

给出一个线索?谢谢


memset有时用于初始化构造函数中的数据,如



示例如下。初始化这种方式有什么好处?



在这个例子中工作了吗?它一般有效吗?这是个好主意

一般吗?
>I met a question about memset and have no idea right now. Could
anybody
give a clue? Thanks

memset is sometimes used to initialize data in a constructor like
the
example below. What is the benefit of initializing this way? Does
it
work in this example? Does it work in general ? Is it a good idea
in
general?



无,不,不,不。 :-)

None, no, no, and no. :-)


>

A级{

public:

A();

私人:

int a;

浮动f;

char str [35];

长* lp;

};


A :: A()

{

memset(this,0,sizeof(* this));

}
>
class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};

A::A()
{
memset(this, 0, sizeof(*this));
}



使用memset函数在C中初始化结构(或者可能是
数组),因为C没有构造函数。


在C ++中它仍然允许C类对象(POD),但不是

其他任何东西。你的类有一个构造函数排除

使用memset的事实。使用多个访问说明符也是如此。


如果需要初始化,请使用构造函数并信任编译器

来优化代码。如果你只是把所有东西都归零,那么编译器相当于memset的几率很高




Bo Persson

The memset function is used in C to initialize structs (or possibly
arrays), because C doesn''t have constructors.

In C++ it is still allowed for C like objects (PODs), but not for
anything else. The fact that your class has a constructor rules out
the use of memset. The use of multiple access specifiers does too.

If you need initialization, use a constructor and trust the compiler
to optimize the code. If you just zero everything, the odds are high
that the compiler will do the equivalent of a memset anyway.

Bo Persson


志愿者... @ gmail.com写道:
volunte...@gmail.com wrote:

我遇到了一个关于memset的问题,现在不知道。任何人都可以给b $ b一个线索吗?谢谢


memset有时用于初始化构造函数中的数据,如下面的

示例。初始化这种方式有什么好处?在这个例子中是否可以使用
?它一般有效吗?这是一个好主意吗

一般?


A级{

公开:

A ();

私人:

int a;

浮动f;

char str [35];

长* lp;

};


A :: A()

{

memset(this,0,sizeof(* this));

}
I met a question about memset and have no idea right now. Could anybody
give a clue? Thanks

memset is sometimes used to initialize data in a constructor like the
example below. What is the benefit of initializing this way? Does it
work in this example? Does it work in general ? Is it a good idea in
general?

class A {
public:
A();
private:
int a;
float f;
char str[35];
long *lp;
};

A::A()
{
memset(this, 0, sizeof(*this));
}



在C ++中使用memset通常泄气。没有问题

初始化POD结构和数组(事实上,一些memset

实现是最有效的方法),但当你

来到非POD类,可能有虚拟表(或其他一些
虚拟机制),它可能会造成灾难。请参阅此常见问题解答:

http://www.parashift.com/c++-faq-lit....html#faq-20.4


话虽这么说,你的例子可能有用。这只是一种危险的

技术,因此最好避免使用。更喜欢使用初始化列表

http://www.parashift.com/c++-faq-lit....html#faq-10.6

可能和其他技术不同时。例如,我会像这样重写

你的构造函数:


A :: A()

:a(0)

,f(0)

,lp(0)

{

str [0] = 0;

//或者如果你想要整个数组:

// std :: fill(str,str +(sizeof(str)/ sizeof(char)) ,0);

//或

// std :: memset(str,0,sizeof(str)/ sizeof(char));

}

干杯! --M

Using memset in C++ is generally discouraged. There''s no problem
initializing POD structs and arrays (in fact, some memset
implementations are the most efficient way to do that), but when you
come to non-POD classes that could have virtual tables (or some other
virtual mechanism), it could spell disaster. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-20.4

That being said, your example probably works. It''s just a dangerous
technique and so is best avoided. Prefer using initialization lists
(http://www.parashift.com/c++-faq-lit....html#faq-10.6) when
possible and other techniques when not. For instance, I would rewrite
your constructor like this:

A::A()
: a( 0 )
, f( 0 )
, lp( 0 )
{
str[0] = 0;
// Or if you want to inialize the whole array:
//std::fill( str, str + (sizeof(str)/sizeof(char)), 0 );
// or
//std::memset( str, 0, sizeof(str)/sizeof(char) );
}
Cheers! --M


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

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