Thursday, December 31, 2009

comp.lang.c++ - 16 new messages in 5 topics - digest

comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en

comp.lang.c++@googlegroups.com

Today's topics:

* map vs list performance - 8 messages, 5 authors
http://groups.google.com/group/comp.lang.c++/t/f0b7f86d78b91416?hl=en
* C++ jobs down another 40% - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/6718a9cd2f3ecdbf?hl=en
* Learning C++ - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/4ccf5387f1787434?hl=en
* Constructor problem - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/d528665531d6d939?hl=en
* c++ as scripting language - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/8b6a860779bd7319?hl=en

==============================================================================
TOPIC: map vs list performance
http://groups.google.com/group/comp.lang.c++/t/f0b7f86d78b91416?hl=en
==============================================================================

== 1 of 8 ==
Date: Wed, Dec 30 2009 4:42 pm
From: "AnonMail2005@gmail.com"


On Dec 30, 6:17 pm, "barcaroller" <barcarol...@music.net> wrote:
> I have an STL list that contains about 1500 objects.  To check if an object
> exists in my list I iterate over it and check if a "key" matches.  A key is
> just a struct that contains four integers.
>
>     typedef struct
>     {
>         int a;
>         int b;
>         int c;
>         int d;
>     } abcd;
>
>     class C
>     {
>         abcd key;
>         char data[100];
>     }
>
>     for i = list.begin(); i != list.end(); i++)
>         if ( mya == i->key.a && myb == i->key.b &&
>              myc == i->key.c && myd == i->key.d )
>             // object has been found; access data[]
>
> To improve performance, I converted the list to an STL map.  I used the
> struct as the map's key and I added the operator<() and operator==()
> functions to allow the map to sort and find objects.  I also replaced the
> list's for loop with map's find() function.
>
>     if (map.find(mykey) != map.end())
>         // object has been found; access data[]
>
> Everything is working correctly (adding, finding, deleting, etc) but the map
> solution is over 100% slower than the list for searches (say 100,000
> searches).  I understand that a map has some additional overhead but I would
> have expected this overhead to be negligible for 1500 elements.  I can't
> explain why a sequential list search is faster (the found objects are
> randomly dispersed and are not at the beginning of the list).
>
> Has anyone else noticed this performance issue?  Using gcc on Linux.

Sounds fishy. Show the operator< function that you wrote. Also, for
a map, you do not need operator==. You would need that operator if
you used the find algorithm with the list.

HTH


== 2 of 8 ==
Date: Wed, Dec 30 2009 4:52 pm
From: "AnonMail2005@gmail.com"


On Dec 30, 7:42 pm, "AnonMail2...@gmail.com" <anonmail2...@gmail.com>
wrote:
> On Dec 30, 6:17 pm, "barcaroller" <barcarol...@music.net> wrote:
>
>
>
>
>
> > I have an STL list that contains about 1500 objects.  To check if an object
> > exists in my list I iterate over it and check if a "key" matches.  A key is
> > just a struct that contains four integers.
>
> >     typedef struct
> >     {
> >         int a;
> >         int b;
> >         int c;
> >         int d;
> >     } abcd;
>
> >     class C
> >     {
> >         abcd key;
> >         char data[100];
> >     }
>
> >     for i = list.begin(); i != list.end(); i++)
> >         if ( mya == i->key.a && myb == i->key.b &&
> >              myc == i->key.c && myd == i->key.d )
> >             // object has been found; access data[]
>
> > To improve performance, I converted the list to an STL map.  I used the
> > struct as the map's key and I added the operator<() and operator==()
> > functions to allow the map to sort and find objects.  I also replaced the
> > list's for loop with map's find() function.
>
> >     if (map.find(mykey) != map.end())
> >         // object has been found; access data[]
>
> > Everything is working correctly (adding, finding, deleting, etc) but the map
> > solution is over 100% slower than the list for searches (say 100,000
> > searches).  I understand that a map has some additional overhead but I would
> > have expected this overhead to be negligible for 1500 elements.  I can't
> > explain why a sequential list search is faster (the found objects are
> > randomly dispersed and are not at the beginning of the list).
>
> > Has anyone else noticed this performance issue?  Using gcc on Linux.
>
> Sounds fishy.  Show the operator< function that you wrote.  Also, for
> a map, you do not need operator==.  You would need that operator if
> you used the find algorithm with the list.
>
> HTH

