Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Wrapper around C lib, problem with c_str and const

Reply
Thread Tools

Wrapper around C lib, problem with c_str and const

 
 
Fred Paris
Guest
Posts: n/a
 
      09-10-2005
Hi

I'm writing a class to act as a wrapper around a C library.

This C library exposes functions like:

SetSomeInfo( char *pTheInfo );

In my wrapper class, the info in question is in a STL string.

std::string m_TheInfo;

So inside the wrapper class I would like to call:

SetSomeInfo( m_TheInfo.c_str() );

But, problem:

c_str returns a const char *, and SetSomeInfo accepts a char * (not
const), so it doesn't compile. Maybe SetSomeInfo is poorly written,
because it should have const in its signature, but it's not mine and
I have no control over it.

So my (rather ugly) workaround right now is:

char *p = (char *)(DWORD) m_TheInfo.c_str() ; //get rid of const

but surely there has to be a more standard way ?

Thanks

 
Reply With Quote
 
 
 
 
Shezan Baig
Guest
Posts: n/a
 
      09-10-2005
Fred Paris wrote:
> [snip]
>
> char *p = (char *)(DWORD) m_TheInfo.c_str() ; //get rid of const
>
> but surely there has to be a more standard way ?
>



Sure:

char *p = const_cast<char*>(m_TheInfo.c_str());

But make sure that the function *really* doesn't modify 'p', otherwise
you might run into undefined behaviour.

If you want to be absolutely safe and more elegant (avoiding the
const_cast and possible undefined behaviour), you can do something
like:

enum { BUF_SIZE = 1024 };

char p[BUF_SIZE];
strncpy(p, m_TheInfo.c_str(), BUF_SIZE);
p[BUF_SIZE-1] = 0;

Hope this helps,
-shez-

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      09-10-2005

"Fred Paris" <> wrote in message
news...
> Hi
>
> I'm writing a class to act as a wrapper around a C library.
>
> This C library exposes functions like:
>
> SetSomeInfo( char *pTheInfo );
>
> In my wrapper class, the info in question is in a STL string.
>
> std::string m_TheInfo;
>
> So inside the wrapper class I would like to call:
>
> SetSomeInfo( m_TheInfo.c_str() );


The following should get you through compilation...but really ask yourself
if that is really what you want (notice the low level operations and
explicit exception handling which are rather ugly):

char* buff = new char[m_TheInfo.length() + 1];
std::copy(buff, m_TheInfo.begin(), m_TheInfo.end());
buff[m_TheInfo.length()] = '\0';
try
{
SetSomeInfo(buff);
}
catch (...)
{
delete[] buff;
throw;
}
delete[] buff;


>
> But, problem:
>
> c_str returns a const char *, and SetSomeInfo accepts a char * (not
> const), so it doesn't compile. Maybe SetSomeInfo is poorly written,
> because it should have const in its signature, but it's not mine and
> I have no control over it.
>
> So my (rather ugly) workaround right now is:
>
> char *p = (char *)(DWORD) m_TheInfo.c_str() ; //get rid of const


Don't do this. You may corrupt the string object and your program may not
recover from such brutal operation. NOT COOL!! std::string::c_str() returns
a const char* for a reason, most likely to set up protection against misuse.

>
> but surely there has to be a more standard way ?
>
> Thanks
>



 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      09-10-2005
"Fred Paris" <> wrote in message
news
> Hi
>
> I'm writing a class to act as a wrapper around a C library.
>
> This C library exposes functions like:
>
> SetSomeInfo( char *pTheInfo );
>
> In my wrapper class, the info in question is in a STL string.
>
> std::string m_TheInfo;
>
> So inside the wrapper class I would like to call:
>
> SetSomeInfo( m_TheInfo.c_str() );
>
> But, problem:
>
> c_str returns a const char *, and SetSomeInfo accepts a char * (not
> const), so it doesn't compile. Maybe SetSomeInfo is poorly written,
> because it should have const in its signature, but it's not mine and
> I have no control over it.
>
> So my (rather ugly) workaround right now is:
>
> char *p = (char *)(DWORD) m_TheInfo.c_str() ; //get rid of const
>
> but surely there has to be a more standard way ?
>
> Thanks



SetSomeInfo(const_cast<char*>( m_TheInfo.c_str() ));


--
John Carson
 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      09-10-2005
benben wrote:
>
> The following should get you through compilation...but really ask yourself
> if that is really what you want (notice the low level operations and
> explicit exception handling which are rather ugly):
>



Or, you could just use a vector<char>:

vector<char> buff(m_TheInfo.length() + 1);
....

This gets rid of all the exception handling ugliness. Note however
that vector<char> will likely allocate, and if you know that the string
is never going to be more than a certain length, its probably better if
you just use a local buffer. You might want to throw an exception if
it exceeds this "maximum" length.

Many people often underestimate the cost of allocation/deallocation,
especially in multi-threaded environments where memory contention
becomes a much bigger issue than in single-threaded environments.

Hope this helps,
-shez-

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      09-10-2005

"Shezan Baig" <> wrote in message
news: ups.com...
> benben wrote:
>>
>> The following should get you through compilation...but really ask
>> yourself
>> if that is really what you want (notice the low level operations and
>> explicit exception handling which are rather ugly):
>>

>
>
> Or, you could just use a vector<char>:
>
> vector<char> buff(m_TheInfo.length() + 1);


Eureka! Sure, I just forgot that a vector guarantees continuous memory block
haha.

> ...
>
> This gets rid of all the exception handling ugliness. Note however
> that vector<char> will likely allocate, and if you know that the string
> is never going to be more than a certain length, its probably better if
> you just use a local buffer. You might want to throw an exception if
> it exceeds this "maximum" length.


That is to say we need a better allocator.

>
> Many people often underestimate the cost of allocation/deallocation,
> especially in multi-threaded environments where memory contention
> becomes a much bigger issue than in single-threaded environments.
>
> Hope this helps,
> -shez-
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
char *std::string::c_str() const. How is it possible to work? grishin C++ 1 11-22-2010 11:40 AM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57