基本记忆清除问题 [英] fundamental memory clearing question

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

问题描述



嗨。


我有一个在循环的前一次迭代中使用的结构,我

想要清除前一次迭代所分配的所有结构值,而不是将每个变量赋值为0.


我想到了几个方法要做到这一点,但它们都是环形的...

有谁知道更好的方法吗?


这是一个样子,我来的样子可以使用。

void clear_the_structure()//没有参数,因为结构是extern

{

for(i = 0; i< sizeof (mystructure); i ++)//对于每个字节中的
结构'的内存

{

((unsigned char)mystructure)[ i] = 0; //将此字节设为空

}

}

解决方案

Jason写道:


你好。

我有一个在循环的前一次迭代中使用的结构,我想要清除上一次迭代分配的所有结构值,而不是将每个变量赋值为0.

我想过几种方法可以做到这一点,但它们都是迂回的......
有没有人知道更好的方法呢?

这是我想出的一个样例。

void clear_the_structure()//没有参数因为结构是extern
{
对于(i = 0; i< sizeof(mystructure); i ++)//对于
结构的内存中的每个字节
{
((unsigned char)mystructure)[i] = 0; //将此字节设置为空
}
}




请不要告诉我们更多关于''mystructure''的信息。 。

它是POD吗?

Stefan

-


< blockquote> * Jason:



我有一个在循环的前一次迭代中使用的结构,我想清除所有结构值在没有将每个变量赋值为0的情况下分配上一次迭代。




使循环的结构本地化,并对其进行零初始化。


while(stillLooping)

{

结构s =结构();

...

}


如果Structure是POD,可以更短。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usen上最烦人的事情是什么et和电子邮件?




" Jason" < JA *** @ acd.net>在消息中写道

新闻:ae ****************************** @ giganews.com .. 。


你好。

我有一个在循环的前一次迭代中使用的结构,我想要清除所有的结构上一次迭代分配的值,而不是将每个变量赋值为0.

我想过几种方法可以做到这一点,但它们都是迂回的...
这是我想出的一个样例。

void clear_the_structure()//没有参数,因为结构是extern
{
对于(i = 0; i< sizeof(mystructure); i ++)//对于
结构的内存中的每个字节
{
((unsigned char)mystructure)[i] = 0; //将此字节设置为空
}
}



memset(& mystructure,42,sizeof(mystructure)) ;


您确实说过您不想将每个变量设置为零......



Hi.

I have a structure that was used in a previous iteration of a loop, and I
want to clear out all the structures values that the previous iteration
assigned without assigning every variable to be 0.

I''ve thought of a few ways to do this, but they are all roundabout...
Does anyone know of a better way to do this?

Heres a sample idea that I came up with.
void clear_the_structure() // no parameters because structure is extern
{
for (i=0; i < sizeof(mystructure); i++) // for each byte in
structure''s memory
{
((unsigned char)mystructure)[i] = 0; // set this byte to null
}
}

解决方案

Jason wrote:


Hi.

I have a structure that was used in a previous iteration of a loop, and I
want to clear out all the structures values that the previous iteration
assigned without assigning every variable to be 0.

I''ve thought of a few ways to do this, but they are all roundabout...
Does anyone know of a better way to do this?

Heres a sample idea that I came up with.
void clear_the_structure() // no parameters because structure is extern
{
for (i=0; i < sizeof(mystructure); i++) // for each byte in
structure''s memory
{
((unsigned char)mystructure)[i] = 0; // set this byte to null
}
}



Please, don''t tell us more about ''mystructure''...
Is it a POD ?
Stefan
--


* Jason:



I have a structure that was used in a previous iteration of a loop, and I
want to clear out all the structures values that the previous iteration
assigned without assigning every variable to be 0.



Make the structure local to the loop, and zero-initialize it.

while( stillLooping )
{
Structure s = Structure();
...
}

which can be shorter if Structure is a POD.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Jason" <ja***@acd.net> wrote in message
news:ae******************************@giganews.com ...


Hi.

I have a structure that was used in a previous iteration of a loop, and I
want to clear out all the structures values that the previous iteration
assigned without assigning every variable to be 0.

I''ve thought of a few ways to do this, but they are all roundabout...
Does anyone know of a better way to do this?

Heres a sample idea that I came up with.
void clear_the_structure() // no parameters because structure is extern
{
for (i=0; i < sizeof(mystructure); i++) // for each byte in
structure''s memory
{
((unsigned char)mystructure)[i] = 0; // set this byte to null
}
}



memset(&mystructure, 42, sizeof(mystructure));

You did say you don''t want to set every variable to zero...


这篇关于基本记忆清除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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