return null-terminated string

the thing is that my string is going to be constant in a class, so would an std::string be unnecessary? right now i have it as a pointer, but i thought maybe i should just use a char* with strcpy…it might be simpler

If it’s going to be constant, just delcare it as:

const char* str = “Some Value”;

You DON’T want to use strcpy to copy a value into a pointer with no memory allocated. The following would not be good.

char* str;
strcpy(str, “Some Value”);

strcpy tries to copy from the memory of “Some Value”, which is stored in the constant space, to the memory of str, which is a dangling pointer in the code above.

basically its gonna store the window class name, so i cant assign a value to it…it has to be input by the user…

i could allocate memory on the heap though, right? and then delete the memory within the deconstructor?

[This message has been edited by paneb (edited 04-16-2003).]

this works:

void set (char ** str)
{
strcpy (*str, “HELLO WORLD”);
}

void main ()
{
char * str = new char [20];

set (&str);

cout << str << endl;
delete str;
}

it is supposed to right?

posted by deiussum:
Since string::c_str() returns a const char*, it can’t be written to by these APIs, so you need to use a regular char* in these cases anyway.

you can still use std::strings in these situations if you want to. &str[0] (where str is a std::string) will get you a non-const pointer to the data.

paneb, what you have there should work fine. with that function though, it’s now pointless to pass in a pointer to a pointer, since you’re not modifying the pointer itself when you do a strcpy(), you’re only modifying the data that the pointer is pointing to. so passing in a regular char* would work fine. ok, this thread is way off topic, so i’m gonna stop posting now. this board is an opengl board, and really shouldn’t be used for standard programming questions.

Originally posted by SThomas:
pass the string by reference, ie use char*&, which will only work in c++ (c doesn’t have references)

did i miss something here? references do work with C, i thought…

jebus

thats why we need an OpenGL board cannot go without a small general programming board…anyways, thanks i got it now…

Originally posted by SThomas:
you can still use std::strings in these situations if you want to. &str[0] (where str is a std::string) will get you a non-const pointer to the data.

Does that throw an exception if not enough space has been allocated in the string yet, though? I would think if you have something like the following would throw an exception:

string s = “”;

strcpy(&s[0], “Hello”);

I know that when you do concatentation, the string will re-size itself as necessary, but I wouldn’t think it would do so in the above case.

Originally posted by jebus:
[b] did i miss something here? references do work with C, i thought…

jebus[/b]

Nope. References were added in C++. In C you use a pointer to a pointer.

Anyway, as SThomas said, this is really more of a standard programming question than an OpenGL question. I always seem to get drawn into these for some reason, though.

string::operator[] does not thrown an exception (even if you access an element outside the string, use string::at if you want exceptions), nor will that code thrown any exception related to std::string if strcpy mess up. Of course, an exception is thrown if strcpy tries to access memory outside allocated space, but that would be a regular access violation and has nothing to do with std::string.

Even if you can do that, I suggest you don’t. If you use std::string, you should also stick to the C++ standard library only. The C library is not designed to work with std::string’s, but the C++ library is. As Deiussum said above, there can be some problems with C API’s, but I would consider using char arrays for temporary storage in those cases.

edit: stupid smileys…

[This message has been edited by Bob (edited 04-16-2003).]

Thanks Bob. That’s what I suspected. When I said exception, I actually meant an access violation. strcpy was really just an example I used. I could easily have used one of the Win32 API functions, but strcpy was the first thing to come to mind so I used it.

With VB, if you needed to allocate space for a string before passing it to a Win32 API function, you could set the VB string to Space(sizeNeeded), or something like that. The same thing would apply to std::string, but I agree with you that using a char* for temporary storage just seems better in those cases.