如何更改函数中的值或静态 char*? C++

发布于 2024-09-04 03:43:43 字数 290 浏览 1 评论 0原文

我试图更改我在启动时定义的“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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云归处 2024-09-11 03:43:43

X = buf 行将指针 X 设置为指向数组 buf 的第一个元素。当函数返回时,buf 超出范围并且不能再使用,因此指针此时毫无用处。

除非您有特定原因在程序中使用原始 C 字符串,否则使用 std::string 容器,那么您可以简单地返回 std::string 而不必担心自己动态分配和管理字符串的内存。

The line X = buf sets the pointer X to point to the first element of the array buf. 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 a std::string and not worry about dynamically allocating and managing the memory for the string yourself.

向日葵 2024-09-11 03:43:43

正如 James 所说,使用 std::string...但要注意翻译单元之间的全局构造和销毁顺序是未定义的。

因此,如果您仍想使用 char*,请使用 strcpy(请参阅 man strcpy)并确保 buf得到 NUL 终止。 strcpy 会将 buf 复制到目标 X 中。

char buf[256];
// ...
strcpy(X, buf);

我应该补充一点,使用 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*, use strcpy (see man strcpy) and make sure buf gets NUL-terminated. strcpy will copy the buf into the destination X.

char buf[256];
// ...
strcpy(X, buf);

I should add that there are more reasons to use std::string. When using strcpy, 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 than strlen("test_1"), so you'll have problems. There are ways around this reallocate X (like this X = new char[number_of_characters_needed]). Or initialize X to a char array of 256 instead of a char*.

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++! Use std::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)

她比我温柔 2024-09-11 03:43:43
static char *X = "test_1";

void testFunc()
{
    char buf[256];
    // fill buf with stuff...
    X = buf;
}

在上面的示例中,如果运行并调试代码,您将看到 X = buf; 行上的 X 值将发生更改。

一旦 buf 不是静态的并且在特定范围内(在 { } 之间)定义,它将被分配为临时堆栈变量。每当指令指针离开该范围时,buf 就会变得未定义,但 X 会保留旧的 buf 地址(指向堆栈地址)和无效数据。

请注意,X 只是一个指针,因此您可以随时更改它。并且考虑到它是静态的,它的定义将保持有效直到程序结束。

因此,如果您想更改 X 值,只需将其添加到您想要的任何值即可。请注意,在访问 X 数据 (*X) 之前,不要使其指向的数据无效。

static char *X = "test_1";

void testFunc()
{
    char buf[256];
    // fill buf with stuff...
    X = buf;
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文