简体   繁体   English

将不同的值存储在char *向量中,数组中的所有位置都具有相同的值

[英]Storing differnt values in char* vector, all positions in array have same value

Let me explain my problem 让我解释一下我的问题

First of all, imagine I randomly generated the number "5". 首先,假设我随机生成数字“ 5”。

I want to convert this integer to a char* so I can display it using my Draw_String() method. 我想将此整数转换为char *,以便可以使用Draw_String()方法显示它。

I want to have "5" as the central position which would be [1] in a char* array[2]. 我想将“ 5”作为中心位置,该位置应为char * array [2]中的[1]。

In position [0] I want the lower adjacent number "4" 在[0]位置,我希望下一个相邻数字“ 4”

In position [2] I want the higher adjacent number "6" 在位置[2]中,我希望较高的相邻数字“ 6”

so 所以

[0] = 4 [1] = 5 [2] = 6 [0] = 4 [1] = 5 [2] = 6

Then I want to display the contents of the vector positions, like this. 然后,我要显示矢量位置的内容,像这样。

4
5
6

However, my output is more like this: 但是,我的输出更像这样:

5
5
5

No matter what number I generate, and no matter which operations I perform, I will only get the number "5" in all of my vectors positions. 无论生成什么数字,无论执行什么操作,我在所有矢量位置上都只会得到数字“ 5”。

Code

int startpoint = rand() % 7 + 1;
int assigner = -1;
for (int i = 0; i < 3; i++)
{
    startpoint += assigner;
    convert = std::to_string(startpoint);
    char *converted = &convert[0u];
    blocks.push_back(converted);
    assigner++;
}

//spin around twice for visual effect.

for (int counter = 0; counter < 2; counter++)
{
    Draw_String(drawY, drawX - 1, blocks.at(0));
    Draw_String(drawY, drawX, blocks.at(1));
    Draw_String(drawY, drawX + 1, blocks.at(2));
}

if anyone could help out I'd appreciate it. 如果有人可以帮忙,我将不胜感激。

Don't ask me to get rid of char* . 不要要求我摆脱char*

Your code has undefined behavior: it harvests a pointer to the internal string buffer from inside the conver string 您的代码具有未定义的行为:它从conver字符串内部获取指向内部字符串缓冲区的指针

char *converted = &convert[0u];

but the buffer can become invalid the next time convert string is re-assigned. 但是缓冲区可能在下一次重新分配convert字符串时变为无效。 The behavior that you see suggests that the buffer gets reused, rather than re-allocated, so the same pointer gets stored in the blocks vector. 您看到的行为表明缓冲区已被重用,而不是重新分配,因此同一指针被存储在blocks向量中。

The best solution would be to store std::string directly, because it would manage both copying and de-allocation for you: 最好的解决方案是直接存储std::string ,因为它将为您管理复制和取消分配:

std::vector<std::string> blocks;
...
blocks.push_back(std::to_string(startpoint));

If you insist on using pointers, make a copy of the string before adding it to the container: 如果您坚持使用指针,请在将字符串添加到容器之前对其进行复制:

char *copy = new char[strlen(converted)+1];
strcpy(copy, converted);
blocks.push_back(copy);

Now you are required to call delete[] on each element of blocks when you are done in order to avoid memory leaks. 现在,您需要在完成后在blocks每个元素上调用delete[] ,以避免内存泄漏。

If you needto use char* and not std::string than you need to allocate it and copy content of the string intead of copy the pointer. 如果您需要使用char *而不是std :: string,则需要分配它并复制复制指针的字符串内容。

instead of: 代替:

char *converted = &convert[0u];

use: 采用:

char *converted = malloc(convert.length() + 1); // number + '\0'
// can also use char *converted = new char[convert.length() + 1];
strcpy(converted, convert.c_str())

Now, no matter what happens to convert, you have control on your converted data. 现在,无论发生什么转换,您都可以控制转换后的数据。 (take care to also delete it when you don't need it) (请注意在不需要时也将其删除)

Otherwise when convert changes, converted can change or point to invalid data. 否则,当转换更改时,转换后的内容可能会更改或指向无效数据。 This is because in the original implementation converted is pointing to data that is managed by convert object. 这是因为在原始实现中,转换后的对象指向由转换对象管理的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM