Tuesday, January 26, 2010

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

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

comp.lang.c++@googlegroups.com

Today's topics:

* Generate a random number - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/dd36f1e5144c1857?hl=en
* I'm a newbie. Is this code ugly? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/f085e44989fab5ef?hl=en
* complex struct - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/59f0b47ab3f08848?hl=en
* writing binary files - 6 messages, 5 authors
http://groups.google.com/group/comp.lang.c++/t/3acbfe82ba5d0012?hl=en
* How to exclude some .CPP files from being linked in Visual Studio? - 2
messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/bf9a0fa82529e4bc?hl=en
* Again substrings and so on - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/736557e5175db0ba?hl=en
* generate rand number - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/fcb11e63c73e0db8?hl=en
* Virtual construtors - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/aea7843ff0dd519f?hl=en
* STL/CLR library in Visual Studio 2008 - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/2108ce8aea45b62f?hl=en
* Memory contents mysteriously changing - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/d3d7f50cba06cf27?hl=en
* std::not1() doesn't accept a function pointer - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/940b7aa3b05d8e70?hl=en

==============================================================================
TOPIC: Generate a random number
http://groups.google.com/group/comp.lang.c++/t/dd36f1e5144c1857?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 26 2010 6:53 am
From: Robbin-Lime


On Jan 26, 9:50 pm, Robbin-Lime <gentlerob...@gmail.com> wrote:
> I'd like to generate a random number to observe behaviors of intrusive
> and leeches like programs in my own computers, I discovered 2, both of
> which relates to my named blk IBM server. Really intensify my
> eyestrain everytime I see them their but undeleteable.
> These I can find a bunch in stores near the park. I can also wish to
> manually adjust them instead by the way

I have no experience in writing a debugger to reveal their internal
behavior "(

==============================================================================
TOPIC: I'm a newbie. Is this code ugly?
http://groups.google.com/group/comp.lang.c++/t/f085e44989fab5ef?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 7:29 am
From: "io_x"

"Richard Herring" <junk@[127.0.0.1]> ha scritto nel messaggio
news:o4ESjYCO6WXLFwII@baesystems.com...
> In message <4b5b5b92$0$1101$4fafbaef@reader4.news.tin.it>, io_x
> <a@b.c.invalid> writes
>>
>>"Richard Herring" <junk@[127.0.0.1]> ha scritto nel messaggio
>>news:KJlK$0BNjXWLFw2x@baesystems.com...
>>> In message <4b594ee2$0$1115$4fafbaef@reader4.news.tin.it>, io_x
>>> <a@b.c.invalid> writes
>>>>>>>>{int i, n;
>>>>>>>> char *p;
>>>>>>>> if(v==0) R 0;
>>>>>>>> n=strlen(v);
>>>>>>>> if(n<=0) R 0;
>>>>>>>
>>>>>>> Why? There's nothing intrinsically wrong with a zero-length string.
>>>>>>
>>>>>>yes here i would say "if(n<0) R 0;"
>>>>>
>>>>> And under what circumstances would strlen() ever return a negative value?
>>>>
>>>>it seems strlen() here returned one unsigned type: size_t
>>>>here size_t is "unsigned int" of 32 bit but this means
>>>>strlen can return 0xF0000000 that is a negative number
>>>>seen it like int;
>>>
>>> Only because you are perversely choosing to cast it to a signed type when it
>>> is not.
>>
>>>>and for me that negative number is not ok
>>>
>>> So you should be declaring the variable n as size_t, not int.
>>
>>i choose int
>>so i choose that the valid array of chars
>>could have len in the range 0..max_int
>>at last here in my sys
>>
> Not a valid reason. By that logic, if you chose double, you could have lengths
> up to DBL_MAX.
>
> Why do you have a problem with giving n the obvious type (because it is what
> strlen returns) of size_t? What do you think the length range would be in that
> case?

size_t could be the "the obvious type" but with "int" i can check
for overflow checking if there is some value negative
in some places where could be overflow or afther too

if one program has
size_t t=90;

and there is one
t+=v;
and v make overflow t; what that program has to do:
0) check and return to the caller one error result
1) abort the program, the sys, the OS
2) nothing has to happen

my answer is 0

