Friday, January 1, 2010

comp.lang.c++ - 25 new messages in 14 topics - digest

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

comp.lang.c++@googlegroups.com

Today's topics:

* Constructor problem - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/d528665531d6d939?hl=en
* output - 5 messages, 3 authors
http://groups.google.com/group/comp.lang.c++/t/b9723ac6c0efc610?hl=en
* Possible workaround for dlsym() and cast from/to void* - 1 messages, 1
author
http://groups.google.com/group/comp.lang.c++/t/c0644c95757da3b1?hl=en
* Saving a binary file into a string - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/ddc89ddf30293133?hl=en
* Writing good articles that have much better chance to be seen by others - 2
messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/ddbe20a354342370?hl=en
* map vs list performance - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/f0b7f86d78b91416?hl=en
* .imanway.com/islam/modules...ayer.php? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/2494f07771560331?hl=en
* Learning C++ - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/4ccf5387f1787434?hl=en
* Which is best way to store objects? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/001f7d39ccdd72e8?hl=en
* Which C++ books are recommended? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/3b718e059a7036ac?hl=en
* Buffer wrapping in a stream - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/17cb466bada074d5?hl=en
* String not printing data on next line despite \n in the string - 1 messages,
1 author
http://groups.google.com/group/comp.lang.c++/t/35658d761ffe5130?hl=en
* Enumerated type and RNG - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/bcb1e1b64694a16d?hl=en
* Vectors vs Arrays performance - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/da11694bc14ba3ef?hl=en

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

== 1 of 3 ==
Date: Fri, Jan 1 2010 12:23 am
From: "io_x"


"Robert" <watkinsdev@hotmail.com> ha scritto nel messaggio
news:5dcb4b84-5d57-4f31-8a7a-ffc542f6bb58@k19g2000yqc.googlegroups.com...

i speak for me:

your exmple not show any indentation, the code not compile too.
so if you have some problem with one part of code it is better
you cut the most little peace of it that compile, run,
and self-show the problem.

when you tell something in a programming group,
do it preferibily in few words (it has not matter if they are
not spell perfect (at last for me)).

== 2 of 3 ==
Date: Fri, Jan 1 2010 4:59 am
From: Robert


On Dec 31 2009, 4:50 am, LR <lr...@superlink.net> wrote:
> 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- Hide quoted text -
>
> - Show quoted text -

Many thanks LR.

Even with my current headache, I actually made it work!

== 3 of 3 ==
Date: Fri, Jan 1 2010 5:00 am
From: Robert


On Dec 31 2009, 11:33 am, "Balog Pal" <p...@lib.hu> wrote:
> "Robert" <watkins...@hotmail.com>
>
> class FloatKeys
> {
> public:> DWORD nValues;
>
>  >float* fvalues;
>
> replace to:
>
>   std::vector<float> fvalues;
>
> >FloatKeys::FloatKeys( DWORD n_type, float* fkeys )
> >{
> >nValues = n_type;
> >...
>
> replace to:
>
> FloatKeys::FloatKeys( size_t size, const float* fkeys )
>  : fvalues(fkeys, fkeys+size)
> {}
>
> This solves the few dozen problems your code currently have.

Thanks balog pal, look justwhat I need!

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

== 1 of 5 ==
Date: Fri, Jan 1 2010 3:14 am
From: arunix


Thanks Sam for reply..
but again its not working
it shows like this.
for complie i use this command......

~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic ex-5-4-1.cpp
~/data/CPP/SSC++ $ ./a.out
aa
bb
aa
bb
aa
aa
bb // press Ctrl+D
interrupt
is there something wrong
and i am confused with


while(cin>>buf) value(buf)++;
why increment ?

.........


== 2 of 5 ==
Date: Fri, Jan 1 2010 4:56 am
From: domachine


On 1 Jan., 12:14, arunix <arru...@gmail.com> wrote:
> Thanks Sam for reply..
> but again its not working
> it shows like this.
> for complie i use this command......
>
> ~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic  ex-5-4-1.cpp
> ~/data/CPP/SSC++ $ ./a.out
> aa
> bb
> aa
> bb
> aa
> aa
> bb      // press Ctrl+D
> interrupt
> is there something wrong
> and i am confused with
>
> while(cin>>buf) value(buf)++;
> why increment ?
>
> .........

I think you should first understand some basic things of C/C++ before
moving on to topics like vector etc.
The question "why increment ?" tells me that you're a beginner. So
move on slowly.
Start your book from the beginning and try again.

Greetings


== 3 of 5 ==
Date: Fri, Jan 1 2010 11:53 am
From: Jonathan Lee


On Jan 1, 6:14 am, arunix <arru...@gmail.com> wrote:
> for complie i use this command......
> ~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic  ex-5-4-1.cpp

That seems ok. Worked for me.

> aa
> bb
> aa
> bb
> aa
> aa
> bb      // press Ctrl+D
> interrupt

Do you mean that you don't get the list of frequencies at
the end? i.e.,
aa: 4
bb: 3

That seems odd... but I would think it has less to do with
your program than your OS, or the particular terminal
application you're using.

> and i am confused with
>
> while(cin>>buf) value(buf)++;
> why increment ?

It's counting the number of times each input string is
typed. "value()" returns a reference to "number of times
'buf' has been typed". Then that number is incremented.

--Jonathan


== 4 of 5 ==
Date: Fri, Jan 1 2010 9:11 pm
From: arunix


On Jan 1, 5:56 pm, domachine <dominik.burgdoer...@googlemail.com>
wrote:
> On 1 Jan., 12:14, arunix <arru...@gmail.com> wrote:
>
>
>
> > Thanks Sam for reply..
> > but again its not working
> > it shows like this.
> > for complie i use this command......
>
> > ~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic  ex-5-4-1.cpp
> > ~/data/CPP/SSC++ $ ./a.out
> > aa
> > bb
> > aa
> > bb
> > aa
> > aa
> > bb      // press Ctrl+D
> > interrupt
> > is there something wrong
> > and i am confused with
>
> > while(cin>>buf) value(buf)++;
> > why increment ?
>
> > .........
>
> I think you should first understand some basic things of C/C++ before
> moving on to topics like vector etc.
> The question "why increment ?" tells me that you're a beginner. So
> move on slowly.
> Start your book from the beginning and try again.
>
> Greetings

yes i did it....


== 5 of 5 ==
Date: Fri, Jan 1 2010 9:14 pm
From: arunix

> Do you mean that you don't get the list of frequencies at
> the end? i.e.,
> aa: 4
> bb: 3

yes

> That seems odd... but I would think it has less to do with
> your program than your OS, or the particular terminal
> application you're using.

using gcc 4.4.2

> It's counting the number of times each input string is
> typed. "value()" returns a reference to "number of times
> 'buf' has been typed". Then that number is incremented.
Thanks...

==============================================================================
TOPIC: Possible workaround for dlsym() and cast from/to void*
http://groups.google.com/group/comp.lang.c++/t/c0644c95757da3b1?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 4:54 am
From: James Kanze


On Dec 31 2009, 4:20 pm, Joshua Maurice <joshuamaur...@gmail.com>
wrote:
> On Dec 31, 11:04 am, anonimo <anon...@no.it> wrote:> Hello everyone,

> > i want to dinamically load one new function with
> > dlopen()+dlsym(). As we all know, the problem is that ISO
> > C++ does not allow to safely cast from void* to
> > pointer-to-function and viceversa.

> [snip]

> Just because it's not defined by one standard (the C++
> standard) does not make it undefined. It's defined by the
> POSIX standard. So, it's ok.

No it's not, and it's illegal in C++ (and C), so a compiler is
required to diagnose the error. (Only in strict conformance
mode, of course, and having done so, it can go on an compile
your code correctly.)

Posix does require all function, void and object pointers to
have the same size and representation, and suggests:

void (*pf)();

*(void**)( &pf ) = dlsym...

They also hint that they are looking into a cleaner solution.

> However, it still lacks all kinds of type safety, but there's
> no way to really have type safety with the current system. The
> object file format does not store the type of function, so you
> have to know beforehand the function type, and thus the
> dynamic loader and dlsym cannot help you out.

That is, regretfully, true for most systems of explicit dynamic
loading.

--
James Kanze

==============================================================================
TOPIC: Saving a binary file into a string
http://groups.google.com/group/comp.lang.c++/t/ddc89ddf30293133?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 5:08 am
From: James Kanze


On Dec 31 2009, 10:04 pm, Dominik Schmidt <dominikschmi...@gmx.net>
wrote:
> On Mon, 28 Dec 2009 12:53:46 -0800 (PST), James Kanze wrote:
> >> I'd like to know if I made an error, so if there's any
> >> possibility that my functions won't work in some case?

> > The question is: how portable do you want to be?

> Currently I'm working on a Windows application, but I'm sure
> I'll use those functions in other projects in the future. So
> it should at least work on Windows and on common Linux
> distributions (like Debian).

In sum, very limited portability.

> > (While I'm at it, what's with these file1? It's output, or
> > what ever, or even file, given the context, but without the
> > 1.)

> I like names with numbers!? Is there something wrong about
> calling this variable "file1"? Would you prefer "Johnson"? :-)

