trying to use vector<char*> as char**

M

Marco Costa

Hello all,

I wrote a simple ODBC wrapper class that used code like this ( not real
code, added types for clarification ):

char** type bufs = new char*[numberOfColumns]

for( int i = 0 ; i < numberOfColumns ; i++ ) {
size = getSizeOfColumn();
bufs = new char[sizeOfColumn+1];
BindBufferToColumn( bufs );
}

it worked as above, but when I tried to use a vector<char*> to simplify
the code like:

vector<char*> bufs;

for( int i = 0 ; i < numberOfColumns ; i++ ) {
size = getSizeOfColumn();
bufs.push_back(new char[sizeOfColumn+1]);
BindBufferToColumn( bufs );
}

Question is, does vector mess around with pointer addresses? should I
be using vector<char *const>?

I'd appreciate any insight.

Regards,
Marco
 
R

Rolf Magnus

Marco said:
Hello all,

I wrote a simple ODBC wrapper class that used code like this ( not real
code, added types for clarification ):

char** type bufs = new char*[numberOfColumns]

for( int i = 0 ; i < numberOfColumns ; i++ ) {
size = getSizeOfColumn();
bufs = new char[sizeOfColumn+1];
BindBufferToColumn( bufs );
}

it worked as above, but when I tried to use a vector<char*> to simplify
the code like:

vector<char*> bufs;

for( int i = 0 ; i < numberOfColumns ; i++ ) {
size = getSizeOfColumn();
bufs.push_back(new char[sizeOfColumn+1]);


Did you mean:

bufs.push_back(new char[size+1]);

BindBufferToColumn( bufs );
}

Question is, does vector mess around with pointer addresses?


No, why should it?
should I be using vector<char *const>?

I think you should make it something like:

vector<vector<char> > bufs(numberOfColumns);
for (int i = 0; i < numberOfColumns; ++i)
{
bufs.resize(getSizeOfColumn()+1);
}

Btw: Why does getSizeOfColumn return one less than the size of the column?
 
M

Marco Costa

Btw: Why does getSizeOfColumn return one less than the size of the column?

Because its the size of the column in the database. When the other
function actually writes the data into the buffer, it includes the '\0'
terminator character so I gotta account for that.

I'm gonna try that out now, thanks for the help. :)
 
M

Marco Costa

Rolf Magnus escreveu:
I think you should make it something like:

vector<vector<char> > bufs(numberOfColumns);
for (int i = 0; i < numberOfColumns; ++i)
{
bufs.resize(getSizeOfColumn()+1);
}


that seems to work fine.

another thing: is there an easy way to convert std::vector<char> to
std::string?
 
R

Rolf Magnus

Marco said:
Rolf Magnus escreveu:
I think you should make it something like:

vector<vector<char> > bufs(numberOfColumns);
for (int i = 0; i < numberOfColumns; ++i)
{
bufs.resize(getSizeOfColumn()+1);
}


that seems to work fine.

another thing: is there an easy way to convert std::vector<char> to
std::string?


Something like:

std::vector<char> myvec;
//...
std::string s(myvec.begin(), myvec.end());

But why not directly use std::string instead of std::vector<char> then?
 
M

Marco Costa

the thing is, the ODBC function requires me to give it a pointer to a
buffer. so when I fetch the data it writes to the buffer it points to.

like:

char* buf;
buf = new char[size];
SQLBindCol(someHandle,someIndex,someType, buf, size+1, someOtherBuffer
);

when I use the above, it works ok. when I tried:

vector<char> buf;
buf.resize(size);
SQLBindCol(someHandle,someIndex,someType, &buf, size+1, someOtherBuffer
);

It crashed on me. Now to think of it, would &buf[0] work? 8-|
huummmm....
 
R

red floyd

Rolf said:
Marco said:
Rolf Magnus escreveu:
I think you should make it something like:

vector<vector<char> > bufs(numberOfColumns);
for (int i = 0; i < numberOfColumns; ++i)
{
bufs.resize(getSizeOfColumn()+1);
}

that seems to work fine.

another thing: is there an easy way to convert std::vector<char> to
std::string?


Something like:

std::vector<char> myvec;
//...
std::string s(myvec.begin(), myvec.end());

But why not directly use std::string instead of std::vector<char> then?


There are cases where you need to pass a pointer to a non-const char
array to an API.
 
R

red floyd

Marco Costa wrote:
orks ok. when I tried:
vector<char> buf;
buf.resize(size);
SQLBindCol(someHandle,someIndex,someType, &buf, size+1, someOtherBuffer
);

It crashed on me. Now to think of it, would &buf[0] work? 8-|
huummmm....

&buf[0] is generally recognized as the way to convert a vector to a pointer.

I don't belive there's a direct conversion between vector<T> (or
vector<T>*) and T*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top