> --
> Richard Herring

== 2 of 2 ==
Date: Tues, Jan 26 2010 7:55 am
From: Richard Herring


In message <4b5f0875$0$1112$4fafbaef@reader2.news.tin.it>, io_x
<a@b.c.invalid> writes
>
>"Richard Herring" <junk@[127.0.0.1]> ha scritto nel messaggio
>news:o4ESjYCO6WXLFwII@baesystems.com...
>> In message <4b5b5b92$0$1101$4fafbaef@reader4.news.tin.it>, io_x
>> <a@b.c.invalid> writes
>>>
>>>"Richard Herring" <junk@[127.0.0.1]> ha scritto nel messaggio
>>>news:KJlK$0BNjXWLFw2x@baesystems.com...
>>>> In message <4b594ee2$0$1115$4fafbaef@reader4.news.tin.it>, io_x
>>>> <a@b.c.invalid> writes
>>>>>>>>>{int i, n;
>>>>>>>>> char *p;
>>>>>>>>> if(v==0) R 0;
>>>>>>>>> n=strlen(v);
>>>>>>>>> if(n<=0) R 0;
>>>>>>>>
>>>>>>>> Why? There's nothing intrinsically wrong with a zero-length string.
>>>>>>>
>>>>>>>yes here i would say "if(n<0) R 0;"
>>>>>>
>>>>>> And under what circumstances would strlen() ever return a negative value?
>>>>>
>>>>>it seems strlen() here returned one unsigned type: size_t
>>>>>here size_t is "unsigned int" of 32 bit but this means
>>>>>strlen can return 0xF0000000 that is a negative number
>>>>>seen it like int;
>>>>
>>>> Only because you are perversely choosing to cast it to a signed
>>>>type when it
>>>> is not.
>>>
>>>>>and for me that negative number is not ok
>>>>
>>>> So you should be declaring the variable n as size_t, not int.
>>>
>>>i choose int
>>>so i choose that the valid array of chars
>>>could have len in the range 0..max_int
>>>at last here in my sys
>>>
>> Not a valid reason. By that logic, if you chose double, you could
>>have lengths
>> up to DBL_MAX.
>>
>> Why do you have a problem with giving n the obvious type (because it is what
>> strlen returns) of size_t? What do you think the length range would
>>be in that
>> case?
>
>size_t could be the "the obvious type" but with "int" i can check
>for overflow checking if there is some value negative
>in some places where could be overflow or afther too
>
>if one program has
>size_t t=90;
>
>and there is one
>t+=v;
>and v make overflow t;

So you're using wrapping as a poor substitute for checking that t is in
range. On most systems, t will reach a value which exceeds the
available memory long before it wraps to a negative value.

If range checking is important, check the range. Just as
std::vector::at() (and probably operator[] too) does, using unsigned
subscripts.

>what that program has to do:
>0) check and return to the caller one error result
>1) abort the program, the sys, the OS
>2) nothing has to happen
>
>my answer is 0

Right. But for many values of v, your solution will actually give (2).

--
Richard Herring

==============================================================================
TOPIC: complex struct
http://groups.google.com/group/comp.lang.c++/t/59f0b47ab3f08848?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 8:18 am
From: "Larry"


Hi,

I have those structs:

const int numbuff = 3;

struct buffer
{
unsigned char data[1024];
int bytesRecorded;
int user;
buffer(const unsigned char * data_, const int bytesRecorded_, const int
user_) :
bytesRecorded(bytesRecorded_), user(user_)
{
copy(data_, data_ + bytesRecorded_, data);
}
};

struct circular
{
circular_buffer<buffer> cb[numbuff];
};

// circular is an array of numbiff buffer(s).


Now, I can't seem to access buffer's fields by circular anymore! All I can
access is buffer constructor:

circular c;

c.cb->push_back(buffer(NULL, 0, 0)); // write or overwrite buffer

If I were to read the last element from buffer how could I go about it?

thanks

== 2 of 2 ==
Date: Tues, Jan 26 2010 12:17 pm
From: "John H."