There's definitely something wrong with names with no semantic
content. If the function is clearly dealing with only one file,
then "file" may be sufficient. Generally, however, you'd want
"inputfile" and "outputfile" (or simply "source" and
"destination", if it's clear that they are files). About the
only time something like "file1" and "file2" would be
appropriate is if the function outputs exactly the same thing to
two different files.

[...]
> > If you're using system specific code... Use system specific
> > versions of open/read/write/close. They should offer the
> > necessary options for causing open to fail if the file doesn't
> > exist (input) or causing your file to replace any existing file
> > (output). For that matter, IIRC, this is guaranteed when you
> > open an ifstream or an ofstream, so you don't need any of this
> > anywhere.

> Yes, I'm using system specific code in FileExists(). The only
> reason is that I'm not sure yet how to do this in a better
> way. I want to get a false (as return value) only if the file
> does not exist, but a true, if it exists, even if it's locked
> or hidden.

Hidden only affects things like dir, I think; open shouldn't
care if a file is hidden or not. But for getting any more
information than whether you can write it or not, you will have
to use system level reqests.

[...]
> >> bool KillFile(string FilePath)
> >> {
> >> int Out1;

> >> Out1 = remove(FilePath.c_str());

> >> return Out1 == 0;
> >> }

> > Just for the record, this can have interesting consequences
> > under Unix, and might not be what you want.

> What (consequences) do you mean by that?

The semantics when hard links are involved.

Not knowing the application, it's hard to know what the
"correct" solution is, but typically, just opening an existing
file for writing results in the correct semantics. If there are
reasons you don't want to do this (transactional integrity, for
example), then the solution is more complex, and typically
involves using a temporary file (necessary for transactional
integrity anyway) and possibly physical copying; the important
thing is that if the number of hard links to the file is greater
than one, you can't delete and recreate it---you have to
overwrite the original, or some of the hard links will continue
to refer to the old version.

--
James Kanze


==============================================================================
TOPIC: Writing good articles that have much better chance to be seen by others
http://groups.google.com/group/comp.lang.c++/t/ddbe20a354342370?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Jan 1 2010 5:14 am
From: James Kanze


On Dec 31 2009, 4:05 pm, Andy Champ <no....@nospam.invalid> wrote:
> tanix wrote:

> > My humble friend, I understand Usenet better than you,
> > probably an order of magnitude better.

> James has been providing useful replies on this group for at
> least three years.

Just to set the matters straight, I've been active in newsgroups
since 1991, and at one time, maintained a newsgroup server. For
many years, my specialty was network management software, so I
do know what I'm talking about here.

--
James Kanze


== 2 of 2 ==
Date: Fri, Jan 1 2010 7:27 am
From: tanix@mongo.net (tanix)


In article <366a1d98-6605-431d-8c2d-3853ce4eaac3@v25g2000yqk.googlegroups.com>, James Kanze <james.kanze@gmail.com> wrote:
>On Dec 31 2009, 4:05 pm, Andy Champ <no....@nospam.invalid> wrote:
>> tanix wrote:
>
>> > My humble friend, I understand Usenet better than you,
>> > probably an order of magnitude better.
>
>> James has been providing useful replies on this group for at
>> least three years.
>
>Just to set the matters straight, I've been active in newsgroups
>since 1991, and at one time, maintained a newsgroup server. For
>many years, my specialty was network management software, so I
>do know what I'm talking about here.

