By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

fundamental memory clearing question

P: n/a

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
}
}
Nov 2 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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
--

Nov 2 '05 #2

P: n/a
* 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?
Nov 2 '05 #3

P: n/a
lpw

"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...
Nov 2 '05 #4

P: n/a
My main concern was that the structure is going to change in about 20
minutes when I start coding it again, and I didn't want to have to go in and
modify this line of code to accomidate every member variable every time I
add one.

The code that you gave me, lpw, will work, I think. memset is exactly what
I was looking for.

out of curiosity, why did you choose 42 in your sample code? was it an
arbitrary number? or does it have some intrinsic value?
me = c++ newbie


"lpw" <lw*********@hotmail.com> wrote in message
news:Iq******************************@rogers.com.. .

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

Nov 2 '05 #5

P: n/a
* Jason:
[top-posting]
Please don't top-post in this group, read the FAQ.

My main concern was that the structure is going to change in about 20
minutes when I start coding it again, and I didn't want to have to go in and
modify this line of code to accomidate every member variable every time I
add one.

The code that you gave me, lpw, will work, I think. memset is exactly what
I was looking for.


Don't use memset.

It's dangerous and inefficient.

Let C++ do the work for you, e.g. see my previous reply.

--
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?
Nov 2 '05 #6

P: n/a
Jason wrote:
out of curiosity, why did you choose 42 in your sample code? was it an
arbitrary number? or does it have some intrinsic value?


He chose 42 because you said you did not want it set to 0, that, and it
is THE answer.

Nov 2 '05 #7

P: n/a
lpw
> Don't use memset.

It's dangerous and inefficient.


Busted.

*bows head in shame*
Nov 2 '05 #8

P: n/a
is it the efficient way to do it instead of memset? The local obj will
be constructed/destructed again and again?

Nov 2 '05 #9

P: n/a
Could you pl explain why memset is "dangerous and inefficient" for
struct of data?

Nov 2 '05 #10

P: n/a
lpw
<we*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Could you pl explain why memset is "dangerous and inefficient" for
struct of data?


"Dangerous" because it might overwrite the vtable (if a vtable exists).
"Inefficient", well, I think that's highly implementation-dependent and,
therefore, arguable.
Nov 2 '05 #11

P: n/a
I am thinking that I was talking about POD, and I realized that pointer
type may be the problem if the mem allocated need to be released before
reset/re-allocate.

Nov 2 '05 #12

P: n/a
* we*****@yahoo.com:
is it the efficient way to do it instead of memset? The local obj will
be constructed/destructed again and again?


You have two cases.

One, it's a simple structure, a POD, in which case "constructed" amounts
to clearing it in the most efficient way the compiler knows, and
"destructed" amounts to nothing at all.

Two, it's not a simple structure, in which case memset is incorrect,
Undefined Behavior, and may cause Serious Havoc.

--
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?
Nov 2 '05 #13

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
:* 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();

How about the alternative:
Structure s = {0};

To me this C-inherited notation for zero-initialization expresses
clearly that Structure is a simple aggregate of values that will
be initialized to zero. So I tend to prefer it.

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Nov 3 '05 #14

P: n/a
* Ivan Vecerina:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:43*****************@news.individual.net...
:* 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();

How about the alternative:
Structure s = {0};

To me this C-inherited notation for zero-initialization expresses
clearly that Structure is a simple aggregate of values that will
be initialized to zero. So I tend to prefer it.


Yes, that's what I wrote. ;-) "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?
Nov 3 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.