2009-02-12 102 views
0

函数somefunction()将三指针作为参数。为三指针分配内存

int somefunction(tchar ***returnErrors); 

如何为returnErrors参数分配内存?

+0

函数应该做什么?你有文件吗? – jpalecek 2009-02-12 10:58:26

回答

2

在猜测。 。 。

您可以将returnErrors看作指向字符串数组的指针。

  1. 第一* imples指针TCHAR的阵列 (或 返回tchars单个字符串)
  2. 第二* imples的指针 阵列串。
  3. 最后*因此您可以更改 returnErrors并将新的 内存传回。

要delare内存本(傻例如,内SomeFunction分配内存)

tchar ** errors; 
// Oops it appears I need to pass back two error strings (+ 1 for null on end, so we know there are no more - thanks tlholaday) 
errors = malloc(sizeof(tchar*) * 3); 

// the first string has length 20 (+ 1 for null terminator) 
errors[0] = malloc(sizeof(tchar) * 21); 

// the second string has length 30 (+ 1 for null terminator) 
errors[1] = malloc(sizeof(tchar) * 31); 

// ensure the last is null 
errors[2] = 0; 

*returnErrors = errors; 

注:调用函数需要知道SomeFunction已分配的内存,需要释放它。

0

如果您还没有该类型的构造,则三指针不会使任何有意义。我宁愿推荐使用一个类 - 或者至少一个标准容器。但是,如果你必须知道,它就像

tchar ***pointer = (tchar***) malloc(sizeof(tchar**) * amount); 
+0

我想你有一个*失踪。它应该是(tchar ***)malloc(...。 – 2009-02-12 12:24:20

+0

哦,你说得对,谢谢 – soulmerge 2009-02-12 16:35:40

+0

只需再做一件事,删除cast并确保包含stdlib.h – ant2009 2010-09-29 16:40:07

1

这取决于“somefunction”的期望值。你必须对此进行调查!

它可能期望指向一个固定大小的数组2或3的常规数组,或?

在我mentionned的情况下,代码可能看起来像

tchar errors[SIZE1][SIZE2]; 
somefunction(&errors); 

tchar errors[SIZE1][SIZE2][SIZE3]; 
somefunction(errors); 
+0

或者可能有其他几十种可能性如果没有“somefunction”的文档(或源代码),完全不可能知道什么是预期的。 – Sol 2009-02-12 16:43:45

2

实施 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并删除[]而不是NULL,因为标签上写着C++,所以免费。

0

有两种用例可以用于我将使用的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*/ 
} 
1

有谁知道如何分配 内存为returnErrors参数?

这个问题太笼统了,在一般情况下无法回答。以下是调用它的可能代码片段的示例。

tchar foo; 
tchar * p_foo = &foo; 
tchar ** pp_foo = &p_foo; 
tchar *** ppp_foo = &pp_foo; 
somefunction(ppp_foo); 

只是一个评论:我会认为你的函数签名是不安全的,因此即使它有一个星星少了代码气味。

此外,请注意:

  • 指针是从来没有的阵列。它是一个 变量,它包含值为 的内存地址或NULL。
  • 指针包含的地址 并不总是对应于数组的起始地址 。 int ** p并不总是引用int [] []的起始地址 。
  • 一个指针,其值包含数组的起始地址 不是 是将此数组作为 函数参数传递的最佳方式。相反,可以使用对数组类型的引用。
  • 数组通常不是在C++中包含一组相关值的最佳方式。应该考虑std :: vector和其他STL容器。 (但是你的问题有两种语言,C和C++,如标签,当然,这仅适用于后者的可能性为什么两个标签?)
0

我倾向于tlholaday的猜测一致,与修改函数可能返回分配的错误数量。

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); 
} 

请注意,无论您使用free还是delete [],都将取决于内存是如何分配的。