By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

string::c_str() and char*

P: 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


Oct 10 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Alex Vinokur wrote:
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?


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

Cheers! --M

Oct 10 '05 #2

P: n/a

"mlimber" <ml*****@gmail.com> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com...

Alex Vinokur wrote:
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?


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

Cheers! --M


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

Oct 10 '05 #3

P: n/a
Alex Vinokur wrote:
"mlimber" <ml*****@gmail.com> wrote in message news:11*********************@g14g2000cwa.googlegro ups.com...
Alex Vinokur wrote:
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?


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

Cheers! --M

I think it is unsafe.


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
Oct 10 '05 #4

P: n/a
"Alex Vinokur" <al****@go.to> wrote in message
news:di**********@reader.greatnowhere.com...

"mlimber" <ml*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

Alex Vinokur wrote:
> 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?


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

Cheers! --M


I think it is unsafe.


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

Oct 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.