0

I want to use Intel's Math Kernel Library for some matrix manipulation and multiplication. I have read Intel's tutorials, here is an example of the arrays they use :

A = (double *)mkl_malloc(m*p*sizeof(double), 64);

Now, I'm new to C++, I have to write several functions that will return arrays, to be multiplied in a global function. But I have read that it is a bad idea to return pointers on arrays unless you know what you're doing. Can I have some clarification on this ? with an example if possible ?

Here is what I'm thinking of :

double* pexpList(double first, double last, size_t n)
{
    double *vector;
    vector = (double *)mkl_malloc(n * sizeof(double), 64);
    double m = (double) 1 / (n - 1);

    if (first * last > 0.0) {
        double quotient = pow(last / first, m);

        vector[0] = first;
        vector[n - 1] = last;

        for (size_t i = 1; i < n - 1; i++)
            vector[i] = vector[i - 1] * quotient;
    }

    return vector;
}
Naucle
  • 626
  • 12
  • 28

1 Answers1

2

You could say that there are two types of arrays: Statically allocated arrays, the ones you usually call arrays, and are declared like

double array[SOME_SIZE];

Then there's the second kind of array, which are allocated dynamically using e.g. malloc (or in your case mkl_malloc):

double *array = malloc(some_size);

The biggest difference between those two kind of arrays, is that for the first type of array it's allocated (usually) on the stack and that you can use sizeof to get its size, and the second type is allocated on the heap and you need to keep track of its size yourself (and also free it when done with it).

Both types can otherwise be used exactly the same, as arrays of the first type decays to pointers, and you can use array-indexing syntax with pointers.

The thing about returning pointers from functions is that if you declare the first type of array as a local variable inside a function, that variable will go out of scope once the function returns and any pointers to it become invalid. For the second type there is no such problem, as the memory pointed to will not go out of scope and will be valid until you free the memory.


To illustrate with an example:

double *function1(void)
{
    double array[SOME_SIZE];
    return array;
}

double *function2(void)
{
    double *ptr = malloc(SOME_SIZE * sizeof(*array));
    return ptr;
}

int main(void)
{
    double *ptr1 = function1();  // Invalid pointer
    double *ptr2 = function2();  // Okay pointer
}

The function1 function will return a pointer to the local variable array, but as soon as the function returns array goes out of scope, so in main the variable ptr1 will not point to valid memory, using that variable in any way except making it point somewhere else leads to undefined behavior.

However function2 return a pointer to dynamically allocated memory that exists for the life-time of the program (or until you call free using the pointer), and so that pointer will be valid and can be used freely.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Basic C knowledge, thank you for the clarification, your last paragraph answered my question. I guess I confused the two array types while searching through questions here, for some reason. – Naucle May 11 '15 at 08:54