Reinitialize Structure Object

M

Mike Copeland

How can I reinitialize all the values in the structure object below?
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error. So that isn't the answer... 8<{{
Please advise. TIA

struct TestStructure
{
int aa, bb, cc, dd;
} myTest = { 0 };
 
F

Francesco S. Carta

[email protected] (Mike Copeland) said:
   How can I reinitialize all the values in the structure object below?  
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
   I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
   I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error.  So that isn't the answer... 8<{{
   Please advise.  TIA

struct TestStructure
{
    int aa, bb, cc, dd;

} myTest = { 0 };

Hi there, try adding a constructor and initialize the data members
there - you just need to add one simple line.
 
F

Francesco S. Carta

Victor Bazarov said:
    How can I reinitialize all the values in the structure object below?

You can't reinitialise anything.  You can initialise it or you can
assign to it.
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.

Huh?  What do you mean?  Assign an empty struct to it

    void foo(TestStructure &rTest) {
      ...
      rTest = TestStructure();
      ...
    I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).

Yes, you can.  Your struct is a POD class, you are allowed to use
'memmove' with it, if that's what you prefer.
    I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error.  So that isn't the answer... 8<{{
    Please advise.  TIA

See above.


struct TestStructure
{
     int aa, bb, cc, dd;
} myTest = { 0 };

oops... freaking GG, I did't see that last line, hence I didn't
realize the OP had a first TestStructure instance initialized to 0...

The method suggested here (i.e. assigning an empty structure to an
existing one) will not do what the OP is looking for (if I got the
question right) because it needs an appropriate constructor that
initializes the data members of all new instances of that struct.
 
F

Francesco S. Carta

Francesco S. Carta said:
Victor Bazarov said:
On 6/18/2010 7:26 PM, Mike Copeland wrote:
You can't reinitialise anything.  You can initialise it or you can
assign to it.
Huh?  What do you mean?  Assign an empty struct to it
    void foo(TestStructure &rTest) {
      ...
      rTest = TestStructure();
      ...
Yes, you can.  Your struct is a POD class, you are allowed to use
'memmove' with it, if that's what you prefer.
    I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error.  So that isn't the answer... 8<{{
    Please advise.  TIA
See above.

oops... freaking GG, I did't see that last line, hence I didn't
realize the OP had a first TestStructure instance initialized to 0...

The method suggested here (i.e. assigning an empty structure to an
existing one) will not do what the OP is looking for (if I got the
question right) because it needs an appropriate constructor that
initializes the data members of all new instances of that struct.

Forget about it, C is definitely not my language :)
 
M

Mike Copeland

[email protected] (Mike Copeland) said:
How can I reinitialize all the values in the structure object below
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
I also tried to make an assignment like the declaration [ myTest {
0 }; ], but I got a compiler error. =A0So that isn't the answer... 8<{{
Please advise. =A0TIA

struct TestStructure
{
int aa, bb, cc, dd;
} myTest =3D { 0 };

Hi there, try adding a constructor and initialize the data members
there - you just need to add one simple line.

Sorry, I'm missing something: "constructor for a struct"? I don't
know how to do such a thing (which I assumed only applies to a class
definition). Please explain...
 
M

Mike Copeland

How can I reinitialize all the values in the structure object below?
You can't reinitialise anything. You can initialise it or you can
assign to it.


Huh? What do you mean? Assign an empty struct to it

void foo(TestStructure &rTest) {
...
rTest = TestStructure();
...

Not sure that can work, as several different subprograms need to
access the same data throughout their processing. I require that the
structure object be global data (I know, that's frowned upon, but that's
how I do it...), so passing referencing and having instantiated
object(s) doesn't do what I want.
I'm just looking for a simple way to clear a structure (of common
counters) each time I engage various functions in a menu-driven program.
Sloppy and somewhat kludgey, but that's how it is... 8<{{
 
F

Francesco S. Carta

[email protected] (Mike Copeland) said:
[email protected] (Mike Copeland) said:
   How can I reinitialize all the values in the structure object below
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
   I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
   I also tried to make an assignment like the declaration [ myTest {
0 }; ], but I got a compiler error. =A0So that isn't the answer... 8<{{
   Please advise. =A0TIA
struct TestStructure
{
   int aa, bb, cc, dd;
} myTest =3D { 0 };
Hi there, try adding a constructor and initialize the data members
there - you just need to add one simple line.

   Sorry, I'm missing something: "constructor for a struct"?  I don't
