473,382 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 5676

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
8
by: Hamish | last post by:
I havea program which on execution gives unpredictable behaviour (it shouldn't). In trying to track down the problem, I'm wondering if there is a difference between these two ways of filling a...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
2
by: Buck Brown | last post by:
Hi, I am using Visual C++ 6.0 MFC. I have been trying to build an array of objects. First I tried using CArray but it was just giving me fits. Once I got my copy contructor built so I would not...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
8
by: yomama | last post by:
Hello, I am writing a program in which I take a string (specifically, first and last name with a space in between) from the user and put it into a data member of a class node. I have tried various...
3
by: jason.cipriani | last post by:
I'm in a rather strange situation. I have a class that does some stuff and uses a buffer to hold some data in; the details aren't important but the buffer is an std::vector: class Something {...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.