-6

I want to try catch the free(hello); so that the free(world); can still be executed for freeing all allocated memory in the main program.

int main()
{
    const char *hello = "Hello World";

    char *world = malloc(strlen(hello) + 1);
    strcpy(world, hello);

    free(hello);
    free(world);
    
    return 0;
}

Here is the expected code in Java style:

try{
    free(hello);
}catch(Throwable ex) {
}finally{
}

free(world);

How to try catch finally the free(hello); in c?

13
  • 12
    C doesn't have exceptions. Commented Oct 28 at 14:05
  • 10
    You didn't malloc hello so you cannot (and must not) free it. All allocations are released when your program exits (or crashes) anyway. Commented Oct 28 at 14:14
  • 3
    It's worth noting that a call to strcpy or free simply cannot fail gracefully. Causing a segfault with a call to strcpy is not at all analogous to, for instance, raising an IllegalArgumentException in Java. Attempting to recover from misuse of a function is not sane in C. Commented Oct 28 at 14:22
  • 2
    And misusing a function in C is not like misusing a function in Java. C has a concept of "undefined behavior" where, per rules of the language, anything can happen. C doesn't hold your hand - if you do something wrong, memory can get corrupted - code gets overwritten, data gets destroyed. That's just one possible result of doing something wrong. Commented Oct 28 at 14:33
  • 3
    The simple answer is that C doesn't provide any way to do this. Commented Oct 28 at 14:49

2 Answers 2

13

You seem to have a misconception regarding how error handling works in C and what happens in exceptional cases.

There are certain things a C program can do that will give rise to undefined behavior. Attempting to call free on a pointer that was not returned by malloc is one of those things.

When undefined behavior happens, the C standard imposes no requirements on what the program will do. It could crash, it could output strange results, or it could appear to work properly (and possibly crash later at a seemingly unrelated point).

So attempting to capture what will happen when you call free(hello) makes no sense in part because there's no guarantee something "bad" will happen at that time. So even if you were to (for example) set up a signal handler for SIGSEGV it might not trigger, and even if it does you program will be in such a state that there's nothing you can really do to "clean up".

So to summarize, given the question of how to capture undefined behavior: don't cause undefined behavior.

That being said, there are tools such as Valgrind that can detect memory management errors so that they can be prevented from happening in the first place.

Sign up to request clarification or add additional context in comments.

1 Comment

Side note: if a handler for SIGSEGV does trigger, then (even more) UB occurs if and when the handler returns. To avoid that, such a handler should terminate the program instead of returning.
5

C doesn't have any form of structured exception handling. You can kind-of sort-of fake it with signals and with setjmp/longjmp, but then Malcolm's Law1 applies.

The problem is that library functions like free don't throw exceptions. If you pass it an invalid pointer or a pointer to something that wasn't allocated with *alloc, it'll do something, but that something isn't well-defined. It could crash, it could corrupt data, it could appear to work but leave the program in a bad state such that it crashes elsewhere.

What it won't do is raise any kind of exception or signal that can be caught and handled.

You just can't do this in C.


1. "You were so preoccupied with whether you could that you didn't stop to think if you should."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.