Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将内存分配给C++; 当我通过C++代码时,我遇到了一个行进,开发者正在尝试将一个新的单元添加到 char **/COD>变量。执行此操作时,下面是他用来为新单元分配内存的代码行 *(plantValue + (plantCount-1)) = (char *) malloc(sizeof(char) * SAPPlantStr.length());_C++_Memory Management - Fatal编程技术网

将内存分配给C++; 当我通过C++代码时,我遇到了一个行进,开发者正在尝试将一个新的单元添加到 char **/COD>变量。执行此操作时,下面是他用来为新单元分配内存的代码行 *(plantValue + (plantCount-1)) = (char *) malloc(sizeof(char) * SAPPlantStr.length());

将内存分配给C++; 当我通过C++代码时,我遇到了一个行进,开发者正在尝试将一个新的单元添加到 char **/COD>变量。执行此操作时,下面是他用来为新单元分配内存的代码行 *(plantValue + (plantCount-1)) = (char *) malloc(sizeof(char) * SAPPlantStr.length());,c++,memory-management,C++,Memory Management,其中plantValue为char**且plantCount为整数 请有人解释一下上面的代码行,因为我无法理解 提前感谢。您的代码中的plantValue似乎是指向原始C样式字符串指针数组的指针,即: +----------+ plantValue --> | char * | --> String #1 (char **) +----------+ | char * | --> String #2

其中
plantValue
char**
plantCount
为整数

请有人解释一下上面的代码行,因为我无法理解


提前感谢。

您的代码中的
plantValue
似乎是指向原始C样式字符串指针数组的指针,即:

               +----------+
plantValue --> |  char *  | --> String #1
(char **)      +----------+
               |  char *  | --> String #2
               +----------+
               |   ...    |
               +----------+
               |  char *  | --> String #N
               +----------+
因此,该代码基本上是使用
malloc()
分配一个新的C样式字符串,并将指向它的指针存储在上面表示的
char*
向量的插槽中

具体而言,
plantCount-1
是预分配指针数组中第一个可用插槽的索引
plantValue+(plantCount-1)
指向该插槽,并使用
*(plantValue+(plantCount-1))=…
malloc()
返回的字符串指针写入该插槽

<>请注意,上述代码比C++多C;例如,在C++中,您将使用<代码> NeX[]/CODE而不是<代码> MalCube()/<代码>以进行显式动态内存分配;不过,在C++中,你应该使用标准<强>容器类<强> >喜欢<代码>:ST::vector < /COD>,和<代码> STD::String < /C> >而不是原始<代码> char */COD>拥有指针。这些C++类自动管理自己的内存,并简化了代码。

<强> P.S.还指出C样式字符串是NUL终止的,因此,当用<代码> MalCube()/code >分配新字符串的内存时,在计算总长度时,也应该考虑终止NUL(换言之,在代码中应该有<代码> SpAPANTSTR.LUTHOTH(+)+1代码/>代码>

PrimeValtual[PrtucNoto1]。包含一个指向已分配内存的指针,其长度为SAPPlantStr

对于任何指针或数组
p
和索引
i
,表达式
*(p+i)
等于
p[i]
。在您的特定情况下,这意味着
*(plantValue+(plantCount-1))
等于
plantValue[plantCount-1]
。希望这能让你更清楚。如果你用它来学习C++,你应该把代码扔掉。“适当”的C++将用于字符串,以及动态的“数组”。哦,在显示的代码中可能存在错误,因为分配不包括终止<代码> 0的'/Cuth>字符的空间。谢谢您的响应。我已经澄清了我的疑问,他应该这样写的。指针解引用语法令人困惑。感谢您的回复。我已经澄清了我的疑问。