Thread: Using a vector of pointers in a struct...HELP!

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Using a vector of pointers in a struct...HELP!

    Hello,

    Im trying to define a struct that contains a vector<unsigned char*> :

    Code:
    struct Q 
    {
           vector<unsigned char*> qu;
    };
    I create a Q with the following function:

    Code:
    Q * createQ()
    {
          Q newQ;
          Q* ptr;
          ptr = &newQ;
          return ptr;
    }
    And have a function that adds an unsigned char to it:

    Code:
    void enQ(Q * q, unsigned char b) 
    {
         q->qu.push_back(&b);
    }
    They compile with no errors, but when I use the enQ() function in the following way:

    Code:
    int main()
    {
        Q * ptr1 = createQ();
    
        unsigned char theChar = 'x';
        enQ(ptr1, theChar);
        
        cout << "ptr1[0]: " << ptr1->qu[0] << endl;
    }
    ... I get a runtime error and the console needs to be closed. What am I missing here?

    Thanks in advance,
    J

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Your createQ() function is wrong. You are assigning ptr to newQ which is a temporary variable, it will exist only inside createQ(), not outside. Therefore, what you return with createQ() is a pointer that points to somewhere it doesn't have access. Also, I don't see the point of having a pointer point (pun not intended) to a single character. Finally, you have 'unsigned char' but 'char' is 'unsigned' by default.

    You may want to get rid of your createQ() function. You can create memory for a pointer using the 'new' keyword:
    Code:
    Q* ptr = new Q();
    However, in your example, it is absolutely pointless to do so, if you have only a single element (i.e. an array with only 1 index..) you do not need a pointer but rather a single object declared like this:
    Code:
    Q myObj; // You can give it the name you want

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by misterAction
    What am I missing here?
    A few things.
    Code:
    Q * createQ()
    {
          Q newQ;
          Q* ptr;
          ptr = &newQ;
          return ptr;
    }
    Here you return a pointer to a local object. Its lost afer return of that function.
    you need to
    Code:
    Q * createQ() {
          return new Q;
    }
    a similar problem here
    Code:
    void enQ(Q * q, unsigned char b) 
    {
         q->qu.push_back(&b);
    }
    You try to add a pointer to the function parameter b to the Vector. That again is lost after return of that function.
    something like this would work
    Code:
    void enQ(Q * q, unsigned char b) 
    {
         q->qu.push_back(new char(b));
    }
    And then I don't understand the purpose of putting pointers to single characters into a vector.
    Putting the characters into the vector would not only save space ( 1 byte for the char, and typically 4 bytes for a pointer ) it would also make the purpose of the class clearer. Users would not confuse the pointers to single characters with c-strings.
    Kurt
    EDIT: you beat me.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    Thanks for the quick reply! I know it seems strange to do what Im doing, but my assignment says I can only store the chars in an array called data[2048]. I cant actually store the chars in the vector. The assignment calls for an implementation of variable-length queues, all using the data[] array for storage.

    My idea is to have a struct with the vector of pointers to the data[] array, and also an int id# (which I didnt put in there since I at least have that part working!). The structs will keep track of what indexes of the data array are being used for the given Q. Is this a bad idea?

    -Josh

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 'char' is 'unsigned' by default
    Not true. It depends on the implementation.

    >> Is this a bad idea?
    Instead of holding pointers, just use the indexes with the id# in the struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM