如何更改函数中的值或静态 char*? C++
我试图更改我在启动时定义的“static char *”的值,我从函数内部执行此操作,当此函数返回 var 时,我尝试重新设置该值,但不保留它。
示例:
static char *X = "test_1";
void testFunc()
{
char buf[256];
// fill buf with stuff...
X = buf;
}
如何在不使用 static for buf 的情况下实现此目的?我应该使用其他数据类型吗?如果有,是哪一个?
I am trying to change the value of a "static char *" I define at startup, I do it from inside a function, and when this function returns the var I am trying to re-set the value doesn't retain it.
Example:
static char *X = "test_1";
void testFunc()
{
char buf[256];
// fill buf with stuff...
X = buf;
}
How can I achieve this without using static for buf? Should I use another datatype? if so, which one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
X = buf
行将指针X
设置为指向数组buf
的第一个元素。当函数返回时,buf 超出范围并且不能再使用,因此指针此时毫无用处。除非您有特定原因在程序中使用原始 C 字符串,否则使用
std::string
容器,那么您可以简单地返回std::string
而不必担心自己动态分配和管理字符串的内存。The line
X = buf
sets the pointerX
to point to the first element of the arraybuf
. When the function returns,buf
goes out of scope and can no longer be used, so the pointer is useless at that point.Unless you have specific reason to use raw C strings in your program, use the
std::string
container, then you can simply return astd::string
and not worry about dynamically allocating and managing the memory for the string yourself.正如 James 所说,使用 std::string...但要注意翻译单元之间的全局构造和销毁顺序是未定义的。
因此,如果您仍想使用
char*
,请使用strcpy
(请参阅man strcpy
)并确保buf
得到 NUL 终止。 strcpy 会将buf
复制到目标X
中。我应该补充一点,使用 std::string 有更多理由。使用
strcpy
时,需要确保目标缓冲区(X
)有足够的内存来接收源缓冲区。在本例中,256 比strlen("test_1")
大得多,因此您会遇到问题。有多种方法可以解决重新分配 X 的问题(例如X = new char[number_of_characters_needed]
)。或者将X
初始化为 256 的 char 数组,而不是char*
。IIRC,
strcpy
到静态定义的字符串文字(如 char *X = "test_1")是未定义的行为...这个故事的寓意是...它是 C++!使用std::string
! :)(你说你是c++新手,所以你可能没有听说过“未定义行为”意味着计算机可以打你的脸......这通常意味着你的程序会崩溃)
As James said, use
std::string
... except be aware that global construction and destruction order is undefined between translation units.So, if you still want to use
char*
, usestrcpy
(seeman strcpy
) and make surebuf
gets NUL-terminated. strcpy will copy thebuf
into the destinationX
.I should add that there are more reasons to use
std::string
. When usingstrcpy
, you need to make sure that the destination buffer (X
) has enough memory to receive the source buffer. In this case, 256 is much larger thanstrlen("test_1")
, so you'll have problems. There are ways around this reallocate X (like thisX = new char[number_of_characters_needed]
). Or initializeX
to a char array of 256 instead of achar*
.IIRC,
strcpy
to a static defined string literal (like char *X = "test_1") is undefined behavior... the moral of the story is... It's C++! Usestd::string
! :)(You said you were new to c++, so you may not have heard "undefined behavior" means the computer can punch you in the face... it usually means your program will crash)
在上面的示例中,如果运行并调试代码,您将看到 X = buf; 行上的 X 值将发生更改。
一旦 buf 不是静态的并且在特定范围内(在 { } 之间)定义,它将被分配为临时堆栈变量。每当指令指针离开该范围时,buf 就会变得未定义,但 X 会保留旧的 buf 地址(指向堆栈地址)和无效数据。
请注意,X 只是一个指针,因此您可以随时更改它。并且考虑到它是静态的,它的定义将保持有效直到程序结束。
因此,如果您想更改 X 值,只需将其添加到您想要的任何值即可。请注意,在访问 X 数据 (*X) 之前,不要使其指向的数据无效。
On the example above if you run and debug the code, you will see that the value of X will be changed on the line X = buf;
Once buf is not static and is defined inside a specific scope (between { }), it will be allocated as a temporary stack variable. Whenever the instruction pointer leaves that scope, buf becomes undefined, but X keeps the old buf address (pointing to a stack address) with a not valid data.
Notice that X is just a pointer, so you can change it anytime you want. And considering that it is static, its definition will be kept valid up to the end of the program.
So, if you want to change X value just atrib it anything you want. Just be careful to not invalidate the data it will point before you access X data (*X).