Cool.
:--}

--
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: map vs list performance
http://groups.google.com/group/comp.lang.c++/t/f0b7f86d78b91416?hl=en
==============================================================================

== 1 of 4 ==
Date: Fri, Jan 1 2010 6:13 am
From: "Leigh Johnston"


"Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
news:rcbpj559cc6n6kpfjf5i1fl5hi498jjmvj@4ax.com...
>
> or
>
> bool abcd::operator<(const abcd & rhs) const
> {
> if (a != rhs.a)
> return (a < rhs.a);
> else if (b != rhs.b)
> return (b < rhs.b);
> else if (c != rhs.c)
> return (c < rhs.c);
> else
> return (d < rhs.d);
> }

That looks wrong, you have to check for a > rhs.a etc. also.

== 2 of 4 ==
Date: Fri, Jan 1 2010 6:15 am
From: "Leigh Johnston"


Actually I retract that... :)

"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:p4ydnad4YemLnqPWnZ2dnUVZ7vCdnZ2d@giganews.com...
> "Stephen Howe" <sjhoweATdialDOTpipexDOTcom> wrote in message
> news:rcbpj559cc6n6kpfjf5i1fl5hi498jjmvj@4ax.com...
>>
>> or
>>
>> bool abcd::operator<(const abcd & rhs) const
>> {
>> if (a != rhs.a)
>> return (a < rhs.a);
>> else if (b != rhs.b)
>> return (b < rhs.b);
>> else if (c != rhs.c)
>> return (c < rhs.c);
>> else
>> return (d < rhs.d);
>> }
>
> That looks wrong, you have to check for a > rhs.a etc. also.

== 3 of 4 ==
Date: Fri, Jan 1 2010 6:42 am
From: "Leigh Johnston"


My timings indicate something somewhat different to your findings:

struct timer
{
timer(const char* message)
{
std::cout << message;
QueryPerformanceCounter(&iStartTime);
}
~timer()
{
LARGE_INTEGER endTime;
QueryPerformanceCounter(&endTime);
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
std::cout.precision(4);
std::cout << std::fixed << (float)(endTime.QuadPart -
iStartTime.QuadPart)/(float)freq.QuadPart << " seconds" << std::endl;
}
LARGE_INTEGER iStartTime;
};

struct abcd
{
int a;
int b;
int c;
int d;
abcd() : a(rand() % 1000), b(rand() % 1000), c(rand() % 1000), d(rand() %
1000) {}
bool operator<(const abcd& rhs) const
{
if (a != rhs.a)
return (a < rhs.a);
else if (b != rhs.b)
return (b < rhs.b);
else if (c != rhs.c)
return (c < rhs.c);
else
return (d < rhs.d);
}
bool operator==(const abcd& rhs) const
{
return (a == rhs.a && b == rhs.b && c == rhs.c && d == rhs.d);
}
};

int main()
{
std::vector<abcd> v;
for (int i = 0; i != 1500; ++i)
v.push_back(abcd());
std::list<abcd> l;
std::multiset<abcd> s;
for (int i = 0; i != 1500; ++i)
{
l.push_back(v[i]);
s.insert(v[i]);
}
{
srand(0);
timer t("list: ");
for (int i = 1; i != 1000000; ++i)
std::find(l.begin(), l.end(), v[rand() % 1500]);
}
{
srand(0);
timer t("multiset: ");
for (int i = 1; i != 1000000; ++i)
s.find(v[rand() % 1500]);
}
}

---

list: 2.1494 seconds
multiset: 0.1614 seconds

multiset is equivalent to map for the purposes of this discussion.

== 4 of 4 ==
Date: Fri, Jan 1 2010 3:13 pm
From: Maxim Yegorushkin


On 31/12/09 00:54, Branimir Maksimovic wrote:
> 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.

Not so.

