string::c_str() and char*

A

Alex Vinokur

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?
 
M

mlimber

Alex said:
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
 
A

Alex Vinokur

mlimber said:
Alex said:
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.
 
V

Victor Bazarov

Alex said:
mlimber said:
Alex said:
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
 
G

Guest

Alex Vinokur said:
mlimber said:
Alex said:
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
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top