array of std::string pointers

This is a discussion on array of std::string pointers within the C++ Programming forums, part of the General Programming Boards category; If someone could help me out that would be great! I'm trying to assign values to an array of std::strings, ...

  1. #1
    luc
    luc is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    array of std::string pointers

    If someone could help me out that would be great! I'm trying to assign values to an array of std::strings, declared from gSOAP as follows:
    Code:
    class bla
    { public:
        std::string* *__ptr;
        int __size;
        struct soap *soap;
    };
    Then I try to assign values like this:
    Code:
    bla* user = new bla;
    bla->__size = 5;
    bla->__ptr = (std::string **)soap_malloc(bla->soap, sizeof(std::string *) * bla->__size);
    
    std::vector <std::string> v;
    for (int i=0; i < 5; i++) {
      v.push_back(someFunctionReturningC_STR(i));
      bla->__ptr[i] = &v.back();
    }
    I'm using a vector only because that's the only thing I found that actually compiles, but running this code gives a runtime error; I suspect &v.back() is at fault.

    So I have 2 questions, 1) how do I assign string data to my array? (although in my example the array is of fixed length, in my application the array will not be fixed), and 2) can you get a pointer to a specific element of the vector data? to read only, not to write...

    Thanks

    Luc

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    Code:
    class bla
    { public:
        std::string* *__ptr; // I doubt you intended to have a pointer to a pointer
        ...
    I think that's the real issue with your code. I wonder why you don't have a constructor for bla though. If there is a reason beyond "I don't know what a constructor is," ignore me.

    > 2) can you get a pointer to a specific element of the vector data? to read only, not to write...
    Yes, creating a const pointer to the element is one option.

  3. #3
    luc
    luc is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    the class declaration is a stub automatically generated from a WSDL document using gSOAP, and it works fine for receiving data from the server, but I'm trying to populate the structure to send data back to the server... I just can't figure out how to populate the information back... since it's pointers to pointers I can't just assign values, I need to create something that can be referenced by a pointer... I've never used vectors before but it seemed like a good option.. ?

  4. #4
    luc
    luc is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    I figured it out..

    Code:
    bla* user = new bla;
    bla->__size = 5;
    bla->__ptr = (std::string **)soap_malloc(bla->soap, sizeof(std::string *) * bla->__size);
    
    std::vector <std::string> v;
    for (int i=0; i < 5; i++) 
      v.push_back(someFunctionReturningC_STR(i));
    
    std::vector <std::string>::iterator iv;
    int i = 0;
    for (iv = v.begin(); iv != v.end(); iv++) {
      bla->__ptr[i] = &(*iv);
      i++;
    }
    Not sure if this is the best way, I'm sure there's more than one way to do this...

    what does everyone think? would a list be better? or some other data structure altogether?

    thanks!

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It's not actually 5 is it..

    >> since it's pointers to pointers I can't just assign values, I need to create something that can be referenced by a pointer

    I mean, if it is, you could have just:

    Code:
    for(int i = 0; i < 5; ++i)
    {
        bla->__ptr[i] = new std::string(someFunctionReturningC_STR(i));
    }

  6. #6
    luc
    luc is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    hehe, wow.. look at how simple that is. It works too. Thanks.

    One question, I'm not very familiar with memory management in C, say I do what you suggest, is the memory allocated by the new std::string reserved until the end of the function? Even though it's not specifically assigned to a variable? I know it's assigned to __ptr, but that's just a pointer to an address right?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    It's reserved until you delete it, whenever that may be, or at the end of the program when the OS will recover all memory used by the process.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    luc
    luc is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    ok.. so if I do this..
    Code:
    void function foo() {
      for(int i = 0; i < 5; ++i)
      {
        bla->__ptr[i] = new std::string(someFunctionReturningC_STR(i));
      }
    }
    I need to delete them later? like..

    Code:
      for(int i = 0; i < 5; ++i)
      {
        delete bla->__ptr[i];
      }
    that?

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21