Both map and list store elements in nodes. Each node is allocated
separately.

> I have discovered
> that by using bidirectional iterator for quick sort instead of
> random access I gain double the speed of quick sort...

Can not be true. Quick sort requires random iterators, this is why you
can't pass std::list<> iterators into std::sort(). std::list<> has sort
member function for sorting which employs mergesort because quicksort
can not be used for lists.

[]

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

Sounds unlikely. Can't ignore CPU cache effects.

--
Max

==============================================================================
TOPIC: .imanway.com/islam/modules...ayer.php?
http://groups.google.com/group/comp.lang.c++/t/2494f07771560331?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 6:18 am
From: Mary


http://www.imanway.com/islam/modules/debaser/player.php?id=17

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

== 1 of 1 ==
Date: Fri, Jan 1 2010 8:00 am
From: "Ron AF Greve"


Hi,

Microsoft visual C++ express edition is a free download from :

http://www.microsoft.com/express/

You might also want to download the MSDN docs.


"Harold A. Climer" <Harold-Climer@utc.edu> wrote in message
news:nu3oj5lfhug5cva054rkoiq7okov4go8uu@4ax.com...
| 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

--
Regards, Ron AF Greve

http://informationsuperhighway.eu

==============================================================================
TOPIC: Which is best way to store objects?
http://groups.google.com/group/comp.lang.c++/t/001f7d39ccdd72e8?hl=en
==============================================================================

== 1 of 2 ==
Date: Fri, Jan 1 2010 10:56 am
From: Angus


Hello

I want to store a collection of objects in a vector. I think the best
way is for me to create each object on the free store (heap) and then
store a pointer to the object in the vector.

Eg like this:
std::vector<MyWidget*> m_widgets;
MyWidget* newone = new MyWidget;
//populate/process
m_widgets.push_back(newone);

Rather than:
std::vector<MyWidget> m_widgets;
MyWidget newone;
//populate/process
m_widgets.push_back(newone);

Reason being that with a pointer the push back does not have to do a
copy of the entire object, it simply copies the address of the object.

The only downside to pointer approach is having to delete the object
at the end but that is easy enough to do.

What other approaches could I use? Could I use a reference instead?

Any comments would be very much appreciated.

Angus


== 2 of 2 ==
Date: Fri, Jan 1 2010 11:14 am
From: Jonathan Lee


On Jan 1, 1:56 pm, Angus <anguscom...@gmail.com> wrote:
> Hello
>
> I want to store a collection of objects in a vector.  I think the best
> way is for me to create each object on the free store (heap) and then
> store a pointer to the object in the vector.

I think the best option for what you describe would be to use a
smart pointer, like the Boost shared_ptr.

> Reason being that with a pointer the push back does not have to do a
> copy of the entire object, it simply copies the address of the object.

Depending on your object, you might have the option of pushing an
"empty" object and then using swap to get your data into the vector.

myvector.push_back(MyObject()); // Default constructor
myvector[myvector.size() - 1].swap(TheActualObject);

(Presumably the default constructor has very little data in it.)
A little roundabout, but you get to have a vector of objects instead
of a vector of pointers.

--Jonathan

==============================================================================
TOPIC: Which C++ books are recommended?
http://groups.google.com/group/comp.lang.c++/t/3b718e059a7036ac?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 1:19 pm
From: nick


On Dec 31 2009, 3:50 pm, Immortal Nephi <Immortal_Ne...@hotmail.com>
wrote:
>         I have been through study by reading How to Program C++ by Deitel 6th
> Edition.  I believe that it has very little information to explain
> design patterns.
>         I want to expand more knowledge of better understanding how Design
> Patterns is used with Object-Oriented Programming.  I don't find any
> good books.
>         ATM example teaches how to use UML 2.0 and write several base
> classes, one inheritance and polymorphism.  ATM is the base class.
> Screen, Keypads, Cash Withdrawal, and Deposit Slot are base classes
> and they have compositions to be placed in ATM's base class.
>         Think of creating more subclasses.  The screen's base class is good
> example.  I might want to build more subclasses and add more member
> functions and data members to all subclasses.  They are private
> subclasses inside screen's base class.
>         We discussed gigantic class post earlier.  Screen's base class might
> become gigantic class.  I am able to extract gigantic class into
> subclasses by using refactoring.
>         I want to learn how to use Design Patterns and Refactoring.  Here are
> two books below.  I am not sure if they are the right books.  If not,
> please make the list which books do you recommend me to study more.
>
> Object-Oriented Computation in C++ and Java: A Practical Guide to
> Design Patterns for Object-Oriented Computing by Conrad Weisert
>
> Refactoring: Ruby Edition (Hardcover) by Jay Fields

