1

Consider the following C++ function:

SDL_Surface* loadBMP(std::string path, SDL_Surface* loadedBMP){
    //Load bitmap
    SDL_Surface* loadedBMP = SDL_LoadBMP(path);
    if (loadedBMP == NULL){
        printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }

    return loadedBMP;
    
    //Magic
    SDL_FreeSurface(loadedBMP);
}

Now, for the sake of this question, assume that loadedBMP is a previously declared global variable.

Here is my question:
Is there a way to let a function continue running after a return statement? In terms of this function, is there a way to have the final line, SDL_FreeSurface(loadedBMP), run after returning loadedBMP?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Johan
  • 343
  • 1
  • 4
  • 13
  • Depends on what `SDL_FreeSurface` actually represents (`struct`/`class` or preprocessor macro). Also applying side effects through comma operator come to mind. – πάντα ῥεῖ Feb 05 '15 at 19:28
  • `SDL_FreeSurface` is an SDL function which essentially "deletes" a surface. It's not a `class` or a `struct` – Johan Feb 05 '15 at 19:31
  • If there is a global variable name "loadedBMP", you're hiding that variable by declaring a local variable with that name. The function also has a parameter with the same name. What are you hoping to achieve by freeing the surface that you just created? (It will become invalid immediately.) – molbdnilo Feb 05 '15 at 19:32
  • So, I have added that essential tag. Ask for specific things, and don't let a broad audience guess on what you're actually doing. – πάντα ῥεῖ Feb 05 '15 at 19:33
  • 3
    The subject matter of the function has to do with SDL. However, the question itself, "Can I `return` without exiting a function," has no basis in SDL, and has the resounding answer of "No." – Johan Feb 05 '15 at 19:35

5 Answers5

8

No. But yes. No line of the function will be executed after the return statement. However, the return statement also marks the end of the function and therefor the end of the scope. So if you manage to have an object on the stack (like a local variable), it's destructor will be called.

But that's not what you want. You don't want to free what you return, not even after the return statement.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 3
    The destructor is called as part of the return statement, not after the return statement. – James Kanze Feb 05 '15 at 19:28
  • @JamesKanze But it's called after all effects of any expression in the `return` statement are complete, right? So I can't see how this plays a role. – Angew is no longer proud of SO Feb 05 '15 at 19:30
  • @Angew It's sequenced after the complete expression in the return value. On the other hand, it is sequenced before the return, so I can't see how this invalidates the _no_. – James Kanze Feb 05 '15 at 21:36
5

No, since at assembly level the return instruction gives the control back to the caller so it inherently exits the scope.

In addition to this, freeing an SDL_Surface which you are returning leaves you with a dangling surface which could not be used so I don't see the purpose of doing it.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • @NeilKirk: that's not a way to "let a function continue after the return". A return statement will be assembled as the last instruction even if you use a destructor (which is called BEFORE returning to the caller in any case). – Jack Feb 05 '15 at 19:26
1

what you think you want: To run code after the return statement.

what you probably want: To prevent memory leak by ensuring a resource is always released.

For that, use a std::unique_ptr. roughly(pseudocode):

std::unique_ptr<SDL_Surface,SDL_FreeSurface> loadBMP(std::string path){
    //Load bitmap

    std::unique_ptr<SDL_Surface,SDL_FreeSurface> loadedBMP{SDL_LoadBMP(path)};
    if (!loadedBMP){
        printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }

    return loadedBMP;
}
Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55
0

It executes next statements after return, as i have faced similar situation in postgres with language as C, i sent a data set then i wanted to delete all records after returning the set. it did well for me.

For Clarification keep print statements.

-1

No. The reason is that after return the value your data type then next statement not work.The compiler ignore it.