Connecting Tech Pros Worldwide Help | Site Map

string::c_str() and char*

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 10th, 2005, 05:45 PM
Alex Vinokur
Guest
 
Posts: n/a
Default string::c_str() and char*

I have got two functions:

void foo1(char* str)
{
// Stuff
}

void foo2(int size)
{
char* str;
str = new char[size];
// Stuff-1
foo1(str)
// Stuff-2
}

Function foo1() can't be changed.
Function foo2() can be changed.

I would like to use string::c_str() instead of char* in new version of foo2();
Something like:

void new_foo2(int size)
{
string str(size, '0');

// Stuff-1
foo1(str.c_str())
// Stuff-2
}

It seems to be problematic.

Is there any appropriate solution?


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn







  #2  
Old October 10th, 2005, 05:45 PM
mlimber
Guest
 
Posts: n/a
Default Re: string::c_str() and char*


Alex Vinokur wrote:[color=blue]
> I have got two functions:
>
> void foo1(char* str)
> {
> // Stuff
> }
>
> void foo2(int size)
> {
> char* str;
> str = new char[size];
> // Stuff-1
> foo1(str)
> // Stuff-2
> }
>
> Function foo1() can't be changed.
> Function foo2() can be changed.
>
> I would like to use string::c_str() instead of char* in new version of foo2();
> Something like:
>
> void new_foo2(int size)
> {
> string str(size, '0');
>
> // Stuff-1
> foo1(str.c_str())
> // Stuff-2
> }
>
> It seems to be problematic.
>
> Is there any appropriate solution?[/color]

In what way is it problematic? Compiler errors? Run-time errors? Speed
change? Please elaborate.

Cheers! --M

  #3  
Old October 10th, 2005, 05:55 PM
Alex Vinokur
Guest
 
Posts: n/a
Default Re: string::c_str() and char*


"mlimber" <mlimber@gmail.com> wrote in message news:1128965904.835293.14400@g14g2000cwa.googlegro ups.com...[color=blue]
>
> Alex Vinokur wrote:[color=green]
> > I have got two functions:
> >
> > void foo1(char* str)
> > {
> > // Stuff
> > }
> >
> > void foo2(int size)
> > {
> > char* str;
> > str = new char[size];
> > // Stuff-1
> > foo1(str)
> > // Stuff-2
> > }
> >
> > Function foo1() can't be changed.
> > Function foo2() can be changed.
> >
> > I would like to use string::c_str() instead of char* in new version of foo2();
> > Something like:
> >
> > void new_foo2(int size)
> > {
> > string str(size, '0');
> >
> > // Stuff-1
> > foo1(str.c_str())
> > // Stuff-2
> > }
> >
> > It seems to be problematic.
> >
> > Is there any appropriate solution?[/color]
>
> In what way is it problematic? Compiler errors? Run-time errors? Speed
> change? Please elaborate.
>
> Cheers! --M
>[/color]

I think it is unsafe.


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn





  #4  
Old October 10th, 2005, 06:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: string::c_str() and char*

Alex Vinokur wrote:[color=blue]
> "mlimber" <mlimber@gmail.com> wrote in message news:1128965904.835293.14400@g14g2000cwa.googlegro ups.com...
>[color=green]
>>Alex Vinokur wrote:
>>[color=darkred]
>>>I have got two functions:
>>>
>>>void foo1(char* str)
>>>{
>>> // Stuff
>>>}
>>>
>>>void foo2(int size)
>>>{
>>>char* str;
>>> str = new char[size];
>>> // Stuff-1
>>> foo1(str)
>>> // Stuff-2
>>>}
>>>
>>>Function foo1() can't be changed.
>>>Function foo2() can be changed.
>>>
>>>I would like to use string::c_str() instead of char* in new version of foo2();
>>>Something like:
>>>
>>>void new_foo2(int size)
>>>{
>>>string str(size, '0');
>>>
>>> // Stuff-1
>>> foo1(str.c_str())
>>> // Stuff-2
>>>}
>>>
>>>It seems to be problematic.
>>>
>>>Is there any appropriate solution?[/color]
>>
>>In what way is it problematic? Compiler errors? Run-time errors? Speed
>>change? Please elaborate.
>>
>>Cheers! --M
>>[/color]
>
>
> I think it is unsafe.[/color]

What's "unsafe" about it? Does it attempt to change the contents of the
memory pointed to by the 'str'? If so, you can't use '.c_str()'. Make
sure there is a null character after the last one (by inserting it into
your string _explicitly_) and pass '.data()'.

Of course, a safer method would be to write 'new_foo2' like this:

string str(size, 0);
// Stuff-1
char* temp_buffer = new char[strlen(str.c_str()) + 1];
strcpy(temp_buffer, str.c_str());
foo1(temp_buffer);
str = temp_buffer;
delete[] temp_buffer;
// Stuff-2

V
  #5  
Old October 10th, 2005, 07:05 PM
Ali Çehreli
Guest
 
Posts: n/a
Default Re: string::c_str() and char*

"Alex Vinokur" <alexvn@go.to> wrote in message
news:die953$tih$1@reader.greatnowhere.com...[color=blue]
>
> "mlimber" <mlimber@gmail.com> wrote in message
> news:1128965904.835293.14400@g14g2000cwa.googlegro ups.com...[color=green]
>>
>> Alex Vinokur wrote:[color=darkred]
>> > I have got two functions:
>> >
>> > void foo1(char* str)
>> > {
>> > // Stuff
>> > }
>> >
>> > void foo2(int size)
>> > {
>> > char* str;
>> > str = new char[size];
>> > // Stuff-1
>> > foo1(str)
>> > // Stuff-2
>> > }
>> >
>> > Function foo1() can't be changed.
>> > Function foo2() can be changed.
>> >
>> > I would like to use string::c_str() instead of char* in new version of
>> > foo2();
>> > Something like:
>> >
>> > void new_foo2(int size)
>> > {
>> > string str(size, '0');
>> >
>> > // Stuff-1
>> > foo1(str.c_str())
>> > // Stuff-2
>> > }
>> >
>> > It seems to be problematic.
>> >
>> > Is there any appropriate solution?[/color]
>>
>> In what way is it problematic? Compiler errors? Run-time errors? Speed
>> change? Please elaborate.
>>
>> Cheers! --M
>>[/color]
>
> I think it is unsafe.[/color]

As Victor explained, if foo1 is going to change str, you should not pass
what c_str() returns.

But if foo1 documents that it doesn't change the contents of str, then it
should have been declared as

void foo1(char const *);

You can cover up foo1's problem by using const_cast:

void my_foo1(char const * str)
{
// We know that foo1 doesn't change the string
foo1(const_cast<char *>(str));
}

Now, instead of calling foo1, call my_foo1 everywhere:

string str("something");
my_foo1(str.c_str());

Ali

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.