如何将 std::string 传递给 glShaderSource?
我有以下代码:
glShaderSource(shader, 1, (const char **)data.c_str(), NULL);
但这使我的程序崩溃。如何将 std::string
转换为 const char **
? 我也尝试过 (const char **)&
但它说“需要左值”,我不明白。当我使用此代码时,它工作正常:
const char *data = "some code";
glShaderSource(shader, 1, &data, NULL);
但我无法使其直接从 std::string
工作。我可以为其分配一个新的 char 数组,但这不是一个好的代码。
我也尝试过使用 const GLchar 但显然它没有什么区别。
I have the following code:
glShaderSource(shader, 1, (const char **)data.c_str(), NULL);
But it makes my program crash. How do I convert std::string
into const char **
?
I also tried (const char **)&
but it said "requires l-value" which I don't understand. It works fine when I use this code:
const char *data = "some code";
glShaderSource(shader, 1, &data, NULL);
But I can't make it work directly from a std::string
. I could allocate a new char
array for it but that is not nice code.
I also tried with const GLchar
but obviously it makes no difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
data.c_str()
返回一个const char*
,所以这样做:data.c_str()
returns aconst char*
, so do this:std::string::c_str() 的返回值是指向 std::string 数据结构内保存的静态字符串数组的指针值(即地址)对象。由于返回值只是一个临时右值(即,它只是存储在 CPU 寄存器中的数字),因此它不是左值,因此它没有可以存储的内存地址。实际上获取地址并转换为指针到指针。首先必须将返回指针值保存在内存地址中。内存位置是左值,并且可以对其应用地址运算符。这就是为什么你的第二个方法(或 Dark Falcon 的方法)有效,尽管请记住返回的指针值是临时的,这意味着如果你对
std::string
对象执行任何操作,它可能会使指针无效,因为std::string
对象在内部管理其数据结构的内存。因此,仅仅因为您已将返回指针值保存在内存位置中,并不意味着该指针不会在以后某个时间以及您可能无法确定性选择的时间点失效。The return value of
std::string::c_str()
is a pointer value (i.e., an address) to a static string array held inside the data-structures of thestd::string
object. Since the return value is just a temporary r-value (i.e., it's just a number stored in a CPU register), it is not an l-value and therefore it does not have a memory address you can actually take the address of and cast to a pointer-to-pointer. You first must save the return pointer value in a memory address. Memory-locations are l-values, and can have the address-of operator applied to them. So that is why your second method (or Dark Falcon's method) works, although keep in mind that the pointer value returned is a temporary, meaning that if you do any operations on thestd::string
object, it could invalidate the pointer since thestd::string
object internally manages the memory of its data-structures. So just because you've saved the return pointer value in a memory location doesn't mean that the pointer won't be invalidated at some later time, and at a point that you may not be capable of deterministically choosing.您可以通过使用辅助类来获得看起来合理的调用。定义这个类:
然后,当你需要调用
glShaderSource
时,这样做:You can get a reasonable-looking call by using a helper class. Define this class:
Then, when you need to call
glShaderSource
, do it this way:glShaderSource
签名是,根据 glShaderSource doc:其中 < code>string “指定指向字符串的指针数组,其中包含要加载到着色器中的源代码”。您试图传递的是一个指向 NULL 终止字符串的指针(即指向
const char*
的指针)。不幸的是,我不熟悉
glShaderSource
,但我可以猜测它不是指向“某些代码”的指针,而是类似这样的指针:来自 opengl-redbook,您可以阅读一个示例(我故意缩短了它):
glShaderSource
signature is, according to glShaderSource doc:where
string
"Specifies an array of pointers to strings containing the source code to be loaded into the shader". What you're trying to pass is a pointer to a NULL terminated string (that is, a pointer to aconst char*
).Unfortunately, I am not familiar with
glShaderSource
, but I can guess it's not expected a pointer to "some code" but something like this instead:From opengl-redbook, you can read an example (I've shortened it in purpose):
我只想指出,只有当您不执行任何需要重新分配 std::string 内部缓冲区的操作时,
c_str()
返回的指针才有效。这会使你得到的指针无效。但由于您确实需要
**
我会这样做:只要您不离开 mychararr 的范围,这应该可以很好地工作。
I only want to point out that the pointer returned by
c_str()
is only valid as long as you don't do anything that requires reallocation of the internal buffer of std::string. That invalidates the pointer you got.But since you really require a
**
i would do this:That should work nicely as long as you don't leave the scope of mychararr.
尝试使用 .c_str() 它会给你一个 char * ,你可以使用它,因为它对你有用 b4
Try using the .c_str() it give you a char * that you can use as it worked for you b4
着色器.cpp
着色器.hpp
Shader.cpp
Shader.hpp