Connecting Tech Pros Worldwide Help | Site Map

string::c_str() and char*

  #1  
Old October 10th, 2005, 06:45 PM
Alex Vinokur
Guest
 
Posts: n/a
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, 06:45 PM
mlimber
Guest
 
Posts: n/a

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, 06:55 PM
Alex Vinokur
Guest
 
Posts: n/a

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, 07:25 PM
Victor Bazarov
Guest
 
Posts: n/a

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, 08:05 PM
Ali Çehreli
Guest
 
Posts: n/a

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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
core dump due to string::c_str () puzzlecracker answers 8 September 25th, 2008 03:45 PM
On std::string::c_str() and const_cast<char*> Tom Smith answers 5 October 4th, 2006 11:15 PM
What's the difference between string::c_str() and string::data()? Metro12 answers 18 August 24th, 2005 09:35 AM
How is string::c_str() usually implemented? Derek answers 15 July 22nd, 2005 12:55 PM