mercredi 17 mars 2021

C++ pattern for pointer deletion?

I was wondering if there was a pattern for the deletion of pointers. More specifically when you need to call some kind of intermediate function for data. Here is a example of what I was thinking:

int main() {
    // some code here

    char* return_of_function1 = function1(int_array);
    int return_of_function2 = function2(return_of_function1);
    delete[] return_of_function1;

    // some code here
} 

char* function1(int* int_array) {
    // some code here
}

int function2(char* char_array) {
    // some code here
}
int main() {
    // some code here

    int return_of_function2 = function2(int_array);

    // some code here
}

char* function1(int* int_array) {
    // some code here
}

int function2(int* int_array) {
    // some code here
    return function2(function1(int_array), true);
}

int function2(char* char_array, bool delete_array) {
    // some code here

    if(delete_array) {
        delete[] char_array;
    }

    return /* return value */;
}

The motivation is to avoid intermediate calls for data on properties of a set. Again, I'm just asking if this is commonly used, or even a good idea to begin with.

Thanks so much.

Aucun commentaire:

Enregistrer un commentaire