unsure how to implement MFC CArray with class

M

Mike Wahler

Mike Stenzler said:
All UNREASONABLE questions will be instantly rejected and the supplicant
suitably punished.

Mike aka Senior Vice President & Chief Technology Officer of a company
that manages (very well thankyou) close to a billion (with a B) USD - of
course I have absolutely no qualifications for the job, they just took
pity on me once they realized what a tough time I had programming.

[sigh]

*PLONK*

-Mike
 
M

Mike Stenzler

Default said:
Mike said:
Jonathan Mcdougall wrote:

[snip]
shows us that you don't want to look by yourself, which is not a good
[snip]

Jonathan

BTW- who is "us"? Do you speak for others?

I agree with both Jonathan and WW.

You are behaving like a jackass. There are plenty of people helping
others all the time here.

That is an argumentum ad hominem. In case you don't know what that is
please RTFM. Additionally, I didn't know jackasses can type. I think you
are behaving like an orangutan.
You come in, don't bother lurking or checking the FAQ for topicality,

You're absolutely right - but I already admitted to that transgression
and apologized. Then I moved on and asked a question that WAS topical.
then insult the entire group by stating that they refuse to help people.

Read the thread. Didn't happen. I never said a word about the behaviour
of anyone except those I was in direct communication with. That comment
(see below) was a sarcastic comment directed at one single individual
who took it upon himself to "manage" the newsgroup.
2. If my question is on-topic I can expect to be told to RTFM as this group isn't about helping people.

I didn't ask for a tutorial, I didn't ask for 4 chapters of text, I
didn't ask to understand the entire subject, I didn't ask to get into
this little pissing match - I just asked how to use std::vector to
implement an array of classes. I DEFINITELY did not SAY ANYTHING about
the newsgroup as a whole and the netiquette of it's posters. As evidence
that this question was not so overly broad as to be unanswerable except
by reading several chapters of a book - Jonathan answered my question in
about 20 lines of code and gave me the info I was looking for.
As pointed out, there are a number of tutorials regarding std::vector
and the other standard container classes.

Yes, it was previously mentioned.

Mike Stenzler
 
B

Buster

Mike Stenzler said:
What I'm curious about now is I don't see
any way to batch allocate an array (vector) of a certain size

For default-initialized elements,
std::vector <name_of_type> name_of_vector (size_of_vector);

For copy-initialized elements, I forget which way round it goes
(but it's in the FM :)

Good luck and best regards,
Buster.
 
M

Mike Stenzler

Jonathon-


Jonathan Mcdougall wrote:
[snip]
FYI, I've been here since the start and I answered your questions.

I know you did. I acknowledged that. My thanks were genuine. I just
don't think my question was that broad to get painted with the RTFM
brush. You proved that by answering it with like maybe 20 lines of code.
Yes, I need to do some reading to use this further. But Jeez, lighten up
on people.
Look on Google, it archives all posts, maybe you missed one. Note
that it may take up to 8h for a post to appear there.

I didn't miss any posts, I can see the whole thread here in my news
reader. I DID miss a few smiley's of yours and probably wouldn't have
given YOU any sass if I'd seen them.
This kind of statement ("as this group isn't about helping people.")
is useless and only makes everybody angry. Please, try to
stay polite and think about what you write.

Is calling me a jackass, polite? (Not you - Brian Rodenborn aka
Default_User).

That statement was made as a sarcastic comment on Atilla's behaviour as
newsgroup nazi. I just don't like being shit upon, especially by a herd.

[snip]
But never, _never_ ask a question implying that
you don't have the time to do something so you're asking others.

This is obviosly a terrible crime. Possibly a felony. Please add this
very important requirement to the FAQ so I or others like me who
actually have to do things in life won't make this terrible mistake
again.

It is.

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

I looked but I'm not sure I see that in the faq.

[snip]
That was to be taken with a smile. I think irony does not travel well
by internet.


You're right. It doesn't. I missed the smiley, missed 2 of 'em I think.
Most seemed to have missed my ironies.

Since I've managed to insult 2 fully qualified C++ experts on this
[snip]


That was a joke, noticed the smiley?

Missed it. Sorry.
Well go for it and stop saying these things!! This discussion is *useless*,
people are here to help other people, not to talk like that. So please,
and this applies to *everyone* including me, shut the **** up, let's
forget that thread, and let's start a new one about std::vectors, ok ?
I'm getting tired of that shit.

<snipped>

Jonathan

OK, I'll bite (that's how I got into several people's kill files to
begin with) :)

it seems to me that using this std::vector class to instantiate an array
of class objects isn't too efficient. In C you declare (and allocate
memory for) an array in one shot, then use very efficient direct
addressing (subscript notation, not ptr) to access the array members.
Unfortunately, the array is not dynamically resizeable, so limited in
usage (from my perspective) - I prefer linked lists, usually when I use
an array it's strictly for efficiency. In VB of course, arrays are
resizeable (redim). The MFC CArray class creates an empty array that is
then settable to a number of elements using CArray::SetSize(). So in a
possible implementation for me I could instantiate an array, set it to
some reasonable size, then fetch data into it and if the reasonable size
is exceeded I could use CArray::SetAtGrow() or CArray::Add() to grow the
array record by record. Microsoft claims that using CArray::GetAt() &
CArray::SetAt() to access the array members (each in this case a class)
is just as efficient as the direct addressing of C language.

Now, I DID RTFM as far as the a.pop_back() that you had included in your
snippet, I'm not sure I understand why, but id does solve the problem I
immediately saw of an empty element at .last. I still see (under a
debugger) an empty at .end - not sure if this is a problem. Here's where
a RTFM comment is warranted. What I'm curious about now is I don't see
any way to batch allocate an array (vector) of a certain size - it's
certainly clear that using your method will work, but it grows the array
a record at a time - that seems inefficient compared to the MFC
implementation and certainly compared to raw C arrays. Am I missing
something? Or id this just a tradeoff situation?

Mike
 
W

WW

Mike said:
That statement was made as a sarcastic comment on Atilla's behaviour
as newsgroup nazi. I just don't like being shit upon, especially by a
herd.

You don't need to be shit upon. You smell troll without it. And I have to
inform you that I do not take lightly to be called a nazy. You have crossed
the line.
 
M

Mike Stenzler

Ahh! Thanks.
Mike
For default-initialized elements,
std::vector <name_of_type> name_of_vector (size_of_vector);

