Using/retrieving the internal array of elements from a vector

Discussion in 'C++' started by Vince C., Jul 16, 2007.

  1. Vince C.

    Vince C. Guest

    Hi.

    I'm writing a C++ application that uses GNU getopt_long() to parse command
    line argument syntaxes.

    http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html

    I'd like to check multiple syntaxes hence multiple arrays of items, which
    type are "option", defined in "getopt.h":

    struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
    };

    Since I want to make my program generic, I've used a vector<option> to
    dynamically build an array of options, which I need to pass to function
    getopt_long():

    vector<option> options;

    int getopt_long (int argc, char *const *argv, const char *shortopts, const
    struct option *longopts, int *indexptr)

    My program sucessfully back pushes option items into the vector but
    getopt_long() doesn't seem to react as expected. Since I have no access to
    the vector array data, I just pass &options[0] as the 4th argument,
    longopts.

    Can I be sure &options[0] is actually the address of the beginning of the
    array? In other terms, does vector operator [] return the real adress of an
    element in its container?

    Thanks in advance.

    --

    Vince C.
    Vince C., Jul 16, 2007
    #1
    1. Advertising

  2. Vince C.

    Kai-Uwe Bux Guest

    Vince C. wrote:

    > Hi.
    >
    > I'm writing a C++ application that uses GNU getopt_long() to parse command
    > line argument syntaxes.
    >
    > http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html
    >
    > I'd like to check multiple syntaxes hence multiple arrays of items, which
    > type are "option", defined in "getopt.h":
    >
    > struct option {
    > const char *name;
    > int has_arg;
    > int *flag;
    > int val;
    > };
    >
    > Since I want to make my program generic, I've used a vector<option> to
    > dynamically build an array of options, which I need to pass to function
    > getopt_long():
    >
    > vector<option> options;
    >
    > int getopt_long (int argc, char *const *argv, const char *shortopts, const
    > struct option *longopts, int *indexptr)
    >
    > My program sucessfully back pushes option items into the vector but
    > getopt_long() doesn't seem to react as expected. Since I have no access to
    > the vector array data, I just pass &options[0] as the 4th argument,
    > longopts.
    >
    > Can I be sure &options[0] is actually the address of the beginning of the
    > array?


    Yes, std::vector is guaranteed to be contiguous (with the exception of
    vector<bool>). Also, &options[0] will give you a pointer to the first
    element.


    > In other terms, does vector operator [] return the real adress of
    > an element in its container?


    Not quite, operator[] returns a reference or a const-reference. It does not
    return an address.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 16, 2007
    #2
    1. Advertising

  3. Vince C.

    Sarath Guest


    > Can I be sure &options[0] is actually the address of the beginning of the
    > array? In other terms, does vector operator [] return the real adress of an
    > element in its container?


    Yes it is. operator[] returns the reference to the object. You can
    take it's adress.
    The vector allocation is contiguous so that you can easily access it
    as array.

    Before passing the elements to any function make sure that the vector
    is not empty else it may produce some undesired result. also you have
    to take care that, you should not overrite or use the reserved area of
    vector.

    dequeue is a container which has the close implementation of vector.
    but the underlying memory is not contiguous. So you should not use
    deque instead of vector.


    Regards,
    Sarath
    http://sarathc.wordpress.com/
    Sarath, Jul 16, 2007
    #3
  4. Vince C.

    Vince C. Guest

    @Kai-Uwe Bux:
    @Sarath:

    Thanks for your lights. I've also tried with a static array of options, not
    using a vector and I get the same behaviour: getopt_long() parses the array
    of options only the first time (i.e. when parsing the first syntax, the
    first array of options). If I iterate another array of options
    getopt_long() immediately returns -1 and doesn't parse the array.

    I'll post a new message on that particular topic.

    Thanks again.

    --

    Vince C.
    Vince C., Jul 16, 2007
    #4
    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. pmatos
    Replies:
    6
    Views:
    23,723
  2. P
    Replies:
    1
    Views:
    1,144
    Joe Kesselman
    Jul 7, 2006
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    542
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    344
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page