Vector as array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Vinokur

    #1

    Vector as array

    Let v be an vector; for instance:
    std::vector<dou blev;

    Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?

    In other words,
    Let foo() have the following prototype:
    void (double* p, std::size_t arrarySize);

    Is this calling is correct:
    foo (&v[0], v.size()) ?

    Alex Vinokur

  • Kai-Uwe Bux

    #2
    Re: Vector as array

    Alex Vinokur wrote:
    Let v be an vector; for instance:
    std::vector<dou blev;
    >
    Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
    >
    In other words,
    Let foo() have the following prototype:
    void (double* p, std::size_t arrarySize);
    >
    Is this calling is correct:
    foo (&v[0], v.size()) ?
    I think that is what the standard means by calling the memory of a
    vector "contiguous ". The precise guarantee is for types other that bool
    [23.2.4/1]:

    ... The elements of a vector are stored contiguously, meaning that if v is
    a vector<T, Allocatorwhere T is some type other than bool, then it obeys
    the identity &v[n] == &v[0] + n for all 0 <= n < v.size().


    Best

    Kai-Uwe Bux

    Comment

    • Hendrik Schober

      #3
      Re: Vector as array

      Alex Vinokur wrote:
      Let v be an vector; for instance:
      std::vector<dou blev;
      >
      Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
      >
      In other words,
      Let foo() have the following prototype:
      void (double* p, std::size_t arrarySize);
      >
      Is this calling is correct:
      foo (&v[0], v.size()) ?
      It is, except when 'v.size() == 0', in which case 'v[0]' is
      undefined (and produces runt-time errors on some popular
      platforms).
      Alex Vinokur
      Schobi

      Comment

      • Rolf Magnus

        #4
        Re: Vector as array

        Alex Vinokur wrote:
        Let v be an vector; for instance:
        std::vector<dou blev;
        >
        Is sequence &v[0], &v[1], ..., &v[v.size() - 1] a C/C++-array?
        Yes.
        In other words,
        Let foo() have the following prototype:
        void (double* p, std::size_t arrarySize);
        >
        Is this calling is correct:
        foo (&v[0], v.size()) ?
        Yes.

        Comment

        Working...