Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::vector as a class data member. Is this wrong? (http://www.velocityreviews.com/forums/t284294-std-vector-as-a-class-data-member-is-this-wrong.html)

Jerry Krinock 07-11-2004 05:04 AM

std::vector as a class data member. Is this wrong?
 
I've declared a class that has some std::vector data members like this:

class MyClass
{
public:
...
std::vector<Apples> apples ;
...
private:
...
std::vector<Oranges> oranges ;
...
}

This program often crashes when I call on my "apples" or "oranges".

Now, I was thinking that maybe the problem is that I declare the instance of
MyClass using "new", so it is on the heap, but I do not use "new" when
declaring the apples and oranges; they look like they are on the stack, yet
they are a member of something which is on the heap. I'm not sure where
they are!

Is there something inherently wrong with the above declaration?

THANKS!

Jerry Krinock
San Jose, CA USA


Phlip 07-11-2004 05:18 AM

Re: std::vector as a class data member. Is this wrong?
 
Jerry Krinock wrote:

> I've declared a class that has some std::vector data members like this:
>
> class MyClass
> {
> public:
> ...
> std::vector<Apple> apples ;
> ...
> private:
> ...
> std::vector<Orange> oranges ;
> ...
> }


> Is there something inherently wrong with the above declaration?


What's wrong is apples contains the potential to store Apples, but it has
none.

To push them in, do this:

MyClass aObject;
aObject.apples.push_back(Apple("granny smith"));
aObject.apples.push_back(Apple("crab"));
aObject.apples.push_back(Apple("macintosh"));

Now indices 0, 1 and 2 work in aObject.apples[x]. But before pushing
anything back, any index might crash.

In future, if you report your code crashes, paste in the code that actually
crashes, not just the code that declares data structures associated with the
crash.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces



Jonathan Turkanis 07-11-2004 05:22 AM

Re: std::vector as a class data member. Is this wrong?
 

"Jerry Krinock" <jerry@ieee.org> wrote in message
news:BD16186E.1C4E%jerry@ieee.org...
> I've declared a class that has some std::vector data members like

this:
>
> class MyClass
> {
> public:
> ...
> std::vector<Apples> apples ;
> ...
> private:
> ...
> std::vector<Oranges> oranges ;
> ...
> }
>
> This program often crashes when I call on my "apples" or "oranges".


What does 'call on' mean here?

> Is there something inherently wrong with the above declaration?


Assuming Apples and Oranges meet the requirements for being stored in
a std::vector (CopyConstructible and Assignable), the only thing that
is (possibly) wrong with the above is that apples is public. But this
won't cause a crash. ;-)

You need to post more code.

Jonathan



Mark 07-11-2004 05:25 AM

Re: std::vector as a class data member. Is this wrong?
 
On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <jerry@ieee.org>
wrote:

[..]

>Now, I was thinking that maybe the problem is that I declare the instance of
>MyClass using "new", so it is on the heap, but I do not use "new" when
>declaring the apples and oranges; they look like they are on the stack, yet
>they are a member of something which is on the heap. I'm not sure where
>they are!
>
>Is there something inherently wrong with the above declaration?


Hard to tell. You might want to post more code. Is apples & oranges
copy constructable and assignable?

Mark
--
[ C++ FAQ: http://www.parashift.com/c++-faq-lite/ ]


John Harrison 07-11-2004 06:00 AM

Re: std::vector as a class data member. Is this wrong?
 
On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <jerry@ieee.org> wrote:

> I've declared a class that has some std::vector data members like this:
>
> class MyClass
> {
> public:
> ...
> std::vector<Apples> apples ;
> ...
> private:
> ...
> std::vector<Oranges> oranges ;
> ...
> }
>
> This program often crashes when I call on my "apples" or "oranges".
>
> Now, I was thinking that maybe the problem is that I declare the
> instance of
> MyClass using "new", so it is on the heap, but I do not use "new" when
> declaring the apples and oranges; they look like they are on the stack,
> yet
> they are a member of something which is on the heap. I'm not sure where
> they are!


No that's wrong, they are on the heap. The are contained within an object
which you allocated on the heap, therefore they are on the heap.

>
> Is there something inherently wrong with the above declaration?
>


No, your problem is elsewhere. It's a common thing with newbie posts to
this group, they post the wrong code. To get quick help on this group post
*small* *complete* programs, not snippetts of code. Unfortunately this
seems too difficult for most people to do, so we have to go round the
houses. There are several different possibilites for what is wrong with
your code.

john

Jerry Krinock 07-11-2004 03:45 PM

Re: std::vector as a class data member. Is this wrong?
 
in article 2lc0orFat0kvU1@uni-berlin.de, Jonathan Turkanis at
technews@kangaroologic.com wrote on 04/07/10 22:22:

>> This program often crashes when I call on my "apples" or "oranges".

>
> What does 'call on' mean here?


Sorry, I should have said "access" or "read".

> Assuming Apples and Oranges meet the requirements for being stored in
> a std::vector (CopyConstructible and Assignable),


This might be my problem, because they contain Objective-C objects (it's a
Macintosh program).

> the only thing that
> is (possibly) wrong with the above is that apples is public.
>
> But this won't cause a crash. ;-)


Yes, I put that public member in there for some sleazy debugging ;).

in article j44Ic.3201$sa1.1219@newssvr17.news.prodigy.com, Phlip at
phlip_cpp@yahoo.com wrote on 04/07/10 22:18:

> But before pushing anything back, any index might crash.


I understand that, but, for instance, a push_back() or size() should not
crash, which it does.

in article opsaylndpg212331@andronicus, John Harrison at
john_andronicus@hotmail.com wrote on 04/07/10 23:00:

> ...they are on the heap. The are contained within an object
> which you allocated on the heap, therefore they are on the heap.


THANKS!

>> Is there something inherently wrong with the above declaration?

>
> No, your problem is elsewhere.


Thanks very much for all the answers. I know I did not post much code, but
I didn't want to bother everyone with actually fixing the problem. I just
wanted to know if the problem was here or somewhere else. I think I've got
my answer...that it is somewhere else, maybe in my bastardizing of C++ with
Objective-C.

Jerry


Phlip 07-11-2004 03:49 PM

Re: std::vector as a class data member. Is this wrong?
 
Jerry Krinock wrote:

> Thanks very much for all the answers. I know I did not post much code,

but
> I didn't want to bother everyone with actually fixing the problem. I just
> wanted to know if the problem was here or somewhere else. I think I've

got
> my answer...that it is somewhere else, maybe in my bastardizing of C++

with
> Objective-C.


Objective-C is two languages trying to become one.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces



Jerry Krinock 07-23-2004 12:42 PM

Re: std::vector as a class data member. Is this wrong? - resolution
 
Yes, it had something to do with the mixing of C++ and Objective-C. I
rewrote the class as an Objective-C class, and also had to make that
std::vector an NSArray, and then the crashes stopped. I still have a
couple other std::vectors in that Objective-C class, but it doesn't
seem to mind that, probably because these other vectors are not
referenced outside the class.

I think I've learned enough. Thanks for all the help,

Jerry


All times are GMT. The time now is 05:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.