The operator< member function should be something like this:

bool abcd::operator<(const abcd & rhs) const
{
bool res = false;
if (a < rhs.a)
{
res = true;
}
else if (a == rhs.a)
{
if (b < rhs.b)
{
res = true;
}
else if (b == rhs.b)
{
if (c < rhs.c)
{
res = true;
}
else if (c == rhs.b)
{
if (d < rhs.d)
{
res = true;
}
}
}
}
return res;
}

HTH


== 3 of 8 ==
Date: Wed, Dec 30 2009 4:54 pm
From: Branimir Maksimovic


AnonMail2005@gmail.com wrote:
> On Dec 30, 6:17 pm, "barcaroller" <barcarol...@music.net> wrote:
>> I have an STL list that contains about 1500 objects. To check if an object
>> exists in my list I iterate over it and check if a "key" matches. A key is
>> just a struct that contains four integers.
>>
>> typedef struct
>> {
>> int a;
>> int b;
>> int c;
>> int d;
>> } abcd;
>>
>> class C
>> {
>> abcd key;
>> char data[100];
>> }
>>
>> for i = list.begin(); i != list.end(); i++)
>> if ( mya == i->key.a && myb == i->key.b &&
>> myc == i->key.c && myd == i->key.d )
>> // object has been found; access data[]
>>
>> To improve performance, I converted the list to an STL map. I used the
>> struct as the map's key and I added the operator<() and operator==()
>> functions to allow the map to sort and find objects. I also replaced the
>> list's for loop with map's find() function.
>>
>> if (map.find(mykey) != map.end())
>> // object has been found; access data[]
>>
>> Everything is working correctly (adding, finding, deleting, etc) but the map
>> solution is over 100% slower than the list for searches (say 100,000
>> searches). I understand that a map has some additional overhead but I would
>> have expected this overhead to be negligible for 1500 elements. I can't
>> explain why a sequential list search is faster (the found objects are
>> randomly dispersed and are not at the beginning of the list).

Because in map, search goes with random access in memory,
and with list it goes in sequential order. I have discovered
that by using bidirectional iterator for quick sort instead of
random access I gain double the speed of quick sort...
because it seems that cpu reads ahead in memory
cache while instructions are executing.
Of course, nodes in list have to be sorted by address.

>>
>> Has anyone else noticed this performance issue? Using gcc on Linux.

Yes. On list where nodes are sorted by address pass through list
is twice as fast as unordered nodes, no matter the size of list
and where nodes are. If nodes are allocated one behind another
you get additional speed boost, perhaps additional double speed.
That's why , search on list on 1500 elements is faster than map,
because probably sequential search goes through level 1 cache
read ahead, which is 10-30 times faster than main memory.

Greets

--
http://maxa.homedns.org/


== 4 of 8 ==
Date: Wed, Dec 30 2009 5:41 pm
From: Horace Cochran


Hi there, did you know GNU time command can be used to clock the runtime of
your executable a.out? For your case, you can write 2 programs each using a
different STL container then run them with 'time a.out'.


== 5 of 8 ==
Date: Wed, Dec 30 2009 7:44 pm
From: Naive Group User


