简体   繁体   English

C ++中的Malloc函数

[英]Malloc in function in c++

Hi I'm trying to neaten up my code by moving all of my Malloc calls (ans subsequent malloc checks) into one routine which looks like: 嗨,我正在尝试通过将所有Malloc调用(以及后续的malloc检查)移动到一个看起来像这样的例程中来整理代码:

int Malloc2D(int n, int m, double** array_ptr) {

array_ptr = (double**) malloc(n * sizeof(double*));
if (array_ptr == NULL) {
    std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
    return -1;
}
for (int i = 0; i < n; i++) {
    array_ptr[i] = (double*) malloc(m * sizeof(double));
    if (array_ptr[i] == NULL) {
        std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
        return -1;
    }
}

return 0;
}

But when in main() I do something like: 但是当在main()中时,我会执行以下操作:

double** test;
if(Malloc2d(10, 20, test) == -1) return -1;

and then try and use the array in main I get a segfault? 然后尝试并在主要使用数组我得到一个段错误? Anyone got any ideas? 任何人有任何想法吗? Jack 插口

Since you are passing a double **array_ptr it won't modify the pointer outside the function. 由于您要传递double **array_ptr ,因此不会在函数外部修改指针。

In C++, you could fix it by making it a reference (and use new , since it's C++) 在C ++中,您可以通过将其作为参考来进行修复(并使用new ,因为它是C ++)

int Malloc2D(int n, int m, double**& array_ptr) {

array_ptr = new double*[n]);
if (array_ptr == NULL) {
    std::cout << "ERROR! new failed on line " << __LINE__ << "..." << std::endl;
    return -1;
}
for (int i = 0; i < n; i++) {
    array_ptr[i] = new double[m];
    if (array_ptr[i] == NULL) {
        std::cout << "ERROR! new failed on line " << __LINE__ << "..." << std::endl;
        return -1;
    }
}

return 0;
}

Or, in C-style fashion, we could use another pointer indirection (using &test in the calling code to pass the address of the double ** test ). 或者,以C样式的方式,我们可以使用另一种指针间接方式(在调用代码中使用&test传递double ** test的地址)。

int Malloc2D(int n, int m, double*** array_ptr) {

*array_ptr = (double**) malloc(n * sizeof(double*));
if (array_ptr == NULL) {
    std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
    return -1;
}
for (int i = 0; i < n; i++) {
    (*array_ptr)[i] = (double*) malloc(m * sizeof(double));
    if ((*array_ptr)[i] == NULL) {
        std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
        return -1;
    }
}

return 0;
}

Or you could simply return the pointer to the array in the first place - but this would require some minor changes to the calling code: 或者您可以简单地将指针首先返回到数组-但这需要对调用代码进行一些小的更改:

double** Malloc2D(int n, int m) {

double** array_ptr;
array_ptr = (double**) malloc(n * sizeof(double*));
if (array_ptr == NULL) {
    std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
    return NULL;
}
for (int i = 0; i < n; i++) {
    array_ptr[i] = (double*) malloc(m * sizeof(double));
    if (array_ptr[i] == NULL) {
        std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
        return NULL;
    }
}

return array_ptr;
}

C and C++ are pass-by-value. C和C ++是按值传递的。
So changes made to array_ptr inside your function Malloc2D are not visible outside the function. 因此,在函数Malloc2D内部对array_ptr所做的更改在函数Malloc2D不可见。

You want to pass a pointer to test, and modify the value of that. 您想要传递一个指针进行测试,并修改其值。
(which leads to a triple-pointer; confusing, but not unmanageable) (这导致三重指针;令人困惑,但并非难以控制)

int Malloc2D(int n, int m, double*** pOutput)
{
    double** array_ptr
    array_ptr = (double**) malloc(n * sizeof(double*));
    if (array_ptr == NULL) {
        std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
        return -1;
    }
    for (int i = 0; i < n; i++) {
        array_ptr[i] = (double*) malloc(m * sizeof(double));
        if (array_ptr[i] == NULL) {
            std::cout << "ERROR! malloc failed on line " << __LINE__ << "..." << std::endl;
            return -1;
        }
    }

    *pOutput = array_ptr;
    return 0;
}

Then to use the function, pass the address of test : 然后使用该功能,通过test的地址:

double** test;
if(Malloc2d(10, 20, &test) == -1) return -1;

You should pass a pointer or a reference on your double array_ptr . 您应该在double array_ptr上传递一个指针或引用。 Here, it is copied, and only the copy is modified. 在这里,它被复制,并且只有副本被修改。

int Malloc2D(int n, int m, double** &array_ptr)

and then try and use the array in main I get a segfault 然后尝试并在主要使用数组我得到一个段错误

The problem is that you are not modifying array_ptr since it is being passed by value. 问题是您没有修改array_ptr因为它是按值传递的。 One possible solution would be to pass it by reference: 一种可能的解决方案是通过引用传递它:

int Malloc2D(int n, int m, double** &array_ptr)
                                    ^

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

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