On Jan 26, 10:18 am, "Larry" <dontmewit...@got.it> wrote:
> struct buffer
> {
>  unsigned char data[1024];
>  int bytesRecorded;
>  buffer(const unsigned char * data_, const int bytesRecorded_, const int
> user_);
> };
>
> struct circular
> {
>  circular_buffer<buffer> cb[numbuff];
>
> };
>
> // circular is an array of numbiff buffer(s).

More accurately, circular HAS an array. That array contains numbuff
circular_buffers. Those circular_buffers are themselves containers
that can contain buffers.

> circular c;
>
> Now, I can't seem to access buffer's fields by circular anymore! All I can
> access is buffer constructor:

c; // gives you the circular object
c.cb; // gives you the array of circular_buffers in c
c.cb[0]; // gives you the first of the circular_buffers in the array
in c
c.cb[0][0]; // gives you the first of the buffers inside the first
circular_buffer in the array in c
c.cb[0][0].data // gives you the data of the first of the buffers
inside the first circular_buffer in the array in c

> If I were to read the last element from buffer how could I go about it?

If you really ment "last element from buffer", perhaps you want to
access the last byte in a buffer object's data member. If this is the
case then:
buffer buff(...);
buff.data[1023];
Perhaps you ment the last byte recorded to the buffer, then something
like:
buff.data[buffer.bytesRecorded-1];

> c.cb->push_back(buffer(NULL, 0, 0));

What you are doing here is taking your circular object named c,
getting the array of circular_buffers named cb, then taking the first
of those circular_buffers, and pushing back a buffer into it. This
might be written more clearly as:
c.cb[0].push_back(buffer(NULL, 0, 0));
Neither of these statement will do anything because all of our
circular_buffers in the array in c are of size 0.

Since you didn't talk much about what you are trying to do, I can only
speculate, but I am guessing that your design here is not doing what
you want it to.

==============================================================================
TOPIC: writing binary files
http://groups.google.com/group/comp.lang.c++/t/3acbfe82ba5d0012?hl=en
==============================================================================

== 1 of 6 ==
Date: Tues, Jan 26 2010 8:31 am
From: aaragon


Hello everyone,

I'm experimenting with binary files, and it seems pretty
straightforward. It seems that I have to use the read and write member
functions, inherited by ostream. However, there is something I really
don't like about using this function and I want to know if there is a
way to avoid it.

Let's say you're writing an double to the binary file, a very simple
example:

int main() {

std::ofstream fout("test.out", std::ofstream::binary);
double test = 10.;
fout.write((char*)&test, sizeof(double));
fout.close();

return 0;
}

I hate to see the C cast in the write function. Is there a way to
better write this line? I really want to avoid the C way of casting.

Thank you all,

aa


== 2 of 6 ==
Date: Tues, Jan 26 2010 9:31 am
From: Öö Tiib


On Jan 26, 6:31 pm, aaragon <alejandro.ara...@gmail.com> wrote:
> Hello everyone,
>
> I'm experimenting with binary files, and it seems pretty
> straightforward. It seems that I have to use the read and write member
> functions, inherited by ostream. However, there is something I really
> don't like about using this function and I want to know if there is a
> way to avoid it.
>
> Let's say you're writing an double to the binary file, a very simple
> example:
>
>                  int main() {
>
>                         std::ofstream fout("test.out", std::ofstream::binary);
>                         double test = 10.;
>                         fout.write((char*)&test, sizeof(double));

You can use C++ cast there:
fout.write( reinterpret_cast<char*>(&test),
sizeof(double) );

>                         fout.close();

close() is not needed, ofstream destructor does it.
>
>                         return 0;
>                   }
>
> I hate to see the C cast in the write function. Is there a way to
> better write this line? I really want to avoid the C way of casting.
>

Bare binary form is probably bad idea to serialize something. On
platform you write the sizes alignments and so on may be different
than on platform you read.

Better write some supporting information too, what it is and how lot
of bytes it contains (it is called TLV tag-length-value). Use memcpy()
to copy all such information into char vector and then write it to
file all at once. memcpy() takes void* and you must not cast anything
into void* that does not do so silently.


== 3 of 6 ==
Date: Tues, Jan 26 2010 9:34 am
From: red floyd


On Jan 26, 8:31 am, aaragon <alejandro.ara...@gmail.com> wrote:
> Hello everyone,
>
> I'm experimenting with binary files, and it seems pretty
> straightforward. It seems that I have to use the read and write member
> functions, inherited by ostream. However, there is something I really
> don't like about using this function and I want to know if there is a
> way to avoid it.
>
> Let's say you're writing an double to the binary file, a very simple
> example:
>
>                  int main() {
>
>                         std::ofstream fout("test.out", std::ofstream::binary);
>                         double test = 10.;
>                         fout.write((char*)&test, sizeof(double));
>                         fout.close();
>
>                         return 0;
>                   }
>
> I hate to see the C cast in the write function. Is there a way to
> better write this line? I really want to avoid the C way of casting.
>

look up static_cast<>


== 4 of 6 ==
Date: Tues, Jan 26 2010 11:23 am
From: "John H."


On Jan 26, 10:31 am, aaragon <alejandro.ara...@gmail.com> wrote:
>                         double test = 10.;
>                         fout.write((char*)&test, sizeof(double));
> Is there a way to
> better write this line? I really want to avoid the C way of casting.

You might leverage the << operator:

#include <fstream>

int main()
{
std::ofstream fout("Test.out", std::ofstream::binary);
double test_out = 10.;

//fout.write((char*)&test_out, sizeof(double));
fout << test_out;
fout.close();

std::ifstream fin("Test.out", std::ifstream::binary);
double test_in = 0.0;
fin >> test_in;

return 0;
}


== 5 of 6 ==
Date: Tues, Jan 26 2010 11:50 am
From: Victor Bazarov


John H. wrote:
> On Jan 26, 10:31 am, aaragon <alejandro.ara...@gmail.com> wrote:
>> double test = 10.;
>> fout.write((char*)&test, sizeof(double));
>> Is there a way to
>> better write this line? I really want to avoid the C way of casting.
>
> You might leverage the << operator:
>
> #include <fstream>
>
> int main()
> {
> std::ofstream fout("Test.out", std::ofstream::binary);
> double test_out = 10.;
>
> //fout.write((char*)&test_out, sizeof(double));
> fout << test_out;
> fout.close();
>
> std::ifstream fin("Test.out", std::ifstream::binary);
> double test_in = 0.0;
> fin >> test_in;
>
> return 0;
> }

The fact that round trip delivers the same value (likely) does not
actually demonstrate the writing to the binary file. operator<<
converts the internal representation of the double into a string of
characters and then writes that out. That, IIUIC, is not what the OP
wanted.

To OP: don't do C style casting, do 'static_cast'. That's what it is
for (among other things).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


== 6 of 6 ==
Date: Tues, Jan 26 2010 12:51 pm
From: "John H."


On Jan 26, 1:50 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> operator<<
> converts the internal representation of the double into a string of
> characters and then writes that out.

That is correct. Don't use the ofstream::operator<<(double) to
achieve binary output.

==============================================================================
TOPIC: How to exclude some .CPP files from being linked in Visual Studio?
http://groups.google.com/group/comp.lang.c++/t/bf9a0fa82529e4bc?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 8:58 am
From: smart


Hello everyone:

I'm working with Visual Studio 2008, and I have multiple CPP files in
the project. I'd like to compile these files, but not to link them,
because they are all MATLAB interface functions with the same function
names. Linking these functions cause the linker to complain, which is
not a big problem. However, I still want to fix it. Is there any way
to tell the linker to avoid certain object files?

Thanks.

== 2 of 2 ==
Date: Tues, Jan 26 2010 9:34 am
From: red floyd


On Jan 26, 8:58 am, smart <smartn...@gmail.com> wrote:
> Hello everyone:
>
> I'm working with Visual Studio 2008, and I have multiple CPP files in
> the project. I'd like to compile these files, but not to link them,
> because they are all MATLAB interface functions with the same function
> names.  Linking these functions cause the linker to complain, which is
> not a big problem. However, I still want to fix it. Is there any way
> to tell the linker to avoid certain object files?

Wrong group. Questions about a specific toolset are better addressed
in
a forum dedicated to your platform. Please try a newsgroup with
"visual
studio" in it's name.

See FAQ 5.9 http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

==============================================================================
TOPIC: Again substrings and so on
http://groups.google.com/group/comp.lang.c++/t/736557e5175db0ba?hl=en
==============================================================================

== 1 of 4 ==
Date: Tues, Jan 26 2010 9:16 am
From: Vicent Giner-Bosch


@Christian and @Victor,

First of all, I didn't mean "wrap" by "trim" --I mixed them up, sorry.
The typical "trim" function which is present at Visual Basic, PL/SQL
Oracle...

Thank you for your feed-back. I think Boost library could be what I am
looking for. Thank you also for the book reference. In fact, I've
never read a whole book about any programming language, but I use to
constantly look up information in many books, tutorials, etc. --and
also at the Internet, of course! But I get your advice.

I've been asking this at comp.lang.c forum too, and I copy here a
more detailed explanation of what I am looking for, which I've already
published there:

What I exactly need to do is the following:

While there are still new lines:

(1) Get one line from a given text file.

(2) In that line, detect a "first" part and a "second part", which are
separated by a "=" symbol.

(3) Take away the possible "blanks" (like a "trim" function would do)
from those parts.

(4) Detect which variable in my program is being referred by the
"first part".

(5) Translate the second part (it is still a "string") into a number.


- About #1 : It can be done by means of standard I/O C libraries. I
guess that there are also ways to do it with C++ libraries.

- About #2 : It would be as simple as: detecting the position of "="
and then get two substrings. I don't understand why this step is so
difficult to perform in C!!!! I mean: there IS a C standard function
for getting the position of a character (it is "strchr"), but not a
function for substring (unless it is a substring that starts at
position 1, which can be done with "strncpy_s"). Is it easier at C++??

- About #3 : I would only need an equivalent of VB's "trim"
function... Is there anything like that at C++?

- About #4 : I can do this by using a "case" or an "if" statement. No
problem at all with this step, provided that "first part" has been
successfully extracted and trimmed.

- About #5 : I hope that a proper casting statement will be enough.
So, do you think that C++ std::string and std:iostream classes are
the right choice for me??


I hope this helps to understand where I am and where I want to go...

Thank you for your valuable feed-back!!!

--
Vicent


== 2 of 4 ==
Date: Tues, Jan 26 2010 9:36 am
From: Christian Hackl


Vicent Giner-Bosch ha scritto:

> I've been asking this at comp.lang.c forum too,

What for? You were looking for a solution in C++, weren't you?

The solution you get in comp.lang.c will work perfectly for C but be an
ugly hack in C++ because it will involve the use of char* instead of
std::string. That's because C and C++ are different programming
languages with different purposes and different philosophies.

> What I exactly need to do is the following:
>
> While there are still new lines:
>
> (1) Get one line from a given text file.

Have a look at the function std::getline. You pass it a std::string and
a std::ifstream, and the next line from the text file will be saved in
the std::string.

> (2) In that line, detect a "first" part and a "second part", which are
> separated by a "=" symbol.

There are a couple of ways to achieve what you want. Have a look at
std::string's member functions substr() and find().

However, you should define these rules with more exceptional cases in
mind. For example, what exactly should the program do if more than one
"=" appears in the line? What should it do if no "=" can be found?

> (3) Take away the possible "blanks" (like a "trim" function would do)
> from those parts.

Boost's string algorithm library has trim functions.

> (5) Translate the second part (it is still a "string") into a number.

Boost also offers boost::lexical_cast for such conversions.

> - About #2 : It would be as simple as: detecting the position of "="
> and then get two substrings. I don't understand why this step is so
> difficult to perform in C!!!!

Because strings are difficult to handle in C. In C++, you have
std::string which does all the work for you. See above.

> - About #5 : I hope that a proper casting statement will be enough.

No, the casting operators provided by standard C++ are not enough.
Again, I recommend boost::lexical_cast.

> So, do you think that C++ std::string and std:iostream classes are
> the right choice for me??

Yes.


--
Christian Hackl
hacki@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiamò, sì!


== 3 of 4 ==
Date: Tues, Jan 26 2010 10:16 am
From: Vicent Giner-Bosch


On 26 ene, 18:36, Christian Hackl <ha...@sbox.tugraz.at> wrote:
> Vicent Giner-Bosch ha scritto:
>
> > I've been asking this at  comp.lang.c  forum too,
>
> What for? You were looking for a solution in C++, weren't you?

Yes, but in fact most of my code is C-compliant, so I thought all my
program could actually feet into C. But now I realize that it is not
necessary and that, in fact, it is convenient for me to use some C++
particular features, like strings.


> The solution you get in comp.lang.c will work perfectly for C but
be an
> ugly hack in C++ because it will involve the use of char* instead of
> std::string. That's because C and C++ are different programming
> languages with different purposes and different philosophies.

Now I get it.


> > (1) Get one line from a given text file.
>
> Have a look at the function std::getline. You pass it a std::string and
> a std::ifstream, and the next line from the text file will be saved in
> the std::string.

So easy... Thank you. I am a bit lost when using classes and so on,
but I know it is easy. Just... I'm not used to.

> > (2) In that line, detect a "first" part and a "second part", which are
> > separated by a "=" symbol.
>
> There are a couple of ways to achieve what you want. Have a look at
> std::string's member functions substr() and find().

Thanks again.


> However, you should define these rules with more exceptional cases in
> mind. For example, what exactly should the program do if more than one
> "=" appears in the line? What should it do if no "=" can be found?

Yes, I already thought about that. I'll deal with it later in my code,
but no problem with that.

> > (3) Take away the possible "blanks" (like a "trim" function would do)
> > from those parts.
>
> Boost's string algorithm library has trim functions.

My question is this: Is Boost string library too "heavy" to include? I
mean, if I only use it "for a while", if I am not going to use it "a
lot", is it worthy to use it? I DO think so, but I would like to read
more opinions...


> > (5) Translate the second part (it is still a "string") into a number.
>
> Boost also offers boost::lexical_cast for such conversions.

> > - About #5 : I hope that a proper casting statement will be enough.
>
> No, the casting operators provided by standard C++ are not enough.
> Again, I recommend boost::lexical_cast.

OK, thank you.


> > So, do you think that C++  std::string  and  std:iostream  classes are
> > the right choice for me??
>
> Yes.
>


Christian, thank you very much!!


== 4 of 4 ==
Date: Tues, Jan 26 2010 11:18 am
From: Christian Hackl


Vicent Giner-Bosch ha scritto:

> My question is this: Is Boost string library too "heavy" to include? I
> mean, if I only use it "for a while", if I am not going to use it "a
> lot", is it worthy to use it? I DO think so, but I would like to read
> more opinions...

Boost is very useful to have around on your computer if you will
continue to write software in C++. Also, you can install it with a nice
installer program on Windows (judging from your messages' headers, you
do use Windows, don't you?), and most of its contents, including string
algorithms and lexical cast, can be used with just #including header
files, so if you have never used a C++ library before, it should be an
easy start.

You can also install Boost libraries separately, i.e. installing only
those libraries you actually need, but this is probably a little more
complicated than just installing all of them.


> Christian, thank you very much!!

You're welcome :)


--
Christian Hackl
hacki@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiamò, sì!

==============================================================================
TOPIC: generate rand number
http://groups.google.com/group/comp.lang.c++/t/fcb11e63c73e0db8?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 9:31 am
From: Andrew Poelstra


On 2010-01-26, Larry <dontmewithme@got.it> wrote:
> Hi,
>
> I need to create a tiny function to generate a random number ranging
> from: 1000000 to 9999999. Also, the number should be unique even if I call
> the function 100 times in a second...can it actually be done?
>
> All I know for the moment is this: srand(time(0)): rand(); but it is not
> that great :-(
>

Depending on how random you need this to be, you could use
something simple, like:
x = 1000000 + (x * Y mod 9000000);
Where x is a static variable that you return with each call,
and Y is anything reasonably big and relatively prime to
9000000.

That will look kinda random, and solve your uniqueness
problem.

This question might better be posed on comp.programming.

== 2 of 2 ==
Date: Tues, Jan 26 2010 12:01 pm
From: Sherm Pendley


"Larry" <dontmewithme@got.it> writes:

> I need to create a tiny function to generate a random number
> ranging from: 1000000 to 9999999.

Have a look at Boost's Random module:

<http://www.boost.org/doc/libs/1_41_0/libs/random/index.html>

It's included in TR1, so depending on your compiler you may be able to
use it even without Boost.

> Also, the number should be unique
> even if I call the function 100 times in a second...

Make a list of all the numbers in the range you need. Shuffle the list;
that is, sort it using a randomized comparitor function. Now you can
simply iterate through the list. This approach has a lot of up-front
overhead to create and sort the list, both memory and processor time,
but gives consistent performance for fetching the next number, no
matter how many times you call it.

Alternatively, you could create a set to hold previously-selected numbers,
and each time you need to select a new one, generate random numbers
until you find one that isn't in the set. That will avoid the up-front
cost of creating the list, but performance will suffer as more items
are added to the already-selected set.

sherm--

==============================================================================
TOPIC: Virtual construtors
http://groups.google.com/group/comp.lang.c++/t/aea7843ff0dd519f?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 10:34 am
From: Mukesh


Why dont we have virtual constructors?


== 2 of 2 ==
Date: Tues, Jan 26 2010 10:49 am
From: SG


On 26 Jan., 19:34, Mukesh <mukeshs...@gmail.com> wrote:
> Why dont we have virtual constructors?

Because it doesn't make any sense. Are you looking for the abstract
factory pattern?

==============================================================================
TOPIC: STL/CLR library in Visual Studio 2008
http://groups.google.com/group/comp.lang.c++/t/2108ce8aea45b62f?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Jan 26 2010 10:59 am
From: Vicent Giner-Bosch


This is a question related with Visual Studio 2008 --I hope you don't
mind. ;-)

At http://msdn.microsoft.com/es-es/library/bb385954.aspx they say
that "The STL/CLR Library is a packaging of the Standard Template
Library (STL), a subset of the Standard C++ Library, for use with C++
and the .NET Framework common language runtime (CLR). With STL/CLR,
you can use all the containers, iterators, and algorithms of STL in a
managed environment."

If I am not going to use .NET features, but only "Visual Studio" ones,
do I need STL/CLR library, or not? What does exactly "a managed
environment" mean??

Thank you for your answers.


== 2 of 2 ==
Date: Tues, Jan 26 2010 11:23 am
From: Christian Hackl


Vicent Giner-Bosch ha scritto:

> This is a question related with Visual Studio 2008 --I hope you don't
> mind. ;-)

Well... yes! :)

This newsgroup is about the C++ language. Visual Studio and .NET are
off-topic here. You should ask in a Microsoft group.


--
Christian Hackl
hacki@sbox.tugraz.at

Milano 2008/2009 -- L'Italia chiamò, sì!

==============================================================================
TOPIC: Memory contents mysteriously changing
http://groups.google.com/group/comp.lang.c++/t/d3d7f50cba06cf27?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 26 2010 11:38 am
From: Mark


>
> Memory management problems show up at places that have nothing to do
> with the spot where the error actually occurred. And since a memory
> management problem typically means that code ends up stomping on memory
> that it shouldn't be touching, the effects can seem random. Swapping two
> lines of code can make the symptoms disappear; commenting out large
> chunks of code can do the same. But often that's just symptoms. The
> underlying problem is still there.

I must admit that everything you said rings of the painful truth.
I've had all of these experiences in the past and the underlying cause
was always memory (mis-)management.

Thanks to Pete, Victor, Kaz, and company for your help. I'll try
using tools like valgrind, which will be a new but worthwhile (and
overdue) learning experience for me.

You all have been generous with your assistance. Thank you!

==============================================================================
TOPIC: std::not1() doesn't accept a function pointer
http://groups.google.com/group/comp.lang.c++/t/940b7aa3b05d8e70?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Jan 26 2010 12:09 pm
From: Juha Nieminen


Basically any STL algorithm which requires a predicate as parameter
can be given either a function object or a regular function as that
parameter. For example, you can give a function object or a regular
function pointer as the third parameter to std::remove_if(), and it
will work ok.

If you want to *negate* the predicate, you can do it with std::not1().
However, for some reason std::not1() can only be called with a function
object, not a function pointer. If you want to use it with a regular
function, you have to do it like std::not1(std::ptr_fun(theFunction)).

But why? Why couldn't std::not1() accept a regular function as parameter?

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

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: