fundamental memory clearing question

  • Thread starter Stefan =?UTF-8?B?TsOkd2U=?=
  • Start date
S

Stefan =?UTF-8?B?TsOkd2U=?=

Jason said:
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) = 0; // set this byte to null
}
}


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


Stefan
--
 
J

Jason

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) = 0; // set this byte to null
}
}
 
A

Alf P. Steinbach

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

lpw

Jason said:
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) = 0; // set this byte to null
}
}


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

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

Jason

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 said:
Jason said:
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) = 0; // set this byte to null
}
}


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

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

Alf P. Steinbach

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

Josh Mcfarlane

Jason said:
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.
 
W

wenmang

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

lpw

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

wenmang

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

Alf P. Steinbach

* (e-mail address removed):
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.
 
I

Ivan Vecerina

:* 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
 
A

Alf P. Steinbach

* Ivan Vecerina:
:* 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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top