C++ 为三指针分配内存
函数C++ 为三指针分配内存,c++,c,pointers,C++,C,Pointers,函数somefunction()将三个指针作为参数 int somefunction(tchar ***returnErrors); 如何为returnErrors参数分配内存?如果您还没有该类型的构造,则三重指针没有任何意义。我更倾向于建议使用一个类,或者至少是一个标准容器。但如果你一定要知道的话,这很简单 tchar ***pointer = (tchar***) malloc(sizeof(tchar**) * amount); 如果您还没有该类型的构造,则三重指针没有任何意义。我更倾
somefunction()
将三个指针作为参数
int somefunction(tchar ***returnErrors);
如何为
returnErrors
参数分配内存?如果您还没有该类型的构造,则三重指针没有任何意义。我更倾向于建议使用一个类,或者至少是一个标准容器。但如果你一定要知道的话,这很简单
tchar ***pointer = (tchar***) malloc(sizeof(tchar**) * amount);
如果您还没有该类型的构造,则三重指针没有任何意义。我更倾向于建议使用一个类,或者至少是一个标准容器。但如果你一定要知道的话,这很简单
tchar ***pointer = (tchar***) malloc(sizeof(tchar**) * amount);
这取决于“somefunction”期望的是什么。你必须对此进行调查
它可能需要指向维度2的固定大小数组或维度3的常规数组的指针,或者
在我提到的例子中,代码可能看起来像
tchar errors[SIZE1][SIZE2];
somefunction( &errors );
或
这取决于“somefunction”期望的是什么。你必须对此进行调查
它可能需要指向维度2的固定大小数组或维度3的常规数组的指针,或者
在我提到的例子中,代码可能看起来像
tchar errors[SIZE1][SIZE2];
somefunction( &errors );
或
猜猜看
您可以将returnErrors视为指向字符串数组的指针
tchar **theErrors = 0; // a vector of tchar vectors.
somefunction(&theErrors);
if (theErrors) {
// use the error values
// free the memory somehow - this is for a null-terminated convention
tchar **victim = theErrors;
while (*victim) delete[](*victim++);
delete[] theErrors;
}
注意:我使用0和Dele[]而不是空的和免费的,因为标签表示C++。
你是在执行某个函数还是调用函数?< /p> 如果您正在调用somefunction,那么somefunction很可能正在分配内存,因此您需要做的就是将其传递到一个安全的地方,以便在以后进行涂鸦和清理tchar **theErrors = 0; // a vector of tchar vectors.
somefunction(&theErrors);
if (theErrors) {
// use the error values
// free the memory somehow - this is for a null-terminated convention
tchar **victim = theErrors;
while (*victim) delete[](*victim++);
delete[] theErrors;
}
注意:我使用0和Dele[]而不是NULL和FALE,因为标签表示C++。
< P>有两个用例,像函数一样,我将使用。 用于初始化:{
tchar **returnErrors;
initErrors(tchar &returnErrors);
/* now returnErrors has a place in memory*/
}
int initErrors(tchar ***returnErrors)
{
*returnErrors = malloc(sizeof(tchar *) * SIZE1)
for (i = 0; i < NUM_ELEMENTS; i++)
(*returnErrors)[i] = malloc(sizeof(tchar) * SIZE2);
/*add checks for malloc failures*/
}
{
returnErrors[SIZE1][SIZE2][SIZE3];
someFunciton(returnErrors);
}
int someFunciton(tchar ***returnErrors)
{
/*assuming that this is a valid index*/
tchar x = returnErrors[1][1][1];
/*return errors is used as a triple array*/
}
对于像someFunction()这样的函数,我将使用两个用例 用于初始化:
{
tchar **returnErrors;
initErrors(tchar &returnErrors);
/* now returnErrors has a place in memory*/
}
int initErrors(tchar ***returnErrors)
{
*returnErrors = malloc(sizeof(tchar *) * SIZE1)
for (i = 0; i < NUM_ELEMENTS; i++)
(*returnErrors)[i] = malloc(sizeof(tchar) * SIZE2);
/*add checks for malloc failures*/
}
{
returnErrors[SIZE1][SIZE2][SIZE3];
someFunciton(returnErrors);
}
int someFunciton(tchar ***returnErrors)
{
/*assuming that this is a valid index*/
tchar x = returnErrors[1][1][1];
/*return errors is used as a triple array*/
}
有人知道如何分配资源吗
returnErrors参数的内存
这个问题太笼统,不能在一般情况下回答。下面只是一个调用它的可能代码段的示例
tchar foo;
tchar * p_foo = &foo;
tchar ** pp_foo = &p_foo;
tchar *** ppp_foo = &pp_foo;
somefunction(ppp_foo);
只是一个评论:我会认为你的函数签名不安全,因此即使有一个星也没有代码气味。
此外,请注意:- 指针永远不是数组。这是一个 包含的值的变量 内存地址,或NULL
- 指针包含的地址 并不总是与 数组的起始地址。int** p并不总是指 int[]的起始地址
- 其值包含 数组的起始地址不正确 将此数组作为 函数参数。相反,可以使用对数组类型的引用
tchar foo;
tchar * p_foo = &foo;
tchar ** pp_foo = &p_foo;
tchar *** ppp_foo = &pp_foo;
somefunction(ppp_foo);
只是一个评论:我会认为你的函数签名不安全,因此即使有一个星也没有代码气味。
此外,请注意:- 指针永远不是数组。这是一个 包含的值的变量 内存地址,或NULL
- 指针包含的地址 并不总是与 数组的起始地址。int** p并不总是指 int[]的起始地址
- 其值包含 数组的起始地址不正确 将此数组作为 函数参数。相反,可以使用对数组类型的引用
tchar **theErrors = 0; // a vector of tchar vectors.
int nErrors = somefunction(&theErrors);
if (nErrors > 0) {
for (int i = 0; i < nErrors; ++i)
{
printf(theErrors[i]);
free(theErrors[i]);
}
free(theErrors);
}
tchar**theErrors=0;//tchar向量的向量。
int nErrors=somefunction(&theErrors);
如果(错误率>0){
对于(int i=0;i
请注意,使用free还是delete[]将取决于