AI Entities in the Future and the Concepts of Life and Soul

In the future, or even now, these are the entities related to AI: Computer A computer is an oracle computer (knowledge) operating on input/o...

25 March, 2025

Keyword 'goto' in C/C++ Is Actually Very Important Indeed

Consider this malloc situation:

Sometimes, it's because of forgetting to free some pointers when exiting a function with 'return'; and especially when the function is creating a lot of pointers and they need to be freed.

This way to use keyword 'goto' should solve the problem:

//---
void test_func(){
    void* A;
    void* B;
    void* C;
    A = malloc(1000);
    B = malloc(1000);
    C = malloc(1000);
    
    // Some deep logic
    if (1==1){
        if (2==2){
            // Do something
            // ...
            // Need to return? don't 
            // Go to the mem freeing at function tail
            goto ret;
        }
    }
    
    // Do other things here
    // ...
    
    ret:
    free(A);
    free(B);
    free(C);
}
//---

No comments:

Post a Comment