Take the 2-minute tour ×

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];

    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)

share|improve this question
Are you asking the same thing as Are std::vector elements guaranteed to be contiguous?? –  Troubadour Jun 17 '11 at 17:38
actually, I shouldn't even have mentioned the vector, my question is more if it would work, even if vectorBuffer was declared as char *VectorBuffer = new char[] –  Bwmat Jun 17 '11 at 17:42
@Troubadour: No, he isn't. He is asking whether the memory used by a std::vector is suitably aligned for any data to put into. –  sbi Jun 17 '11 at 17:47
add comment

3 Answers

up vote 1 down vote accepted

Assuming that you're using vector with an allocator that uses operator new under the hood, then the C++ standard guarantees that an array of char allocated with new will 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.

share|improve this answer
You mean, an array with automatic storage duration. (And/or static storage duration?) It may not be on the actual stack, and it may not be "local" (for some value of "local"). Be precise, please! –  Lightness Races in Orbit Jun 17 '11 at 17:55
Not that it matters now the question has changed, but does the standard guarantee that vector locates the elements of the vector starting from the beginning of the array that it allocates? You'd hope so, since by design a vector is supposed to replace a dynamically-allocated array, so it would be a shame if it was missing a useful property of those. But I don't recall seeing such a guarantee. For other containers it doesn't necessarily locate the element at the start of an allocated block, due to the whole rebind thing going on. –  Steve Jessop Jun 17 '11 at 18:33
add comment

A vector is just a wrapper around a contiguous block of dynamically allocated memory, in other words, an array. So, if this program would work with an array allocated with malloc or new, it should continue to work with a vector.

(Unless it only worked "accidentally" with an array, of course, but the vector is unlikely to introduce extra problems.)

share|improve this answer
add comment

The C++ standard in 23.2.4 guarantees that each element of a vector<> is stored contiguously:

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

It's important to notice what it says about vector < bool > , vector < bool > is a specialized vector<> that happens to try to "optimize space allocation", in some implementations it does so by storing each boolean value in a bit which pretty much renders pointer arithmetic useless.

share|improve this answer
add comment

Your Answer