For copy-initialized elements, I forget which way round it goes
(but it's in the FM :)

Good luck and best regards,
Buster.
 
J

Jonathan Mcdougall

it seems to me that using this std::vector class to instantiate an array
of class objects isn't too efficient.

For sure arrays are more efficient than std::vector, since it is a
class with some book-keeping. But imho, better safe than sorry.
In C you declare (and allocate
memory for) an array in one shot,

std::vector said:
then use very efficient direct
addressing (subscript notation, not ptr)

v[0]

is best you can have. In the best case, it only redirects to the
array (assuming the internal representation _is_ an array).

The member function at() gives checked access to the vector :

try
{
v.at(10) = 50;
}
catch(std::eek:ut_of_range)
{
// ...
}
to access the array members.
Unfortunately, the array is not dynamically resizeable, so limited in
usage (from my perspective) - I prefer linked lists, usually when I use
an array it's strictly for efficiency.

In that case, check out std::list.

[ot]
Usually, std::vector's internal representation is an array which is
copied around and resized when full. std::list is usually a linked
list. But aside from the speed constraints, this is implementation
defined.
[/ot]
In VB of course, arrays are
resizeable (redim).

In C++ too, but you have got to do that manually.
The MFC CArray class creates an empty array that is
then settable to a number of elements using CArray::SetSize().

std::vector::reserve() is the closest match. It reserves memory
to avoid reallocation.

For example :

std::vector<int> v(10);

this creates a vector of 10 elements. It is correct to do

v[0] = 5;
v[1] = 54;
...
v[9] = 21;

If you want to enlarge the vector, you *must* *add* elements
via push_back() or insert().

v.push_back(12);

Since the vector had only 10 elements, it must grow (create a new
array, copy the data, delete the old one). It could grow by one,
but this would not be very efficient, so it grows by a given
factor, let's say 2. So now our std::vector has 20 elements for
which memory is reserved, but only *11* accessible.

If you know you have a lot of inserts or push_back to do, you
can reserve the memory to avoid reallocation :

v.reserve(100);

Here, the memory is reserved for 100 elements. You can now add
100 elements with no reallocation.

You must understand the difference between capacity (with reserve())
and size (with push_back and insert). reserve() does not create
element, insert and push_back do.
So in a
possible implementation for me I could instantiate an array, set it to
some reasonable size, then fetch data into it and if the reasonable size
is exceeded I could use CArray::SetAtGrow() or CArray::Add() to grow the
array record by record.

std::vector::push_back() adds an element at the end of the vector and
grows it if needed. std::vector::insert() inserts an element at the
given position and grows it if needed.
Microsoft claims that using CArray::GetAt() &
CArray::SetAt() to access the array members (each in this case a class)
is just as efficient as the direct addressing of C language.

Do you know we are talking about less than milli-seconds here? If
speed is important, you can do it in assembly, it could be faster.
But the difference is really not big enough to make a difference
(depending on the context, of course).
Now, I DID RTFM as far as the a.pop_back() that you had included in your
snippet, I'm not sure I understand why, but id does solve the problem I
immediately saw of an empty element at .last. I still see (under a
debugger) an empty at .end - not sure if this is a problem.

I am not sure I am following you here.
Here's where
a RTFM comment is warranted. What I'm curious about now is I don't see
any way to batch allocate an array (vector) of a certain size -

As I said

std::vector<int> v(10); // 10 elements

or

v.reserve(100);

to prevent the reallocation if less than 100 elements are inserted.
it's
certainly clear that using your method will work, but it grows the array
a record at a time - that seems inefficient compared to the MFC
implementation and certainly compared to raw C arrays. Am I missing
something? Or id this just a tradeoff situation?

It would be far more interesting for you to get Josuttis's "The C++
Standard Library". Lots and lots of nice informations in there.


Jonathan
 
M

Mike Wahler

WW said:
Mike said:
[sigh]

*PLONK*

Et tu, Mike? ;-)

:)

What really got me was:

<quote>
"after almost 20 years as a professional programmer writing
in 6 languages under I'm not sure how many different OS
environments..."
</quote>

(obviously an attempt to impress people
and/or engender their 'sympathy')


<quote>
"Having 2 small children and job deadline pressures to meet,
I usually don't have the luxury of poring through docs..."
</quote>

(more childish whining)


So, to Mr. Stenzler, a reality check:

[big breath]

During my just under thirty years as a software professional,
in more languages than I can recall (in excess of two dozen),
with 10+ operating systems, dozens of special purpose libraries,
many special-purpose hardware devices with their idiosyncracies
and outright bugs, in over 20 vertical markets, dealing with many irrational
or unreasonable requests/demands, negotiating with
employers, clients, and vendors, managing subordinates,
administrating computer networks, participating in marketing,
deployment, user training, and customer service, continuing my
education via books, seminars, and consultants, in 7 U.S. states,
and often exhausting business travel (both air and land), I've
never needed to make demands of or abuse others in order to do my
job effectively.

I've never used the circumstances of my personal life (which
as is probably the case with almost anyone, have been occasionally
quite trying) to justify defaulting on my self-reponsibility
or attempting to assign that responsibility to others, in order
to acquire any additional knowledge and/or skills necessary
to to my job.


And I still somehow found time to write this rant. :)


Employer/client: "I need this new technology integrated
into the project."

Employee/consultant: "But I don't know how to do that,
I don't have time to learn, and
I can't find anyone to do it for me."

Employer/client "Help wanted."


You need not reply, Mr. Stenzler, I won't see it.

There, I feel better now. We now return you to
your regularly scheduled programming. :)

-Mike
 
R

Ron Natalie

Jonathan Mcdougall said:
For sure arrays are more efficient than std::vector, since it is a
class with some book-keeping. But imho, better safe than sorry.

For sure, vectors are not slower for most of what you are doing on
a reasonable implementation. This is precisely the reason operator[]
does not do any range checking, etc...
is best you can have. In the best case, it only redirects to the
array (assuming the internal representation _is_ an array).'

Which is no more work than what the compiler does to access
the array in most cases.

If you want to enlarge the vector, you *must* *add* elements
via push_back() or insert().

Or resize().
 
J

Jonathan Mcdougall

int main()
{
vector<my_class> v; // a vector of my_class'es

v.push_back(my_class());
v.push_back(my_class());

// v now contains two objects

v.pop_back();

// v now contains two objects

I just saw that comment, yurk! 'v' contains *one* element now,
not two. pop_back() removes the last element. Sorry.
// inserting a new object in front
v.insert(v.begin(), my_class());

Two elements now.
v[0].f(); // "hello"
v[1].f(); // "hello"
}


Jonathan
 
M

Mike Stenzler

Yeah, that did confuse me a bit, see my email.

There *is* some unusual behaviour or wrong usage on my part under VC++,
it may be have to do with reserve() vs the element allocation of
push_back(). I'll check in the AM at work.

Thanks

Mike

Jonathan said:
int main()
{
vector<my_class> v; // a vector of my_class'es

v.push_back(my_class());
v.push_back(my_class());

// v now contains two objects

v.pop_back();

// v now contains two objects

I just saw that comment, yurk! 'v' contains *one* element now,
not two. pop_back() removes the last element. Sorry.
// inserting a new object in front
v.insert(v.begin(), my_class());

Two elements now.
v[0].f(); // "hello"
v[1].f(); // "hello"
}

Jonathan
 
K

Karl Heinz Buchegger

Mike said:
Does this look like 4 chapters to you? To me it looks like a simple
helpful answer.

It's only 5% you should or need to know about vectors.
The remaining 95% are what is written in those 4 chapters.
But it has the benefit, that if you have read those 4 chapters
you would also know how to work with std::list, std::map, std::multimap, etc ...
Mike aka Senior Vice President & Chief Technology Officer of a company
that manages (very well thankyou) close to a billion (with a B) USD - of
course I have absolutely no qualifications for the job, they just took
pity on me once they realized what a tough time I had programming.

In this NG we don't care who you are or what your position is.
Even if Stroustroup himself posts and is wrong he will
be corrected.
 
M

Mike Stenzler

Karl Heinz Buchegger wrote:

[snip]
It's only 5% you should or need to know about vectors.
The remaining 95% are what is written in those 4 chapters.
But it has the benefit, that if you have read those 4 chapters
you would also know how to work with std::list, std::map, std::multimap, etc ...

understood - my immediate need was just for some syntax on std::vector
or any similar animal (hence the initial misdirected request re CArray),
it would be nice to know all of the rest, but to solve my problem of the
moment I just needed to see if vector would work for me.

In this NG we don't care who you are or what your position is.
Even if Stroustroup himself posts and is wrong he will
be corrected.

It was a joke son, a joke! :) I didn't post the above to appear better
than anyone - just to sarcastically reply to the the poster below who
implies that I'm having such a tough time programming. My point was that
the evidence of my position belied the statement - I've been pretty damn
successful in life writing code. I was also kinda teasing him since he
signs his posts as "WW aka Atilla". However, it's true that position
doesn't directly equate to competence - lots of folks get promoted (or
elected) over their heads, and in the context of a newsgroup I can see
how it could be misinterpreted - so it was a poor choice of words in
this senseless pissing match.
WW wrote:
[snip]
Until someone such as yourself is unable to type in std::vector into his
IDE, move the cursor into it and press F1 someone such as yourself will have
somewhat tough time programming.


Anyway, time to move on..

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top