c++ - Can I convert an array in such a way to Pointer and return a pointer to a constant? -
can convert array in such way pointer , return pointer constant ? right in terms of memory allocation ?
const int* convert_vector_to_pointer(std::vector<std::pair<int, int> >& v) { std::vector<std::pair<int, int> >::iterator = v.begin(); int* = new int[2*v.size()]; int = 0; for(; != v.end(); ++it) { if(i < 2*v.size()) { a[i] = (*it).first; a[i + 1] = (*it).first; += 2; } } const int* b = const_cast<const int*>(a); return b; }
the problem function allocate array within function , return const pointer it. either 1 fine, means calling code going tasked deleteing const object, , that's undefined behavior. no example not right.
Comments
Post a Comment