http://groups.google.com/group/comp.lang.c++?hl=en
comp.lang.c++@googlegroups.com
Today's topics:
* How to copy vector? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/4adcee4457a06858?hl=en
* complex struct - 4 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/59f0b47ab3f08848?hl=en
* memory leaks - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/d9b20823062f2cb3?hl=en
* writing binary files - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/3acbfe82ba5d0012?hl=en
* Support for export keyword ? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/0878ed0c9c1ca584?hl=en
* Virtual construtors - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/aea7843ff0dd519f?hl=en
==============================================================================
TOPIC: How to copy vector?
http://groups.google.com/group/comp.lang.c++/t/4adcee4457a06858?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Jan 26 2010 6:02 pm
From: Immortal Nephi
On Jan 26, 7:00 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Immortal Nephi wrote:
> > I create two vector variables. First variable: data is initialized
> > with integers. Second variable: copyData is empty. Both have same
> > sizes.
>
> It is customary to describe what *type* your variables have. 'vector'
> (or, rather, 'std::vector') is a template, which has to be instantiated
> with a type (at least).
>
> > How do I write correct code to copy all elements from data to
> > copyData?
>
> How do you copy a 'double'? You assign it. What about a 'bool'? You
> assign it. Why do you think it's different with 'std::vector'? It's
> not. You simply assign it.
>
> > I don't have to go through iterator loop to copy each
>
> > element.
>
> No, you don't.
>
> > I want to know that vector data should be auto storage in the global
> > function's stack frame.
>
> Huh?
>
> > Then, the return type will be vector to copy
>
> > all elements.
> > If result size is different and is more than 100 elements, will
> > copyData be automatically resized?
>
> Uh... Assignment takes care of everything, if that's what you're asking.
>
>
>
> > vector< int > foo()
> > {
> > int numData = 1;
> > vector< int > result;
> > result.resize( 100 );
>
> > for( vector< int >::iterator it = result.begin();
> > it != result.end(); ++it )
> > *it = numData++;
>
> > return result;
> > }
>
> Looks good. Or, you could simply do
>
> int numData = 1;
> vector<int> result;
> for (int i = 0; i < 100; i++)
> result.push_back(numData++);
>
> (or drop 'numData' altogether and push 'i').
>
>
>
> > int main()
> > {
> > vector< int > copyData;
> > copyData.resize( 100 );
>
> No need to resize.
>
>
>
> > copyData = foo();
>
> That's just fine. Or, you could simply initialise it with the return
> value of 'foo()':
>
> vector<int> copyData = foo();
>
>
>
> > return 0;
> > }
Victor,
Thank you for your assistance. I have another question. First
variable has 640 elements. Second variable has only 64 elements.
They look like ten 2D images with 64 area of elements.
I can't use assignment to copy one out of ten images. Do I need to
use memory address with offset? Or I have to use three dimension
array?
vector< int > foo()
{
static const int TwoDimensionArray = 10;
static const int Width = 8;
static const int Height = 8;
vector< int > result;
result.resize( TwoDimensionArray * Width * Height );
// or vector< int > result[ TwoDimensionArray ][ Width ][ Height ];
for( int i = 0; i != result.size(); ++i )
result.push_back( i );
return result; // I want to copy only one image with 64 elements with
memory address
// or return result[5][0][0];
}
==============================================================================
TOPIC: complex struct
http://groups.google.com/group/comp.lang.c++/t/59f0b47ab3f08848?hl=en
==============================================================================
== 1 of 4 ==
Date: Tues, Jan 26 2010 6:17 pm
From: Yu Han
On 01/27/2010 07:54 AM, Larry wrote:
> "John H." <oldman_fromthec@yahoo.com> ha scritto nel messaggio
> news:c0811d16-94a8-491c-a610-531eb5fac302@c34g2000yqn.googlegroups.com...
>
>> 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.
>
> anyway,before I was doing:
>
> struct buffer
> {
> char data[1024];
> int bytesRecorded;
> buffer(const char * data_, const int bytesRecorded_) :
> bytesRecorded(bytesRecorded_)
> {
> copy(data_, data_ + (bytesRecorded_ * sizeof(char)), data);
> }
> };
>
> int main()
> {
> circular_buffer<buffer> cb(numbuff);
>
> // Insert elements
> cout << "Push elements:" << endl;
> for(int i = 0; i < 10; i++)
> {
> char szTime[30]; getDateTime(szTime);
>
> cb.push_back( buffer(szTime, 30) );
>
> cout << szTime << endl;
>
> Sleep(1000);
> }
>
> // Show elements:
> cout << "Show elements:" << endl;
> for(int i = 0; i<(int)cb.size(); i++)
> {
> cout << &cb[i].data[0] << endl;
> }
>
> system("pause");
> return EXIT_SUCCESS;
> }
>
> that worked great! But now I needed to to something like:
>
> std::map<int, circular_buffer<buffer> cb(numbuff)> users
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What you give is an object, not a type. only circular_buffer<buffer>
Again, you *must* use a pointer(i.e, circular_buffer<buffer>*), or your
application will not work!
>
> which is impossible, that's why I thought I'd wrap up into another struct:
>
> struct circular
> {
> circular_buffer<buffer> cb[numbuff];
> };
>
> so that I should do:
>
> std::map<int, circular> users
>
> but that led me to some problems...
You wrote too many C codes...
and, the buffer struct is too complex
I think what you need is a std::string instead of buffer
--
Yu Han
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
== 2 of 4 ==
Date: Tues, Jan 26 2010 7:06 pm
From: "Larry"
"Yu Han" <hanjunyu@163.com> ha scritto nel messaggio
news:hjo7uh$2rje$1@adenine.netfront.net...
> What you give is an object, not a type. only circular_buffer<buffer>
> Again, you *must* use a pointer(i.e, circular_buffer<buffer>*), or your
> application will not work!
I read up a little better on boost::circular_buffer so I can now do like
this:
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;
};
int main()
{
map<int, circular> users;
int regkey = 1000;
circular k;
k.cb.set_capacity(numbuff); // Finally!
// Prepare buffer
for(int i = 0; i<numbuff; i++)
{
k.cb.push_back(buffer(NULL,0,0));
}
// Add buffer
k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
// Push in the map
users.insert(make_pair(regkey, k));
// Show map: element regkey
// First element
cout << users[regkey].cb.at(0).user << endl; // 1
// Last element
cout << users[regkey].cb.at(numbuff-1).user << endl; // 3
// Remove from map
users.erase(regkey);
}
I cannot use std::string since I need to deal with binary data
== 3 of 4 ==
Date: Tues, Jan 26 2010 8:57 pm
From: Yu Han
On 01/27/2010 11:06 AM, Larry wrote:
> "Yu Han" <hanjunyu@163.com> ha scritto nel messaggio
> news:hjo7uh$2rje$1@adenine.netfront.net...
>
>> What you give is an object, not a type. only circular_buffer<buffer>
>> Again, you *must* use a pointer(i.e, circular_buffer<buffer>*), or
>> your application will not work!
>
> I read up a little better on boost::circular_buffer so I can now do like
> this:
>
> 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;
> };
>
> int main()
> {
> map<int, circular> users;
>
> int regkey = 1000;
>
> circular k;
> k.cb.set_capacity(numbuff); // Finally!
>
> // Prepare buffer
> for(int i = 0; i<numbuff; i++)
> {
> k.cb.push_back(buffer(NULL,0,0));
> }
>
> // Add buffer
> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
>
> // Push in the map
> users.insert(make_pair(regkey, k));
>
> // Show map: element regkey
>
> // First element
> cout << users[regkey].cb.at(0).user << endl; // 1
>
> // Last element
> cout << users[regkey].cb.at(numbuff-1).user << endl; // 3
>
> // Remove from map
> users.erase(regkey);
> }
>
> I cannot use std::string since I need to deal with binary data
Do you? I don't think so.
You mean getDateTime? What about this:
std::string getDateTime()
{
char szTime[30] = { 0 };
time_t rawtime = time(NULL);
struct tm timeinfo;
gmtime_s(&timeinfo, &rawtime);
strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
return std::string(szTime);
}
what you really need is good encapsulation.
--
Yu Han
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
== 4 of 4 ==
Date: Tues, Jan 26 2010 9:13 pm
From: Yu Han
On 01/27/2010 12:57 PM, Yu Han wrote:
> On 01/27/2010 11:06 AM, Larry wrote:
>> "Yu Han" <hanjunyu@163.com> ha scritto nel messaggio
>> news:hjo7uh$2rje$1@adenine.netfront.net...
>>
>>> What you give is an object, not a type. only circular_buffer<buffer>
>>> Again, you *must* use a pointer(i.e, circular_buffer<buffer>*), or
>>> your application will not work!
>>
>> I read up a little better on boost::circular_buffer so I can now do like
>> this:
>>
>> 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;
>> };
>>
>> int main()
>> {
>> map<int, circular> users;
>>
>> int regkey = 1000;
>>
>> circular k;
>> k.cb.set_capacity(numbuff); // Finally!
>>
>> // Prepare buffer
>> for(int i = 0; i<numbuff; i++)
>> {
>> k.cb.push_back(buffer(NULL,0,0));
>> }
>>
>> // Add buffer
>> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,1));
>> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,2));
>> k.cb.push_back(buffer((unsigned char*)"hellow world!", 15,3));
>>
>> // Push in the map
>> users.insert(make_pair(regkey, k));
>>
>> // Show map: element regkey
>>
>> // First element
>> cout << users[regkey].cb.at(0).user << endl; // 1
>>
>> // Last element
>> cout << users[regkey].cb.at(numbuff-1).user << endl; // 3
>>
>> // Remove from map
>> users.erase(regkey);
>> }
>>
>> I cannot use std::string since I need to deal with binary data
even you do need. you can use vector<char> instead.
> Do you? I don't think so.
> You mean getDateTime? What about this:
> std::string getDateTime()
> {
> char szTime[30] = { 0 };
> time_t rawtime = time(NULL);
> struct tm timeinfo;
> gmtime_s(&timeinfo, &rawtime);
> strftime(szTime, 30, "%a, %d %b %Y %X GMT", &timeinfo);
> return std::string(szTime);
> }
>
> what you really need is good encapsulation.
>
--
Yu Han
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
==============================================================================
TOPIC: memory leaks
http://groups.google.com/group/comp.lang.c++/t/d9b20823062f2cb3?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Jan 26 2010 6:35 pm
From: "Alf P. Steinbach"
* James Kanze:
> On Jan 25, 7:42 pm, "Alf P. Steinbach" <al...@start.no> wrote:
>> * Larry:
>>> "Alf P. Steinbach" <al...@start.no> ha scritto nel messaggio
>>> news:hjki9g$rc0$1@news.eternal-september.org...
> [...]
>> More like
>
>> typedef unsigned char Byte;
>
>> struct Something
>> {
>> vector<Byte> bytes;
>> int userId;
>> // Constructor etc.
>> };
>
>>> what the difference between:
>>> --> vector<unsigned char> vuChar;
>>> and
>>> --> unsigne char buffer[]
>
>> The declaration
>
>> unsigned char buffer[51];
>
>> says that 'buffer' is a contiguous area of memory consisting
>> of 51 elements each of type 'unsigned char'.
>
>> The declaration
>
>> vector<unsigned char> v( 51 );
>
>> says that 'v' is a 'vector' which internally holds a pointer
>> to a buffer, and that on construction it should create a
>> buffer of 51 elements. This can be resized (if necessary the
>> vector will discard the original buffer and copy everything
>> over to a and sufficiently larger buffer). And you can assign
>> to 'v' and generally copy 'v', and be sure that there is no
>> memory leak.
>
> In fact, "vector< unsigned char >" is full-fledged type, which
> behaves like any other type, where as "unsigned char buffer[ 51 ]"
> creates an object of a second class type, which is fairly
> broken, and doesn't behave normally.
I probably agree with what you mean, but the wording above might give the wrong
impression to the OP or other readers.
The type of "unsigned char buffer[52]" is technically a full-fledged type. It's
just that, as you note, this type is broken, due to C compatibility. So with
respect to what one can do and with respect to type safety it's not quite up to par.
In particular, regarding technical type full fledgeness :-) (not you don't know
this, but to avoid any misunderstanding), given
unsigned char buffer[51];
one might apply typeid(), or let some templated function infer the type,
whatever -- with respect to the C++ type system there are no not full fledged
types except incomplete types (types where sizeof cannot be applied) and perhaps
to some degree local classes because they don't have linkage and so do not go
well with templating.
> Also, vector< unsigned char > will normally do bounds checking,
> and other important things to avoid undefined behavior.
>
> There are, of course, cases where something like "unsigned char
> buffer[ 51 ]" is justified, but they aren't that common.
Yes, agreed.
Cheers,
- Alf
==============================================================================
TOPIC: writing binary files
http://groups.google.com/group/comp.lang.c++/t/3acbfe82ba5d0012?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Jan 26 2010 7:28 pm
From: tonydee
On Jan 27, 1:31 am, aaragon <alejandro.ara...@gmail.com> wrote:
> fout.write((char*)&test, sizeof(double));
> 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.
I guess you're wondering if there's a way to improve on the write()
call, rather than adding extra cruft, but FWIW:
template <typename T>
void binary_write(std::ostream& os, const T& t)
{
os.write((const char*)&t, sizeof t);
}
(If it then bothers you that it's not a member of ofstream, then you
can derive from ofstream and add your function, though I wouldn't
recommending it.)
Cheers,
Tony
==============================================================================
TOPIC: Support for export keyword ?
http://groups.google.com/group/comp.lang.c++/t/0878ed0c9c1ca584?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Jan 26 2010 8:37 pm
From: Jerry Coffin
In article <hjme2i$4r7$1@news.albasani.net>, vladaspams@gmail.com
says...
>
> Jerry Coffin wrote:
> > In article <4b56ed82$0$283$14726298@news.sunsite.dk>,
> > terminatorul@gmail.com says...
> >
> > [ ... ]
> >
> >> Why do people bother to even mention/talk about such pathetic excuses
> >> for a compiler ?
> >
> > Producing C as its output doesn't make Comeau any less a compiler
> > than producing object code would. Your claim only shows that you
> > don't know what you're talking about.
> >
> > From a practical viewpoint, its use of C as an intermediate language
> > isn't normally visible at all. You run the compiler, and you get an
> > executable file as output.
>
> Sorry for my ignorance, but how do you debug with such compiler?
Just about like usual -- it inserts #file and #line directives into
the C file it produces, and the C compiler passes those through to
object files, so the debugger knows what parts of what source files
are associated with what object code, so it can display the
appropriate lines in the source file as it executes.
--
Later,
Jerry.
==============================================================================
TOPIC: Virtual construtors
http://groups.google.com/group/comp.lang.c++/t/aea7843ff0dd519f?hl=en
==============================================================================
== 1 of 1 ==
Date: Tues, Jan 26 2010 10:24 pm
From: tonydee
On Jan 27, 3:34 am, Mukesh <mukeshs...@gmail.com> wrote:
> Why dont we have virtual constructors?
Been a few comments about this not making sense, but to elaborate with
a framework that might make it easier to explain what you had in mind,
or realise why it might not make sense...
class Fruit
{
Fruit() { }
virtual ~Fruit() { }
};
class Orange : public Fruit
{
Orange() { std::cout << "Orange()\n"; }
}
class Pear : public Fruit
{
Pear() { std::cout << "Pear()\n"; }
}
...then the Pear constructor can be invoked by...
new Pear(); // a new object on the heap
new (address) Pear(); // a new object at an arbitrary memory
location
Pear(); // a local stack-based temp
Pear a; // a local stack-based variable
In any of these situations, the Pear's constructor is called without
any need for "virtual", but the actual type Pear class must be
specified. Perhaps you're hoping a virtual constructor would remove
that latter restriction? That's kind of what virtual functions do in
general... let you invoke an Orange or Pear function when you only
know you have a Fruit. But remember that any given Fruit only becomes
an Orange or Pear at the time it's constructed. virtual functions
just harken back to the earlier determination. If you're calling the
constructor saying "make me a Fruit", you can't magically expect the
compiler to intuit that you felt like an Orange rather than a
Pear. :-)
Hence the FAQ's Pear* clone() and static Pear* create() suggestions...
they use the language's "covariant return type" tolerance (so Pear*
successfully overloads Fruit*), and allow an existing object's runtime
type to determine the new object's type, even though you're referring
to the existing object via a general Fruit*. That's about the most
abstract way of specifying the type to be created. If that's not
enough, and you don't have an existing object of the right type, then
you have to create a factory function, returning Fruit*, with a switch
or if/else statement to run the "return new Orange()" or "return new
Pear()" as you feel appropriate....
Cheers,
Tony
==============================================================================
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:
Post a Comment