The book "Design Patterns Explained: A New Perspective on Object-
Oriented Design" gives a good overview of what design patterns are,
explains in detail some common design patterns, talks about UML class
and interaction diagrams, etc. The code examples are in Java but they
translate to C++ easily. It's a good read for someone just getting
familiar with design patterns.

==============================================================================
TOPIC: Buffer wrapping in a stream
http://groups.google.com/group/comp.lang.c++/t/17cb466bada074d5?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 2:30 pm
From: Dietmar Kuehl


On Dec 30 2009, 10:16 am, Philippe MESMEUR
<philippe.mesm...@gmail.com> wrote:
> ostream.SetBuffer(buf, SIZE);  /// here is what I would like to do
> TestOutStream(ostream);

I don't know what "ostream.SetBuffer()" is supposed to be. If you are
referring to std::streambuf::pubsetbuf() you'll find that this
operation is only really useful to turn a file stream into being
unbuffered. All other uses are non-portable and in most cases don't do
anything.

From the looks of it you want to set up a stream to write into a
memory area. The way to do this is to create a simple stream buffer
which only sets up a put area which is never resized. The stream
buffer is then used to initialize an std::ostream. Here is how this
would roughly look like:

#include <streambuf>
#include <iostream>

struct membuf:
std::streambuf
{
membuf(char* buffer, size_t size) { this->setp(buffer, buffer +
size); }
char* end() const { return this->pptr(); }
};

int main()
{
char buffer[1024];
membuf sbuf(buffer, sizeof(buffer));
std::ostream out(&sbuf);

out << "hello, world\n";

// the sequence [buffer, sbuf.end()) now contains the written
string
}

The code is untested and most likely contains a few typos but should
essentially work...

==============================================================================
TOPIC: String not printing data on next line despite \n in the string
http://groups.google.com/group/comp.lang.c++/t/35658d761ffe5130?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 3:03 pm
From: "BGB / cr88192"

"stan" <smoore@exis.net> wrote in message news:hnfv07-j7n.ln1@invalid.net...
> BGB / cr88192 wrote:
>>>>
>>>> as well, 256 is an "accepted" maximum string length (a tradition since
>>>> long past that something is seriously wrong if a string is longer than
>>>> this).
>
> You're young and your history seems to stem from the micro world.
> Limits date from a time when really long punch cards were hard to deal
> with and they didn't fit the reader. :)
>

possibly, but not that young anymore...

but, yeah, admittedly AFAIK punch cards went out of style decades before I
was born (which was I guess during the high point of 5.25" floppies and the
IBM PC, which were themselves a rapidly dying technology by the time I was
really old enough to do much, in the days of the dying DOS and rise of
Windows...).

but, now, much time has passed, and age is setting in...


>>> Accepted by who? I'm serving 10MB HTTP packets through std::string so
>>> I'm
>>> sorry I have never heard of this convention. (There was a 256-char
>>> string
>>> limitation in Turbo Pascal 3.3, but fortunately this is about 15 years
>>> in
>>> history ;-)
>>
>> "accepted" by traditional practice.
>>
>> typically, constants like PATH_MAX, ... are 256.
>
> POSIX and most linux disagree, check linux/limits.h.
>

odd, I had seen PATH_MAX as 256, but then again, I am in Windows-land...


>> it doesn't take long (for example, if one digs around in system headers),
>> before a string-length limit of 256 becomes a recurring pattern (even if
>> there are variations, for example, UNIX_MAX is 108, but I think this is
>> because of a general rule that (sizeof(sockaddr_storage)==128) or so,
>> ...).
>>
>> there are many other examples of this particular limit being in use.
>
> Calling this "accepted" or "common" is a stretch. Basic, Pascal, and
> environments that represented strings with embedded size were limited
> but even in research unix the constant was often larger even with real
> core memory constraints. It's hard to find or imagine a modern
> unix/linux/Gnu app with a 256 limit.
>

ok.


> Windows has some real ideas about accepted string limits scattered
> around randomly but I can't agree that either the concept of limits or
> even a common 256 default exists in c and it's nonsense for c++.
>

granted.

it depends on usage I guess, since it is worth noting that a longer limit
usually means using up more space on the stack, and stack space is not
exactly free...

likewise, heap isn't exactly free either, and allocating/freeing memory can
hurt performance if done poorly (such as in a function which is called in a
loop).

as others have noted, it may not be a big issue if one is processing input
which comes from disk, but I guess it is a question of what and how much
comes from disk, and how much is being shoved around intra-app (say, for
inter-component communication, ...).


>> it is an accepted limit, much like how i, j, and k, are accepted names
>> for
>> integer variables, ...
>
> Mostly habit from Fortran, where it was part of the language and not
> just a convention, that was passed on through generations.
>

yep.
granted, it is not good to defy traditions though, since usually things are
some particular way for a good reason...


>> granted, sometimes one wants a bigger limit though, and sometimes a
>> bigger
>> limit is used (as there is no real technical reason for this particular
>> limit apart from convention), ...
>
> You've mentioned games many times, so maybe in that domain this is a
> convention. In many other fields this doesn't wash, and it seems you
> may be crossing up c and c++ to boot.

I use both C and C++, though generally more C than C++.

note that, for example, in Quake 2, most string limits are shorter than
this, for example, 16 and 64 character string limits are common (for
example, QPATH_MAX is defined as 64, ...).


I also deal a lot with VM type stuff:
interpreters, JIT compilers, ... where basically the compiler may be working
(say, compiling code fragments, ...) at the same time as other parts of the
application are doing other tasks, ...

interpreting code, and wasting time in an interpreter, can easily kill
performance. an interpreter, for example, often has to cut a lot of corners
in an attempt to keep speed up (and, even then, interpreters still tend to
be rather slow, hence the usage of JIT in many cases, but then one needs to
have relatively fast compiler machinery, ...).


I typically use larger limits, but usually the 256-char limit is for a
single token (in parsing).

for buffers which may deal with globs of text, I usually use either larger
limits, expanding buffers, or size limit checks.

I usually use a limit of around 1024 or so for name-mangled tokens (say,
when the function name and signature are mangled together for linker-related
purposes, ...).

I think by convention though, PE/COFF informally has a limit of 256 here
(for valid function names), and the C standard has a limit around 32 (for a
minimum allowed implementation limit), where a longer name is not required
to be necessarily valid in a conforming compiler (the usual idea being that
the identifier would be truncated).

granted, this should not be a problem so long as one is not using the
MyFunctionNameIsDamnNearAWholeParagraph naming scheme...

granted, it is worth noting though that some bulk to names is usually added
as a result of using a naming convention which tends to add library and
subsystem prefixes to exported names (except public API functions, which
tend to have a much shorter prefix).

this is common in C and mixed C & C++ codebases, given the non-availability
of namespaces, ...


it is also common practice (in C) not to wrap strings in any sort of
container, since this makes things typically more awkward, and generally
hurts performance (say, due to added pointer indirections, function calls,
...).

or such...


==============================================================================
TOPIC: Enumerated type and RNG
http://groups.google.com/group/comp.lang.c++/t/bcb1e1b64694a16d?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 3:43 pm
From: mike3


Hi.

How could I resolve this C++ programming dilemma?

---
enum smth { ZERO, ONE, TWO, MAX }; // not really labeled this way

void f(smth parameter)
{
( ... )
}

( ... )
f(static_cast<smth>(RNG(MAX))); // RNG generates number from 0 to
MAX-1
( ... )
f(TWO); // f is called with fixed constant
( ... )
f(666); // won't work -- good
( ... )
---

Note the marked bit. f() takes the enumerated type smth, so you can't
just stick "666" in there and cause something bad that way, yet I
also need to pass a randomly obtained value from that list, which
requires a cast. Is it considered good practice to use a cast in this
situation? Would it be better to have f() take an "int" or something
like
that and then include an explicit check in there to see if the value
is
outside 0...MAX-1?

Finally, I suppose one could also do this:
int rand(RNG(MAX));
if(rand == ZERO) f(ZERO);
if(rand == ONE) f(ONE);
if(rand == TWO) f(TWO);

This avoids the cast, but seems to introduce a mantainability defect
and
making the code more susceptible to bugs: if one goes and adds more
values to the enum, then must be added here, too (not just in f()),
and
if this is forgotten about... bug!

What should be done here?

==============================================================================
TOPIC: Vectors vs Arrays performance
http://groups.google.com/group/comp.lang.c++/t/da11694bc14ba3ef?hl=en
==============================================================================

== 1 of 1 ==
Date: Fri, Jan 1 2010 10:52 pm
From: Udit Sharma


Hi,

I am a newbie as far as C++ is concerned. I am just reading books like
Effective C++ and More Effective C++. So kindly bear with me if I have
made some very stupid mistake.

I have read in books that it is always better to use vectors rather
than arrays and that there is no performance hit and also that your
code would be more safe to use when using vectors.

I had to write a small program to compute the sum of digits in a
factorial of a number (<= 10000). I have written two versions: one
using vectors, and one using plain arrays.

But I notice a stark difference in execution times. The program using
arrays runs in about 0.309 seconds (to compute sum of digits in
10000!) while the one using vectors takes about 2.9 seconds to finish
the same. I compiled both on an Ubuntu 9.1 Linux machine running using
gcc 4.4.1 and at optimization level 3.

I have listed both the programs here. Kindly tell me if the problem is
with the way I have written the code or something else.

//Program using arrays
#include <iostream>
int main() {
unsigned int factorial = 0;
std::cout << "Enter number : ";
std::cin >> factorial;

const unsigned MAXDIGITS = 10000, BASE = 10000;
unsigned indDigits[MAXDIGITS]; //This is the array that am
talking about
indDigits[0] = 1;
unsigned int counter = 0, oanda = 0, size = 0;

for(unsigned facts = 1; facts <= factorial; facts++) {
for (unsigned index = counter; index <= size; index++){
oanda += facts * indDigits[index];
indDigits[index] = oanda%BASE;
if(index == counter && !((oanda > 0) && (oanda != BASE)))
counter++;
oanda /= BASE;
}
if(oanda)
indDigits[++size]=oanda;
}

unsigned sumDigits = 0;
for (unsigned i = 0; i <= size; i++){
for(unsigned j = 1; j <= BASE/10; j*=10)
sumDigits += (indDigits[i]/j)%10;
}
std::cout << "Sum of Digits: " << sumDigits << std::endl;
}


//Same Program using vectors
#include <iostream>
#include <vector>
typedef std::vector<unsigned> Vec;
typedef std::vector<unsigned>::iterator Iter;

int main() {
unsigned int factorial = 0;
std::cout << "Enter number : ";
std::cin >> factorial;

const unsigned BASE = 10000;
Vec indDigits; //This is the vector that am talking about
indDigits.push_back(1);
unsigned int counter = 0, oanda = 0, size = 0;

for(unsigned facts = 1; facts <= factorial; facts++) {
oanda = 0;
for(Iter iter = indDigits.begin() + counter; iter !=
indDigits.end(); iter++){
oanda += facts * (*iter);
*iter = oanda%BASE;
if((iter == indDigits.begin() + counter) && !((oanda > 0)
&& (oanda != BASE)))
counter++;
oanda /= BASE;
}
if(oanda)
indDigits.push_back(oanda);
}

unsigned sumDigits = 0;
for (Iter iter = indDigits.begin(); iter != indDigits.end(); iter+
+){
for(unsigned j = 1; j <= BASE/10; j*=10)
sumDigits += ((*iter)/j)%10;
}
std::cout << "Sum of Digits: " << sumDigits << std::endl;
}

Thanks in advance.

Cheers
Udit


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

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: