Array of pointers to a vector

Discussion in 'C++' started by Puvendran, Oct 19, 2003.

  1. Puvendran

    Puvendran Guest


    I am trying to create an array of pointers to a vector which holds
    values of a user defined class. The application involved needs
    multiple arrays to point the same vector ie

    class message
    string from;


    void main()
    vector<message>* Arr1[10];
    vector<message>* Arr2[10];

    message m1;

    m1.from = "john";

    I get the following compilation error due to the last line request for member `push_back' in `n[0]', which is of
    type `vector<message,allocator<message> > *'

    What I am trying to achieve in my application is for objects of type
    message to be shared,via pointers, thereby saving memory, rather than
    each array having identical copies of the same object.

    So, what I need is

    1. To be able to add message object to Arr1
    2. Subset of the messages in Arr1 to be referenced by Arr2
    3. Display the contents of Arr1/Arr2.

    Thanking you in advance

    Puvendran, Oct 19, 2003
    1. Advertisements

  2. Puvendran

    Jonas Mellin Guest

    Should it not be "Arr1[0]->push_back(&m1);"?

    While I am at it, I recommend shared pointers instead of pure pointer.
    Avoids a lot of hassle together with templates while scaling the same
    way as pure pointer application as well as not adding an overly large
    overhead to your application compared to using pure pointers.
    So, arr1 is a queue of messages and arr2 should be a subset of arr1
    queue. Is that what you want? Why do you have an array of such queues?
    Do you want to handle multiple queues?
    Jonas Mellin, Oct 19, 2003
    1. Advertisements

  3. Puvendran

    JesseChen Guest


    I have serveral doubts here:
    If as you coded ,why not be Array[0]->push_back(&m1); ?

    I think once you get or duplicate the pointer to an object, you can handle
    the object.

    JesseChen, Oct 19, 2003
  4. Puvendran

    Puvendran Guest

    Basically the program reads a file containig mail messages. You should
    be able to specify different views on the contents eg by who sent the
    mail, subject matter etc. So, to minimise memory impact I did not want
    multiple copies of these , sometimes large , mail messages. Thus the
    idea of a vector of pointers.
    Reason for vector was that different mailboxes could have different
    number of messages so I needed the ability (in effect) to resize the

    I tried the suggestion but got the following error. In function `int main(...)': no matching function for call to
    (const message &)
    Puvendran, Oct 20, 2003
  5. Does the following help ?

    #include <iostream>
    #include <string>
    #include <list>
    #include <vector>
    #include <algorithm>

    using namespace std;

    class Message
    Message( std::string From, std::string Subject, std::string Body )
    : m_From( From ), m_Subject( Subject ), m_Body( Body )

    void Print()
    { cout << "***************************\n"
    << "From : " << m_From << "\n"
    << "Subject : " << m_Subject << "\n"
    << "Body : " << m_Body << "\n";

    friend bool SortByFrom( const Message* rhs, const Message* lhs );
    friend bool SortBySubject( const Message* rhs, const Message* lhs );

    std::string m_From;
    std::string m_Subject;
    std::string m_Body;

    bool SortByFrom( const Message* lhs, const Message* rhs )
    return lhs->m_From < rhs->m_From;

    bool SortBySubject( const Message* lhs, const Message* rhs )
    return lhs->m_Subject < rhs->m_Subject;

    int main()
    list< Message > TheMessages;
    vector< Message* > Sorted[2];

    // strore the messages somewhere
    TheMessages.push_back( Message( "Kalle", "Your Problem", "This should do it" ) );
    TheMessages.push_back( Message( "Tom", "Time?", "Hi. Do you have time?" ) );
    TheMessages.push_back( Message( "Agnes", "[Re]Time?", "No. I don't?" ) );
    TheMessages.push_back( Message( "Bart", "Universe", "42" ) );

    // fetch the pointers from the original messages
    // and store them in the vectors. Note: Since I have
    // stored the messages in a list rather then in a vector
    // the pointers will remain valid, even if the list gets
    // larger
    for( list<Message>::iterator il = TheMessages.begin();
    il != TheMessages.end();
    il++ ) {
    Sorted[0].push_back( &(*il) );
    Sorted[1].push_back( &(*il) );

    // now sort the arrays holding the pointers
    std::sort( Sorted[0].begin(), Sorted[0].end(), SortByFrom );
    std::sort( Sorted[1].begin(), Sorted[1].end(), SortBySubject );

    cout << "\nSorted by From field\n";
    for( int i = 0; i < Sorted[0].size(); ++i )

    cout << "\nSorted by Subject field\n";
    for( i = 0; i < Sorted[1].size(); ++i )

    return 0;
    Karl Heinz Buchegger, Oct 20, 2003
  6. Puvendran

    Puvendran Guest


    Yes a vector of pointers is the way to go

    So, what I have is

    class message
    string from;


    message::add(string s)
    from = s;

    message* m1;
    vector<message *> m;

    m1=new message();


    This compiles but when I call the add method on pointer object m1, as
    soon as it tries to access data member from in that object I get a
    segmentation violation. I am able to do anything else within the
    pointer object. Sorry I am a newbie and am struggling.

    Puvendran, Oct 22, 2003
  7. .... then it is absolutely important that you post a complete
    compileable program not only fragments. The problem you
    experience may be located somewhere else.

    #include <vector>
    #include <string>
    using namespace std;

    class message
    void add( const string& s )

    string from;

    void message::add( const string& s )
    from = s;

    int main()
    message* m1;
    vector< message *> m;

    m.resize( 10 );

    m1 = new message;
    m1->add( "Hello" );

    m[0] = m1;

    Shouldn't give you this problem. There is still the problem of
    leaking memory, but definitely not segmentation violation.
    Karl Heinz Buchegger, Oct 22, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.