This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320C6745: STL vector for C++11

Part Number: TMS320C6745

Tool/software: TI C/C++ Compiler

Dear Sirs:

  I know that the TI compiler only support c++03 and c++98

http://processors.wiki.ti.com/index.php/C%2B%2B_Support_in_TI_Compilers#Status_as_of_March_2014

However, our provider uses C++ STL vector, and they heavily uses the std:vector::data() which is only available in C++11 for their algorithm.

And our provider only use the simple type like char, int, long etc. in the vector.

I read the vector implementation in CGT6X v8.0.3, and make sure there is no data implementation inside it.

However after reading the implementation, just thought, if it is possible to use the iterator memory returned by std::vector::begin() if only simple type is used.

If we can not use std::vector::begin() , then is there any simple solution that can make us use of the function std:vector::data()?

Regards,

/ckhsu

  • The type returned by the vector member function begin() is a 'random access iterator', which does not immediately allow access to the data contained within the vector.


    However, C++03 does provide the 'front' function, which returns a reference to the first element of the vector.

    Because vectors guarantee contiguous storage for its elements, the address of vector::front() can be used as a pointer into the data stored within. For example:

    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main() {
        // Initialize a simple vector
        std::vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
    
        // Extract a reference to the first element in the vector
        int &ra = v.front();
        // Get the address of the first element in the vector
        int *pa = &ra;
    
        for (int i = 0; i < 4; i++)
        {
            // Print the value and address of each element
            int val = *pa;
            size_t addr = (size_t)(pa++);
            cout << addr << " " << val << endl;
        }
    }

    Do beware, if the element type of the vector ever becomes a more complicated type, such as a class, there can be issues with the above example, since that class could overload the unary address-of (&) operator so that it does not return a pointer to itself.


    As a final aside, you could create a helper in your own code to provide a shortcut for this operation:

    namespace ti_helpers {
        template <typename E>
        E *data(std::vector<E> &vec) {
            return &(vec.front());
        }
    }

  • Dear Sir:
    Thank you very much, I'll try this solution.

    Sincerely,
    /ckhsu