On Dec 31, 6:17 am, "barcaroller" <barcarol...@music.net> wrote:
> I have an STL list that contains about 1500 objects.  To check if an object
> exists in my list I iterate over it and check if a "key" matches.  A key is
> just a struct that contains four integers.
>
>     typedef struct
>     {
>         int a;
>         int b;
>         int c;
>         int d;
>     } abcd;
>
>     class C
>     {
>         abcd key;
>         char data[100];
>     }
>
>     for i = list.begin(); i != list.end(); i++)
>         if ( mya == i->key.a && myb == i->key.b &&
>              myc == i->key.c && myd == i->key.d )
>             // object has been found; access data[]
>
> To improve performance, I converted the list to an STL map.  I used the
> struct as the map's key and I added the operator<() and operator==()
> functions to allow the map to sort and find objects.  I also replaced the
> list's for loop with map's find() function.
>
>     if (map.find(mykey) != map.end())
>         // object has been found; access data[]
>
> Everything is working correctly (adding, finding, deleting, etc) but the map
> solution is over 100% slower than the list for searches (say 100,000
> searches).  I understand that a map has some additional overhead but I would
> have expected this overhead to be negligible for 1500 elements.  I can't
> explain why a sequential list search is faster (the found objects are
> randomly dispersed and are not at the beginning of the list).
>
> Has anyone else noticed this performance issue?  Using gcc on Linux.

Dear Sir,

I have just made a question about speaking skill in sci.math- a very
necessary skill I would like to acquire via your experience Sir.
I have been to an interview as of late for a job of business
consultant and definitely I fail but I am not absolutely in a bad mood
because those interviewers are a little limited in viewpoints and
evaluation process, and admittedly there are things I forget; things I
(sadly) missed during I was at schools but definitely I lack a special
speaking skill that I strongly need to acquire during the next few
weeks or more. Thereby, seeking Sir, The Professor and Bear's advice
as well as experience is a must-do right now. Just a few posts don't
count up the tall pyramids but they are priceless and appriciative
things ever from an online community Sir.

About your question, Sir,
Since built-in functions in those containers are mainly to as
versatile as possible to 'feed' a large number of coders of varied
needs. So, a hefty import of different headers as well as libraries
will be performed during the compiling and linking processes. The
containers inheritted from the base classes i.e iostream reimplemented
several to many virtual functions to expose their (aka) polymorphism
for current and future uses with the templatizing capabilities. Once
the objects are assigned to each placeholder of the list which is more
limited in functionalities performed in terms of first-key-second-
value pairwise implementation in comparison to map container. I think
that is why it is much faster than the map in relation to runtime
speed.

== 6 of 8 ==
Date: Wed, Dec 30 2009 7:50 pm
From: Naive Group User


On Dec 31, 10:44 am, Naive Group User <imuaple...@gmail.com> wrote:
> On Dec 31, 6:17 am, "barcaroller" <barcarol...@music.net> wrote:
>
>
>
>
>
> > I have an STL list that contains about 1500 objects.  To check if an object
> > exists in my list I iterate over it and check if a "key" matches.  A key is
> > just a struct that contains four integers.
>
> >     typedef struct
> >     {
> >         int a;
> >         int b;
> >         int c;
> >         int d;
> >     } abcd;
>
> >     class C
> >     {
> >         abcd key;
> >         char data[100];
> >     }
>
> >     for i = list.begin(); i != list.end(); i++)
> >         if ( mya == i->key.a && myb == i->key.b &&
> >              myc == i->key.c && myd == i->key.d )
> >             // object has been found; access data[]
>
> > To improve performance, I converted the list to an STL map.  I used the
> > struct as the map's key and I added the operator<() and operator==()
> > functions to allow the map to sort and find objects.  I also replaced the
> > list's for loop with map's find() function.
>
> >     if (map.find(mykey) != map.end())
> >         // object has been found; access data[]
>
> > Everything is working correctly (adding, finding, deleting, etc) but the map
> > solution is over 100% slower than the list for searches (say 100,000
> > searches).  I understand that a map has some additional overhead but I would
> > have expected this overhead to be negligible for 1500 elements.  I can't
> > explain why a sequential list search is faster (the found objects are
> > randomly dispersed and are not at the beginning of the list).
>
> > Has anyone else noticed this performance issue?  Using gcc on Linux.
>
> Dear Sir,
>
> I have just made a question about speaking skill in sci.math- a very
> necessary skill I would like to acquire via your experience Sir.
> I have been to an interview as of late for a job of business
> consultant and definitely I fail but I am not absolutely in a bad mood
> because those interviewers are a little limited in viewpoints and
> evaluation process, and admittedly there are things I forget; things I
> (sadly) missed during I was at schools but definitely I lack a special
> speaking skill that I strongly need to acquire during the next few
> weeks or more. Thereby, seeking Sir, The Professor and Bear's advice
> as well as experience is a must-do right now. Just a few posts don't
> count up the tall pyramids but they are priceless and appriciative
> things ever from an online community Sir.
>
> About your question, Sir,
> Since built-in functions in those containers are mainly to as
> versatile as possible to 'feed' a large number of coders of varied
> needs. So, a hefty import of different headers as well as libraries
> will be performed during the compiling and linking processes. The
> containers inheritted from the base classes i.e iostream reimplemented
> several to many virtual functions to expose their (aka) polymorphism
> for current and future uses with the templatizing capabilities. Once
> the objects are assigned to each placeholder of the list which is more
> limited in functionalities performed in terms of first-key-second-
> value pairwise implementation in comparison to map container. I think
> that is why it is much faster than the map in relation to runtime
> speed.- Hide quoted text -
>
> - Show quoted text -

Ohhh I read what i wrote and I don't understand what I wrote too :-D

I am Sorry Sir but
I am have been online too long
I better go now

Next time next better
Remember me, remember you


== 7 of 8 ==
Date: Wed, Dec 30 2009 8:01 pm
From: "barcaroller"

"AnonMail2005@gmail.com" <anonmail2005@gmail.com> wrote


> Sounds fishy. Show the operator< function that you wrote. Also, for
> a map, you do not need operator==. You would need that operator if
> you used the find algorithm with the list.
>
>
> The operator< member function should be something like this:
>
> bool abcd::operator<(const abcd & rhs) const
> {
> bool res = false;
> if (a < rhs.a)
> {
> res = true;
> }
> else if (a == rhs.a)
> {
> if (b < rhs.b)
> {
> res = true;
> }
> else if (b == rhs.b)
> {
> if (c < rhs.c)
> {
> res = true;
> }
> else if (c == rhs.b)
> {
> if (d < rhs.d)
> {
> res = true;
> }
> }
> }
> }
> return res;
> }

Thank you. My operator<() function looks like this:

bool operator<(const key& k)
{
if (a < k.a)
return true;
else
if (a > k.a)
return false;

if (b < k.b)
return true;
else
if (b > k.b)
return false;

if (c < k.c)
return true;
else
if (c > k.c)
return false;

if (d < k.d)
return true;

return false;
}


== 8 of 8 ==
Date: Wed, Dec 30 2009 8:20 pm
From: "barcaroller"

"Branimir Maksimovic" <bmaxa@hotmail.com> wrote:

> Because in map, search goes with random access in memory,
> and with list it goes in sequential order. I have discovered
> that by using bidirectional iterator for quick sort instead of
> random access I gain double the speed of quick sort...
> because it seems that cpu reads ahead in memory
> cache while instructions are executing.
> Of course, nodes in list have to be sorted by address.
>
> Yes. On list where nodes are sorted by address pass through list
> is twice as fast as unordered nodes, no matter the size of list
> and where nodes are. If nodes are allocated one behind another
> you get additional speed boost, perhaps additional double speed.
> That's why , search on list on 1500 elements is faster than map,
> because probably sequential search goes through level 1 cache
> read ahead, which is 10-30 times faster than main memory.

Thank you for that explanation. It would explain what I'm seeing. When I
first familiarized myself with the STL (many years ago) I was led to believe
that map and set searches (as opposed to insertions) are generally faster
than list searches for large containers. The theory behind that reasoning
is well defined. It would indeed be unfortunate if the performance of these
container types depends on the CPU architecture.


==============================================================================
TOPIC: C++ jobs down another 40%
http://groups.google.com/group/comp.lang.c++/t/6718a9cd2f3ecdbf?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Dec 30 2009 5:13 pm
From: tanix@mongo.net (tanix)