know how to do such a thing (which I assumed only applies to a class
definition).  Please explain...

Well, you don't really need that, since it's a POD and you have it
initialized to 0, you just need to assign a new TestStructure to an
existing one, and the existing one will be "reset" to 0, as Victor
pointed out (something I didn't recall about, actually, those are the
bits that someone beginning from C++ and going back to C can happen to
forget).

Anyway, a struct isn't any different from a class in C++, only, its
members are public by default, while class' members are private by
default.

A constructor for your struct would then look like simply like this:

struct TestStructure {
int aa, bb, cc, dd;
TestStructure() : aa(0), bb(0), cc(0), dd(0) {}
};
 
F

Francesco S. Carta

[email protected] (Mike Copeland) said:
   Not sure that can work, as several different subprograms need to
access the same data throughout their processing.  I require that the
structure object be global data (I know, that's frowned upon, but that's
how I do it...), so passing referencing and having instantiated
object(s) doesn't do what I want.
   I'm just looking for a simple way to clear a structure (of common
counters) each time I engage various functions in a menu-driven program.  
Sloppy and somewhat kludgey, but that's how it is... 8<{{

Well, just for the records, have a look to the C++ FAQ, it will be a
good read and it will clear out a lot of problems - I think I should
go back there myself and reread some paragraph, that wouldn't do me
any harm ;-)
 
F

Francesco S. Carta

[email protected] (Mike Copeland) said:
   Not sure that can work, as several different subprograms need to
access the same data throughout their processing.  I require that the
structure object be global data (I know, that's frowned upon, but that's
how I do it...), so passing referencing and having instantiated
object(s) doesn't do what I want.
   I'm just looking for a simple way to clear a structure (of common
counters) each time I engage various functions in a menu-driven program.  
Sloppy and somewhat kludgey, but that's how it is... 8<{{

Actually, it will work, you just need to do as Victor suggested on
that global object you have, you could also assign {0} to it, and it
should do the trick as well. Only, this is not the C++ style :)

Leaning towards C++ style, you would create something like this:

struct TestStructure {
int aa, bb, cc, dd;
TestStructure() : aa(0), bb(0), cc(0), dd(0) {}
void reset() {
aa = bb = cc = dd = 0;
}
};

Yet, reading the details of your program, there is quite something
more to "port" to the C++ style ;-)
 
F

Francesco S. Carta

Francesco S. Carta said:
Actually, it will work, you just need to do as Victor suggested on
that global object you have, you could also assign {0} to it, and it
should do the trick as well.

No, it won't, OK, did I tell that C is not my language?

:)
Only, this is not the C++ style :)

Leaning towards C++ style, you would create something like this:

struct TestStructure {
    int aa, bb, cc, dd;
    TestStructure() : aa(0), bb(0), cc(0), dd(0) {}
    void reset() {
        aa = bb = cc = dd = 0;
    }

};

Yet, reading the details of your program, there is quite something
more to "port" to the C++ style ;-)

So then, yes, the FAQ. For both of us.
 
A

Alf P. Steinbach

* Mike Copeland, on 19.06.2010 02:41:
Not sure that can work, as several different subprograms need to
access the same data throughout their processing. I require that the
structure object be global data (I know, that's frowned upon, but that's
how I do it...), so passing referencing and having instantiated
object(s) doesn't do what I want.
I'm just looking for a simple way to clear a structure (of common
counters) each time I engage various functions in a menu-driven program.
Sloppy and somewhat kludgey, but that's how it is... 8<{{

Do you have a C++ compiler?


Cheers,

- Alf
 
A

Alf P. Steinbach

* Mike Copeland, on 19.06.2010 15:07:
Yes, VS6.0 How does that help in this question?

How can a C++ compiler help you determine whether something works?


Cheers & hth.,

- Alf

PS: You will find it beneficial to upgrade to a more modern compiler. They're free.
 
M

Mike Copeland

* Mike Copeland, on 19.06.2010 15:07:
How can a C++ compiler help you determine whether something works?

I assume you mean that I should use it to develop and try test cases,
right? Well, the code I posted was just that, and some code didn't
compile and other code seemed really cumbersome - hence the original
question. What I posted was a subset of a much larger structure, and
the many uses of it that I currently have prompted the query. Because
of this, I have a lot code code I can't easily or safely convert to a
better implementation...even if I clearly understood some of the
concepts being offered here (which in truth I don't).
And part of my difficulty is that I am a very old programmer (70)
who's been programming for 50 years - it's hard adapting to new concepts
(OOP, etc.) and styles. 8<{{
PS: You will find it beneficial to upgrade to a more modern compiler. They're free.

So I hear...my reluctance to do so is based on a fear of conversion
efforts (I have over a million lines of legacy code and libraries) - I
don't know what would be the most practical switch to make (VS8, VS10)
and which wouldn't impose any significant environment changes (.NET
requirements, distribution issues, etc.) on me and my client. To date,
I haven't found any compelling reasons - such as stuff that doesn't work
or performance issues - which force me to upgrade.
I appreciate the advice and information I receive from the gurus
here, but some of it is prohibitively difficult for me to understand and
apply. 8<{{
 
F

Francesco S. Carta

[email protected] (Mike Copeland) said:
   I assume you mean that I should use it to develop and try test cases,
right?  Well, the code I posted was just that, and some code didn't
compile and other code seemed really cumbersome - hence the original
question.  What I posted was a subset of a much larger structure, and
the many uses of it that I currently have prompted the query.  Because
of this, I have a lot code code I can't easily or safely convert to a
better implementation...even if I clearly understood some of the
concepts being offered here (which in truth I don't).

Then by all means feel free to ask for details :)
   And part of my difficulty is that I am a very old programmer (70)
who's been programming for 50 years - it's hard adapting to new concepts
(OOP, etc.) and styles.  8<{{

Well, at least you could straighten out somebody's manners here :)
(social ones, not programming ones, I mean)

On my part, for the (few) things I know for sure about C++, I'd be
glad to help you getting a better grip on the basics - eventually, you
could share your C experience and approaches, which can't do any harm
if clearly used as a comparing reference... that could help me
settingbetter to mind some of the C aspects of C++ - for example -
that could be a mutual help.

All the best,
Francesco
 
A

Alf P. Steinbach

* Mike Copeland, on 19.06.2010 20:18:
I assume you mean that I should use it to develop and try test cases,
right?

That too.

And in your case, with large existing code base that seems to be tied to a very
old pre-standard compiler, having reproducible tests is very important.

But I was thinking primarily of just testing Victor's advice, which you
expressed some doubts about ("not sure that can work").

Indeed it works with a modern compiler:

TestStructure myGlobal;

void foo()
{
// "Reinitialize" myGlobal:
myGlobal = TestStructure();
}

It's that simple.

But MSVC 6.0 just may suprise you by doing the wrong thing (if it does it will
always do), and so testing is a good idea.


[snip]
And part of my difficulty is that I am a very old programmer (70)
who's been programming for 50 years - it's hard adapting to new concepts
(OOP, etc.) and styles. 8<{{

To quote myself about what OOP is:

Instead of common functions that have knowledge of all relevant types of
objects, use objects that have knowledge of all relevant type-specific
functions.

That's it.

But as with a simple-minded little creature that follows a fixed very simple
algorithm for avoiding obstacles (e.g., collision? back off two steps and turn
right), and that is placed on a field with stones and trees and stuff, the
result of applying OOP to a complicated real problem can seem to be pretty
complex. On the bright side, though, the whole point is that it will be less
complex and easier to maintain than a non-OOP solution. Because it centralizes
knowledge, i.e., it reduces redundancy, which is a main cause of complexity.

So I hear...my reluctance to do so is based on a fear of conversion
efforts (I have over a million lines of legacy code and libraries) - I
don't know what would be the most practical switch to make (VS8, VS10)
and which wouldn't impose any significant environment changes (.NET
requirements, distribution issues, etc.) on me and my client.

Your fear is well-founded: it is a non-trivial task to convert such code.

Hence, you're not alone in being stuck with an old compiler!

I think I would attack such a problem by using both the old and new compiler,
the old one for yet-to-be-converted libraries, and the new one for initially
just a dummy 'main' and then for more and more. However, I'd try that out on
something made-up and much smaller first, just to be sure the approach is valid.
And as you can infer from that "I think" that I haven't done such a large scale
conversion, so it's purely my initial thoughts. E.g., I'm pretty sure that it's
impractical to automate very much of it, although that approach can work for
some other languages like Python 2.x -> 3.x conversion. So I'd go for the
gradual one-library-manually-at-a-time approach.

To date,
I haven't found any compelling reasons - such as stuff that doesn't work
or performance issues - which force me to upgrade.

Being able to use modern libraries such as Boost is pretty compelling... ;-)


Cheers & hth.,

- Alf
 
H

Helge Kruse

Mike Copeland said:
How can I reinitialize all the values in the structure object below?
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error. So that isn't the answer... 8<{{
Please advise. TIA

struct TestStructure
{
int aa, bb, cc, dd;
} myTest = { 0 };

If you use TestStructure at several places and need to initialize all
members at one place, you could do this in the C style:

TestStructure myTest = { 0 }; // intialization not important to this answer.
TestStructure myOther;

....

// "reinitialize" myTest
memset(&myTest, 0, sizeof(myTest));


Helge
 
A

Alf P. Steinbach /Usenet

* Helge Kruse, on 21.06.2010 13:51:
Mike Copeland said:
How can I reinitialize all the values in the structure object below?
This structure object (myTest) is common to several subprograms in a
larger program, and when I engage any of the subprograms I want to
assure that the structure is (re)initialized to the zero value I've
defined in its declaration.
I know that the size of the object is 16 bytes (on my machine), but I
sorta doubt that a memmove of 16 bytes of 0 is an appropriate way to do
this (UB?), and since the elements are not an array I can't initialize
it iteratively (or can I?).
I also tried to make an assignment like the declaration [ myTest = {
0 }; ], but I got a compiler error. So that isn't the answer... 8<{{
Please advise. TIA

struct TestStructure
{
int aa, bb, cc, dd;
} myTest = { 0 };

If you use TestStructure at several places and need to initialize all
members at one place, you could do this in the C style:

TestStructure myTest = { 0 }; // intialization not important to this answer.
TestStructure myOther;

...

// "reinitialize" myTest
memset(&myTest, 0, sizeof(myTest));

myTest = TestStructure();


Cheers & hth.,

- Alf
 
H

Helge Kruse

Alf P. Steinbach /Usenet said:
myTest = TestStructure();

This creates an temporary TestSturcture instance and calls the copy
constructor.
Since the OP obviuously asked an POD, that's ok for execution time. But with
large structures you may keep in mind that you need storage twice on the
stack.

When you have not a POD but a class it must be designed to be copy-enabled.
This can be an additional requirement only for this way of reinitialization.
I would prefer an Initialize() method that is called by the constructor and
can be called else where if needed.

Helge
 
A

Alf P. Steinbach /Usenet

* Helge Kruse, on 21.06.2010 16:15:
This creates an temporary TestSturcture instance and calls the copy
constructor.

No, there is no copy constructor call involved.

It has /the same effect/ as creating a temporary TestStructure,
value-initializing that structure, and calling the copy assignment operator.

For a POD value-initialization reduces to zero-initialization.

Whether an actual assignment (copying) is performed is a quality of
implementation issue; a good compiler may just call memset, but with the
difference that the source code is now safe.

Whether stack space is used is a quality of implementation issue.

Since the OP obviuously asked an POD, that's ok for execution time. But with
large structures you may keep in mind that you need storage twice on the
stack.

No, that's a quality of implementation issue.

When you have not a POD but a class it must be designed to be copy-enabled.
This can be an additional requirement only for this way of reinitialization.

If the type does not support copying then a memset will probably be doing The
Wrong Thing.

Keep in mind that a program can be made arbitrarily fast if it does not need to
be correct, e.g., by simply doing nothing.

Anyway it's generally misguided to try to hack one's way around an intentional
design.

I would prefer an Initialize() method that is called by the constructor and
can be called else where if needed.

That's both inefficient and needlessly unconventional <g>.

A constructor deals with raw, uninitialized memory, e.g. nothing needs to be
deallocated. Assignment deals with initialized stuff, where e.g. something may
need to be deallocated. An Initialize method that could be called later would
likewise have to deal with and have as basic assumption, initialized stuff.

And that means (1) inefficiency of construction, because the constructor would
first have to default initialize and then call Initialize, and (2) that
Initialize plays the role of a special-cased copy assignment operator.

So why not use the copy assignment operator for assignment (that's what it's /for/)?

And for construction you really don't want to get into the habit of expressing
construction in terms of assignment, if you can help it. It yields extreme
redundance, subtleties of multiple initializations where bugs will creep in (not
to mention inefficiency), and is generally not exception safe. The conventional
way is instead to express assignment in terms of construction.


Cheers & hth.,

- Alf
 

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,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top