I want to pass in an array of parameters to SQLBindParameters, and have this array held in a char array(since I don’t know the type beforehand) (I want all the elements in the ‘array’ to be the same).
I’ll have a pointer to a sample parameter type, and the size of the parameter.
void *buffer = getBuffer();
int bufferLength = getBufferLength();
const int numElements = 200; //for example
char *array = new char[bufferLength * numElements];
for(int i=0; i < numElements; ++i)
{
memcpy(array + (i * bufferLength), buffer, bufferLength)
}
// now use array in SQLBindParameter call
will this work as expected, without any alignment issues? (i.e., the same as if I had just declared an array of the right type to start with)
Assuming that you’re using
vectorwith an allocator that usesoperator newunder the hood, then the C++ standard guarantees that an array ofcharallocated withnewwill be aligned suitably for use with any data type.EDIT: Yes,
new char[]is guaranteed to be aligned for use with any type.EDIT2: Do note that a local (stack) array
char foo[]has no such alignment guarantees.