In article <hhgrcm$8ue$1@news.xmission.com>, (Richard) legalize+jeeves@mail.xmission.com wrote:
>[Please do not mail me a copy of your followup]
>
>tanix@mongo.net (tanix) spake the secret code
><hh3at8$j6s$1@news.eternal-september.org> thusly:
>
>>I just hate [...]
>
>Yeah, we got that.

Who is WE?

:--}

You are pretty creative at taking it to the levels beyond obscene,
aren't ya?

--
Programmer's Goldmine collections:

http://preciseinfo.org

Tens of thousands of code examples and expert discussions on
C++, MFC, VC, ATL, STL, templates, Java, Python, Javascript,
organized by major topics of language, tools, methods, techniques.


==============================================================================
TOPIC: Learning C++
http://groups.google.com/group/comp.lang.c++/t/4ccf5387f1787434?hl=en
==============================================================================

== 1 of 4 ==
Date: Wed, Dec 30 2009 6:55 pm
From: Harold A. Climer


A friend of mine is moving to California and did not want to move all
his library. He gave me quite a few books on programming using
different computer languages. BASIC,PASCAL, C,C++, etc.
I have had some experience programming in TRUEBASIC.
I would like to learn to program using C or C++.( Not professionally I
am too old for that)
What software do I need?(Something inexpensive) I have two computers.
The one at work is running Vista Home SP1 and the one at home is
running Windows XP MCE SP3.
I have seen some C++ stuff by Borland and Microsoft. I really do not
need the latest software,but I would like to be able for it to work
properly on XP and Vista.
Harold A Climer
Dept. Of Physics Geology, and Astronomy
U.T, Chattanooga
Rm. 406A Engineering, Math & Computer Science Building
615 McCallie Ave. Chattanooga TN 37403
Harold-Cimer@utc.edu


== 2 of 4 ==
Date: Wed, Dec 30 2009 7:33 pm
From: Sam


Harold A. Climer writes:

> A friend of mine is moving to California and did not want to move all
> his library. He gave me quite a few books on programming using
> different computer languages. BASIC,PASCAL, C,C++, etc.
> I have had some experience programming in TRUEBASIC.
> I would like to learn to program using C or C++.( Not professionally I
> am too old for that)
> What software do I need?(Something inexpensive) I have two computers.
> The one at work is running Vista Home SP1 and the one at home is
> running Windows XP MCE SP3.
> I have seen some C++ stuff by Borland and Microsoft. I really do not
> need the latest software,but I would like to be able for it to work
> properly on XP and Vista.

Borland, on this particular subject, is practically dead, monopolized out of
existence, by MSFT.

Somewhere on MSFT's web site you should find a free download of Visual
Studio Express, or something named along those lines. That'll suffice for
beginners. I don't know if MSFT updated their freeware for Vista, but it'll
definitely work on XP.

Full development tools from MSFT are pricy, since developers are a revenue
source for MSFT. However, given that you're @.edu, you should be able to
easily get a rather steep educational discount for the full Visual Studio
kit. MSFT wants mindshare from young skulls full of mush, so they shove
their wares into .edus for peanuts.

The microsoft.* usenet groups would be your most appropriate channel for
getting help for MS development tools. comp.lang.c++ is purely for
language-specific topics, which are independent of any vendor.

== 3 of 4 ==
Date: Wed, Dec 30 2009 8:09 pm
From: "dragan"


Harold A. Climer wrote:
> A friend of mine is moving to California and did not want to move all
> his library.

Wow, I CAN relate!! And who is he that KNOWS C++ cannot?! An appropriate
post for the season (if there is season for such) "lament"?. I will pose the
question as a separate thread.

> He gave me quite a few books on programming using
> different computer languages. BASIC,PASCAL, C,C++, etc.
> I have had some experience programming in TRUEBASIC.
> I would like to learn to program using C or C++.( Not professionally I
> am too old for that)

That is relevant, more than you think maybe.

> What software do I need?(Something inexpensive)

You need: inquisitiveness, desire, drive (a purpose? an alterior
motive?).... The software is pretty much irrelevant. Though I can recommend
the MS free compilers (at least the behemoths gave SOMEthing back!).

> I have two computers.
> The one at work is running Vista Home SP1 and the one at home is
> running Windows XP MCE SP3.
> I have seen some C++ stuff by Borland and Microsoft. I really do not
> need the latest software,but I would like to be able for it to work
> properly on XP and Vista.

You should focus on building something first, before you get into deployment
scenarios. That is, unless you wanna try the way the way of General Moters.

> Harold A Climer
> Dept. Of Physics Geology, and Astronomy

Your ignorance and lack of research on the matter makes me question whether
you are a TROLL. Read: I don't believe you!

> U.T, Chattanooga
> Rm. 406A Engineering, Math & Computer Science Building
> 615 McCallie Ave. Chattanooga TN 37403
> Harold-Cimer@utc.edu


== 4 of 4 ==
Date: Thurs, Dec 31 2009 12:08 am
From: nick


On Dec 30, 10:33 pm, Sam <s...@email-scan.com> wrote:
> Harold A. Climer writes:
> > A friend of mine is moving to California and did not want to move all
> > his library. He gave me quite a few books on  programming using
> > different computer languages. BASIC,PASCAL, C,C++, etc.
> > I have had some experience programming in TRUEBASIC.
> > I would like to learn to program using C or C++.( Not professionally I
> > am too old for that)
> > What software do I need?(Something inexpensive) I have two computers.
> > The one at work is running Vista Home SP1 and the one at home is
> > running Windows XP MCE SP3.
> > I have seen some C++ stuff by Borland and Microsoft. I really do not
> > need the latest software,but I would like to be able for it to work
> > properly on XP and Vista.
>
> Borland, on this particular subject, is practically dead, monopolized out of
> existence, by MSFT.
>

Poor Borland. They sold their entire line of development tools last
year, but apparently some of the software is still going to see new
development. I doubt this gets much serious use these days.
www.embarcadero.com/products/cbuilder

> Somewhere on MSFT's web site you should find a free download of Visual
> Studio Express, or something named along those lines. That'll suffice for
> beginners. I don't know if MSFT updated their freeware for Vista, but it'll
> definitely work on XP.

The newer releases work with post-XP Windows editions. The debugger
and code completion are excellent, great for beginners. Be prepared
for some registration key nonsense though. www.microsoft.com/Express/VC/

> Full development tools from MSFT are pricy, since developers are a revenue
> source for MSFT. However, given that you're @.edu, you should be able to
> easily get a rather steep educational discount for the full Visual Studio
> kit. MSFT wants mindshare from young skulls full of mush, so they shove
> their wares into .edus for peanuts.

The Express Edition should be more than adequate for casual use,
though... You probably won't need to bother with anything more.

The only alternative to the Microsoft toolchain that really gets any
use on Windows as far as I know is MinGW (Windows port of gcc, the
standard-issue gnu/linux compiler). It might be worth trying as well,
but I'm willing to bet it's a bit more trouble to get set up.

-- Nick

==============================================================================
TOPIC: Constructor problem
http://groups.google.com/group/comp.lang.c++/t/d528665531d6d939?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Dec 30 2009 7:39 pm
From: Robert

I am trying to create dynamic constructors but have encountered a
problem.

I am trying to populate a floating point array as part of the class
structure.

But seemingly what ever I try returns the same result.

Which seems to be all data that is not an array, gets passed through
ok.

But if the variable is an array, in this case an array of floats, then
the values are not created.

I figure that, you may be able to help . . .

So, firstly, I have these classes, followed by the code and then the
debug output.

Any help will be very much appreciated.

Rob.
/////////////////////////
class FloatKeys
{

public:

DWORD nValues;
float* fvalues;


FloatKeys( DWORD n_type, float *f );


FloatKeys();
~FloatKeys();


};

FloatKeys::FloatKeys( DWORD n_type, float* fkeys )
{
nValues = n_type;

fvalues = new float [n_type];


fvalues[0] = fkeys[0] ;

fvalues[1] = fkeys[1] ;

fvalues[2] = fkeys[2] ;


}

FloatKeys::FloatKeys()
{

}


FloatKeys::~FloatKeys()
{

}


/////////////////////////////////
class TimedFloatKeys
{

public:

DWORD time;
FloatKeys tfkeys;

TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey );

TimedFloatKeys();
~TimedFloatKeys();

};


TimedFloatKeys::TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey )
{

time = dw_time;

tfkeys.fvalues = new float [3];

tfkeys = fltkey;


}

TimedFloatKeys::TimedFloatKeys()
{

}

TimedFloatKeys::~TimedFloatKeys()
{

}

////////////////////////////////
class Animation_key {

public:

DWORD keyType;

DWORD nKeys;

TimedFloatKeys* timed_float_key;

Animation_key( DWORD dw_type, DWORD n_Key, TimedFloatKeys*

timed_float_array );

Animation_key();
~Animation_key();


};

Animation_key::Animation_key( DWORD dw_type, DWORD n_Key,
TimedFloatKeys*

timed_float_array )
{

DWORD j = 0;

DWORD i = 0;

keyType = dw_type;

nKeys = n_Key;


timed_float_key = new TimedFloatKeys [n_Key];

for( i = 0; i < n_Key; i++ )
{

timed_float_key[i].time = timed_float_array[i].time;

timed_float_key[i].tfkeys.nValues = timed_float_array
[i].tfkeys.nValues;

for( j = 0; j < timed_float_key[j].tfkeys.nValues; j++ )
{

timed_float_key[i].tfkeys.fvalues = new float

[timed_float_key[i].tfkeys.nValues];

timed_float_key[i].tfkeys.fvalues[j] = (float)j;
//
timed_float_array[i].tfkeys.fvalues[j];

}

}

}

Animation_key::Animation_key()
{

}

Animation_key::~Animation_key()
{

}

// I try to invoke the classes with this code.

HRESULT ffile_handling::Save_Animation_Keys( )
{


HRESULT hr;

DWORD cbsize = 64;

char output[255];

float* f_key = new float [3];

f_key[0] = 1.0f;

f_key[1] = 5.0f;

f_key[2] = 9.0f;

DWORD t = 24;

// FLOAT KEY

FloatKeys my_flt( 3, f_key );


// TIMED FLOAT KEYS

TimedFloatKeys* tfk_ptr;

TimedFloatKeys my_timed_flts( t, my_flt );

tfk_ptr = &my_timed_flts;
;


// ANIMATION KEY

Animation_key a_key(2, 1, tfk_ptr );

// BREAK POINT –

the output as seen in the debugger is this:

[] a_key {…}
|
|---key_Type 2
|---nKeys 1
|---[] timed_float_key 0x00cb2204
|-------time 24
|---[] tfkeys { … }
|---nValues 3
|---[] fvalues 0x00cb2140
|-- -4.31602e+008

== 2 of 2 ==
Date: Wed, Dec 30 2009 8:50 pm
From: LR


Robert wrote:
[snippage]
>
> I am trying to create dynamic constructors but have encountered a
> problem.

I'm not sure what you mean by dynamic here.
>
> I am trying to populate a floating point array as part of the class
> structure.

> Which seems to be all data that is not an array, gets passed through
> ok.

Pointers too?

>
> But if the variable is an array, in this case an array of floats, then
> the values are not created.

Created or copied?
>
> I figure that, you may be able to help . . .

Have you thought about using std::vector?

> /////////////////////////
> class FloatKeys
> {
>
> public:
>
> DWORD nValues;
> float* fvalues;
>
>
> FloatKeys( DWORD n_type, float *f );

No copy ctor?
FloatKeys(const FloatKeys &);
>
>
> FloatKeys();
> ~FloatKeys();
>
>
> };
>
> FloatKeys::FloatKeys(DWORD n_type, float* fkeys )
> {
> nValues = n_type;
> fvalues = new float [n_type];
> fvalues[0] = fkeys[0] ;
> fvalues[1] = fkeys[1] ;
> fvalues[2] = fkeys[2] ;

Your code implies that n_type will always be at least 3. Is this true?
If it's possible for it to be some other value, then you might be better
off writing you code like:

FloatKeys::FloatKeys(DWord n_type, float *fkeys)
:
nValues(n_type),
fvalues(new float[n_type])
{
for(int i=0; i<n_type; i++)
fvalues[i] = fkeys[i];
}

> }

OTOH, if you know that n_type is going to be three each and every time,
why bother with the pointer in FloatKeys? Why not just make an array?
class FloatKeys {
float fvalues[3];
public:
FloatKeys() {
fvalues[0] = 0.;
fvalues[1] = 0.;
fvalues[2] = 0.;
}
....
};

> /////////////////////////////////
> class TimedFloatKeys
> {
> public:
> DWORD time;
> FloatKeys tfkeys;
>
> TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey );
No copy ctor here either?
>
> TimedFloatKeys();
> ~TimedFloatKeys();
> };
>
>
> TimedFloatKeys::TimedFloatKeys ( DWORD dw_time, FloatKeys fltkey )
> {
> time = dw_time;
> tfkeys.fvalues = new float [3];

This seems like a very awkward place to be doing this. I think you'd be
better off with a copy ctor for FloatKeys, and writing your ctor like this:

TimedFloatKeys::TimedFloatKeys(DWORD dw_time, const FloatKeys &fltkey)
:
time(dw_time),
tfkeys(fltkey)
{}


> tfkeys = fltkey;

You just clobbered the value that you just put in tfkeys.fvalues. Now
you'll have a memory leak too.

It's not clear to me if you want to do a deep or shallow copy in
TimedFloatKeys.
http://www.lmgtfy.com/?q=deep+and+shallow+copy


> ////////////////////////////////
> class Animation_key {
> public:
>
> DWORD keyType;
> DWORD nKeys;
> TimedFloatKeys* timed_float_key;

Why is that a pointer to a timed_float_key, instead of an instance? Do
you want this to point to an array of TimedFloatKeys?
>
> Animation_key( DWORD dw_type, DWORD n_Key, TimedFloatKeys*
>
> timed_float_array );

If you have copy ctors for the other classes, easier to write
Animation_key(DWORD dw_type, DWORD n_key, const TimedFloatKeys &tfa)
:
keyType(dwType),
nKeys(n_key),
timed_float_key(&tfa)
{}

>
> Animation_key();
> ~Animation_key();
>
>
> };
>
> Animation_key::Animation_key( DWORD dw_type, DWORD n_Key,
> TimedFloatKeys*
>
> timed_float_array )
> {
>
> DWORD j = 0;
>
> DWORD i = 0;
>
> keyType = dw_type;
>
> nKeys = n_Key;
>
>
> timed_float_key = new TimedFloatKeys [n_Key];
>
> for( i = 0; i < n_Key; i++ )
> {
>
> timed_float_key[i].time = timed_float_array[i].time;

[rest snipped]

I suggest that 1) you look up deep and shallow copy, 2) get FloatKeys to
work with both an assignment operator and a copy ctor even if you don't
need them, 3) look at std::vector which may be a help to you.

LR

==============================================================================
TOPIC: c++ as scripting language
http://groups.google.com/group/comp.lang.c++/t/8b6a860779bd7319?hl=en
==============================================================================

== 1 of 1 ==
Date: Thurs, Dec 31 2009 12:11 am
From: balajee

Hi,
Thanks for deep discussion. I am trying to move this thread towards
closing my needs. My requirement is - We have standard automation
framework which runs on TCL. These automation scripts connect to test
devices(our own platforms), execute few commands, fetch the output and
compare with standrd format to cross check everything is perfect. I
just want to know whether same thing can be done using C or C++.
Because of some internal reasons, we decided to not to use TCL or
PERL. We are just evaluating other options. Any suggessions are
welcome. Thanks in advance.

Thanks,
Balajee


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c++"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c++?hl=en

To unsubscribe from this group, send email to comp.lang.c+++unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c++/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

No comments: