Thread: STL Vector to Array

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    STL Vector to Array

    Hi

    I need to convert a STL vector to an array so it can be passed to an API functions. Since I've heard that the vector class keeps the data in continues memory, I convert it through the following code below. But I want to be sure this conversion is fully legal, and not only achievable on my current platform/implementation (which is VC++ btw).

    Code:
    vector<int> vec(10);
    int *array = &vec[0]; // is this ok?

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Not sure of your implementation, but you could try something as such:

    Code:
    vector<int> vec(10);
    
    // do some stuff with it.
    
    int array[vec.size()];
    
    for(int i = 0; i < vec.size(); i++)
        array[i] = vec[i];

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    nopcoder, this is indeed legal. Just be aware that the pointer may become invalid the moment you change the size of the vector, or the vector goes out of scope.

    guyonasm, your code is not legal. Stack-based, dynamically sized arrays are disallowed in C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by CornedBee
    nopcoder, this is indeed legal. Just be aware that the pointer may become invalid the moment you change the size of the vector, or the vector goes out of scope.

    guyonasm, your code is not legal. Stack-based, dynamically sized arrays are disallowed in C++.
    What do you mean not allowed? It compiles just fine and works as necessary? Can I get a reference to where this is no longer legal.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Quote Originally Posted by guyonasm
    What do you mean not allowed? It compiles just fine and works as necessary? Can I get a reference to where this is no longer legal.
    From the ISO Standard on C++ (98) Page 134 of ISO/IEC 14882:1998(E):
    In a declaration T D where D has the form:
    D1 [constant-expression]
    ...then the type of the identifier of D is an array type. T is called the array element type....If the constant-expression is present, it shall be an integral constant expression and its value shall be greater than zero.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Quote Originally Posted by jverkoey
    From the ISO Standard on C++ (98) Page 134 of ISO/IEC 14882:1998(E):
    Thanks. I've got alot of modifications to make in a few projects of mine.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It works as an extension on the gcc compiler because it is allowed in C since C99. If you are planning on using the same compiler for awhile, it isn't entirely necessary to make the modifications. It may become part of the next C++ standard as well (I'm not sure).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM