Home > Back-end >  would anything happen if I pass a pointer function as a parameter?
would anything happen if I pass a pointer function as a parameter?

Time:05-16

I wonder that what would happen if I pass a pointer function as a parameter, is it valid and is the memory of that pointer function stuck somewhere in RAM?

This is the example:

char* Function1(char *array1, int N) {
    ...
    return newChar;
}
char* Function2(char *array2, int M) {
    ...
    return newChar;
}

char* newArray = Function2(Function1(oldArray, N), M);

delete[] oldArray;
delete[] newArray;

Whether Function1(oldArray, N) after having been called will have anything wrong with it?

Thank you very much.

CodePudding user response:

It is valid to write these kind of function calls but as you suspected, the memory you've allocated for the character array in subsequent function calls will be stuck in RAM, a memory leak. Let me explain with the following code.

char* Function1(char *array1, int N) {
    char *newarray = new char [N];
    for (int i = 0; i < N;   i) 
        newarray[i] = array1[i]   1;
    return newarray;
}

char* Function2(char *array2, int N) {
    char *newarray = new char [N];
    for (int i = 0; i < N;   i) 
        newarray[i] = array2[i]   1;
    return newarray;
}

Both the functions allocates a new space, stores the incremented value of the given character array and returns the pointer to the allocated space. Call both the functions as,

int main()
{
    int N = 4;
    char *oldArray = "abcd";
    char *newArray = Function2(Function1(oldArray, N), N);
}

There is nothing wrong in the function call Function1 as it returns the pointer to the newly allocated space and passed it to Function2 as array2. But at the end of Function2, the pointer array2 will be deleted leaving the memory it holds as it is. This results in a memory leak.

To avoid this leak, delete the memory the array2 holds before the end of Function2.

char* Function2(char *array2, int N) {
    ...
    delete[] array2;
    return newarray;
}

Hope it helps!

CodePudding user response:

A " pointer function" isn't really a thing. I think you mean function pointer.

Yes, passing a function pointer as a parameter to a function is perfectly ok but you are not passing a function pointer as parameter here. You pass the value returned by a function that returns a pointer - and that's ok too.

It doesn't matter that it's returning a pointer. Compare with this pointer-free example:

#include <iostream>

int foo(int x) {
    return x   x;
}

int bar(int x) {
    return x * x;
}

int main() {
    std::cout << bar(foo(2)); // prints 16
}

You are not actually passing foo(2) as an argument to bar(). You pass what's returned from foo(2) (which is 4) to bar().

CodePudding user response:

While your code, from the little you've shown, is perfectly valid it is also not what is recommended in modern C .

Here are some problems with this kind of code:

  1. Will the function free the pointer you have passed to it? In that case you must not free it again.
  2. Will the function store the pointer somewhere where it will be accessible at a later time? In that case you can't free the pointer after the function call.
  3. Is the function expecting a pointer to a single char, a C string or maybe even a pointer to an array of chars of some size specified in the documentation?
  4. Is the function returning a pointer to a static buffer? In that case you must not free it.
  5. Is the function returning a pointer to a single char? Then you have to delete it. Or is it returning a pointer to a char array? Then you have to delete[] it. Or maybe it returns a pointer that was created by malloc(). Then you must free() it.

What is int N? It looks suspiciously like that could be the size of the array the pointer points to. But sizes must be size_t because int isn't big enough to hold the size of larger arrays.

You should invest some time and read up on std::array, std::vector, std::span, std::unique_ptr> and std::shared_ptr and related classes. The https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines are also a good read. It's best to forget about raw pointers and C style arrays until you really need them.

  • Related