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

Discussion in 'C++' started by Jerry Krinock, Jul 11, 2004.

  1. 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
     
    Jerry Krinock, Jul 11, 2004
    #1
    1. Advertising

  2. Jerry Krinock

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 11, 2004
    #2
    1. Advertising

  3. "Jerry Krinock" <> wrote in message
    news:BD16186E.1C4E%...
    > 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
     
    Jonathan Turkanis, Jul 11, 2004
    #3
  4. Jerry Krinock

    Mark Guest

    On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <>
    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/ ]
     
    Mark, Jul 11, 2004
    #4
  5. On Sun, 11 Jul 2004 05:04:33 GMT, Jerry Krinock <> 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
     
    John Harrison, Jul 11, 2004
    #5
  6. in article , Jonathan Turkanis at
    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$, Phlip at
    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
    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
     
    Jerry Krinock, Jul 11, 2004
    #6
  7. Jerry Krinock

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 11, 2004
    #7
  8. 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
     
    Jerry Krinock, Jul 23, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    8
    Views:
    863
    Dhruv
    Jul 2, 2003
  2. Peter Jansson
    Replies:
    5
    Views:
    6,352
    Ivan Vecerina
    Mar 17, 2005
  3. Anonymous
    Replies:
    20
    Views:
    4,352
    Pete Becker
    Mar 30, 2005
  4. Jason Heyes
    Replies:
    8
    Views:
    737
    Andrew Koenig
    Jan 15, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    962
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page