When is the garbage colletor called in c...Like in the following code

int main()
    {
    int *i;
    int *fun();
    i=fun();
    printf("%d\n",*i);
    printf("%d\n",*i);
    }
    int *fun()
    { 
    int k=12;
    return(&k);
}

the address being returned is of a variable that is no longer available....but printf statement prints 12 first time and garbage value second time...Can anyone explain the reason...

Dani AI

Generated

Short answer: C has no garbage collector, and returning the address of a local (automatic) variable is undefined behavior. As pointed out, the object you returned a pointer to ceases to exist when the function returns, so the pointer becomes invalid even if it appears to "work" sometimes.

Why the first print showed 12 and the second did not

  • Local variables are typically stored on the stack. After the function returns that stack slot can be reused by subsequent calls (for example by printf itself), so the value you read later may be whatever was written there afterwards.
  • Compilers and calling conventions vary: sometimes the old bytes remain, sometimes they are clobbered, and sometimes optimizations make the problem invisible or worse. This is all described under C storage duration and undefined behavior (see https://en.cppreference.com/w/c/language/storage_duration and ).

Safe alternatives

  • Return by value (return an int instead of int *).
  • Allocate on the heap and return the pointer, then free it in the caller.
  • Let the caller supply storage (pass a pointer to be filled).
  • Use a static object only when persistent, shared state is appropriate (as noted).

Quick examples (illustrative):

int fun_value(void) { return 12; }
int *fun_alloc(void) {
    int *p = malloc(sizeof *p);
    if (!p) return NULL;
    *p = 12;
    return p;
}
void fun_out(int *out) { *out = 12; }

Troubleshooting

  • Compile with warnings enabled (e.g. -Wall -Wextra) and use runtime tools like AddressSanitizer or Valgrind to catch use-after-return or invalid reads (see AddressSanitizer docs and Valgrind docs). Invalid reads from a returned pointer are not a garbage-collector issue but a lifetime/ownership bug.

Recommended Answers

All 3 Replies

C is not garbage collected. So the garbage collected is never called because there is no garbage collector (unless you're using a lib which provides one - in which case it depends on the lib of course).

The reason that your code (which of course invokes undefined behavior, so might technically behave any way it wants) behaves the way it does is that i contains an address on the stack. The first time you dereference i that address has not yet been re-used because you haven't invoked any other function yet. So it still stores the previous value of k, i.e. 12. The second time you dereference i, printf has been called and presumably stored something on that location on the stack. So it now holds whichever value has been stored there by printf. And that's the value that gets printed.

int k=12;

Now try this and you will see different behavior
static int k=12;

sir I have tried it and I know that by making the variable static printf prints its value all the time...because that variable lifetime has not expired

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.