[英]Dynamic array of struct with string members
I've created in my program a dynamic array of struct which members are also strings.我在我的程序中创建了一个动态结构数组,其成员也是字符串。 I've some doubts about safe behaviour in manipulating strings inside the dynamically allocated struct.我对在动态分配的结构内操作字符串的安全行为有一些疑问。 Am I allowed to update or concatenate strings as I do with normal string variables?我是否可以像使用普通字符串变量一样更新或连接字符串? Do I have to concern about the boundaries of the allocated memory of the struct?我是否必须关注结构的分配 memory 的边界?
To make it a bit clear, I've something like the following pseudocode :为了清楚一点,我有类似以下伪代码:
First I've the struct declaration首先我有结构声明
struct myStruct
{
...
int data1;
int data2;
std::string myString1;
std::string myString2;
...
}
Then the struct is allocated然后分配结构
mystruct testStruct*;
testStruct = new mystruct [MAX_DEPTH];
The strings are initialized字符串被初始化
for(int i=0; i<MAX_DEPTH; i++)
{
...
testStruct[i].myString1 = "";
testStruct[i].myString2 = "";
...
}
During the program cycles, the strings values are assigned and updated在程序周期中,字符串值被分配和更新
...
testStruct[N].myString1 = "Some text";
...
testStruct[N].myString1 += "Some other text";
...
Here I'm not sure to deal correctly with the allocated memory.这里我不确定是否正确处理分配的 memory。
Are these assignments correct, and safe, even inside a dynamically allocated array of struct?即使在动态分配的结构数组中,这些分配是否正确且安全? I'm risking to break the boundary of dynamic allocated memory for myStruct variable?我冒着为 myStruct 变量打破动态分配 memory 的边界的风险?
Thanks谢谢
EDIT: I've corrected the [i] wrongly placed in the psuedocode.编辑:我已经更正了 [i] 错误地放置在伪代码中。
For starters the expressions within the loop are incorrect对于初学者来说,循环中的表达式不正确
for(int i=0; i<MAX_DEPTH; i++)
{
...
testStruct.myString1[i] = "";
testStruct.myString2[i] = "";
...
}
You mean你的意思是
for(int i=0; i<MAX_DEPTH; i++)
{
...
testStruct[i].myString1 = "";
testStruct[i].myString2 = "";
...
}
But in any case the loop is redundant because the objects of the type std::string were already default initialized when the memory for the array of structures were allocated.但在任何情况下,循环都是多余的,因为当分配结构数组的 memory 时,std::string 类型的对象已经默认初始化。
As for these statements至于这些说法
testStruct.myString1[N] = "Some text";
...
testStruct.myString1[N] += "Some other text";
then again you need to apply the subscript operator to the pointer testStruct
.然后您需要再次将下标运算符应用于指针testStruct
。 For example例如
testStruct[N].myString1 = "Some text";
...
testStruct[N].myString1 += "Some other text";
I'm risking to break the boundary of dynamic allocated memory for myStruct variable?我冒着为 myStruct 变量打破动态分配 memory 的边界的风险?
If you will use an index that is greater than or equal to MAX_DEPTH
with the pointer testStruct
then it is evident that there will be undefined behavior,如果您将使用大于或等于MAX_DEPTH
和指针testStruct
的索引,那么很明显会有未定义的行为,
class string is implemented in the following manner: class 字符串的实现方式如下:
namespace std {
template <typename charT,
typename traits = char_traits<charT>,
typename Allocator = allocator<charT>>
class basic_string;
typedef basic_string<char> string;
}
The third argument is the memory model used by the string class which handles the allocation and deallocation of memory.第三个参数是字符串 class 使用的 memory model ,它处理 ZCD69B4957F06CD8298DZ7 的分配和解除分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.