468,458 Members | 1,856 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

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

Jul 22 '05 #1
7 5413

"Jerry Krinock" <je***@ieee.org> wrote in message
news:BD16186E.1C4E%je***@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
Jul 22 '05 #2
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
Jul 22 '05 #3
On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <je***@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/ ]

Jul 22 '05 #4
On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <je***@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
Jul 22 '05 #5
in article 2l************@uni-berlin.de, Jonathan Turkanis at
te******@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 j4*****************@newssvr17.news.prodigy.com, Phlip at
ph*******@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
jo*************@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

Jul 22 '05 #6
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
Jul 22 '05 #7
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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Ernst Murnleitner | last post: by
8 posts views Thread by Hamish | last post: by
17 posts views Thread by Michael Hopkins | last post: by
9 posts views Thread by kathy | last post: by
32 posts views Thread by zl2k | last post: by
2 posts views Thread by Buck Brown | last post: by
12 posts views Thread by WaterWalk | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.