0

Hi can anyone tell me would variable a remain in memory or would it get destroyed immediately.

#include <stdio.h> 

int main()
{
    {
        int a=1;
        lab:
        printf("Value of a : %d",a);  
    }   

    return 0;
}

would int a still remain in memory or not ?

8
  • That depends on the actual implementation. However, its scope ended, so you can't access it anymore, and the compiler may (and most probably will) opt to use its backing memory for another variable.
    – user529758
    Commented Jul 11, 2013 at 13:21
  • 2
    Oh, and int main()...
    – user529758
    Commented Jul 11, 2013 at 13:22
  • @Mr Carbonic Acid; +google_plex. How many times do we see void main()!
    – Bathsheba
    Commented Jul 11, 2013 at 13:22
  • @Bathsheba I think half as many times as we see people casting the return value of malloc().
    – user529758
    Commented Jul 11, 2013 at 13:23
  • @H2CO3 only old SO users are aware about it that too because of so much focus on This link.Still almost all the basic C books are following malloc typecasting with an excuse of portability and backward compatibility.
    – Dayal rai
    Commented Jul 11, 2013 at 13:29

3 Answers 3

3

Nope, a has local scope (declared between brackets) so at the closing brace it will be cleaned up.

If you want it to persist for the entirety of the program, either declare it as static or put it outside of any braces, preferably before you use it.

This has the added benefit of having the compiler initialise it for you.

You can try out the following:

#include <stdio.h>
int a;

int main()
{
    static int b;

    int c;

    printf("%d, %d, %d\n", a, b, c); /* a and b should print 0, printing c is undefined behaviour, anything could be there */

    return 0;
}

As Bathsheba pointed out, static variables should be used judiciously if used in a multi-threaded environment.

2
  • 2
    Nice answer; will +1 if you mention that using static will mess up multithreading.
    – Bathsheba
    Commented Jul 11, 2013 at 13:25
  • You do not need multithreading for it. Simple recursion is enough to probably break it. Commented Jul 11, 2013 at 13:34
3

a is destroyed (popped from the stack) when you get to the } following the line with printf , so no, it does not remain in memory at your comment line.

2
  • I thought for sure that that's implementation-specific in C -- ie: though you can't access it by name anymore, it might still be taking up space on the stack.
    – cHao
    Commented Jul 11, 2013 at 13:29
  • It may well be in memory as it would be perfectly fine for the implementation to just move the stack cursor. You'll probably find that instantiating another int just after the brace would recover the original value intact. Relying on that is asking for trouble though. Probably UB.
    – Bathsheba
    Commented Jul 11, 2013 at 13:32
1

First of all: It is not implementation specific. The C standard explicitly says, that leaving a block destroys an object with auto (local declared) lifetime:

For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. [ISO/IEC9899:TC3, 6.2.4, 5]

Of course, this is hard to test, because it loses it scope, too, in this case. (The other way around is easy to test.) But this is important for a formal reason: If you have a pointer to that object, which lives longer than the object, the program is always incorrect and the behavior is undefined – even an implementation let the object being alive. (Undefnied behavior includes that everything works fine.)

7
  • C's concept of "lifetime" isn't as cut-and-dried as all that. "The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it." (6.2.4, 1; emphasis mine) The end of an object's lifetime doesn't mean the space is cleaned up immediately; it just means it's no longer guaranteed by the standard to exist and only be for that object. (The implementation might have its own rules and guarantees either way, or it might not.)
    – cHao
    Commented Jul 11, 2013 at 13:46
  • Your quotation is from ISO/IEC9899:TC3? I cannot find this, but: "An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated. Allocated storage is described in 7.20.3." at 6.2.4, 1. But this does not matter: What you refer to, is that a implementation, that holds an object longer conforms to the standard. This is important to compiler developer. For an application developer the lifetime ends at the point the block is left. Even an implementation holds an object longer, for the application programmer is is died. … Commented Jul 11, 2013 at 14:34
  • … Every C program that rely on (uses) a longer extent is an incorrect C program , even if the implementation promises to hold the object longer and even this implementation would conform to the standard. The extent is not marked as implementation-defined. Commented Jul 11, 2013 at 14:38
  • My quote is copy/pasted directly from the same PDF you linked. How you didn't see it is a mystery to me, as it is literally the very next sentence after 6.2.4,1 (which you just pasted). The point is, the standard doesn't guarantee that the memory will be reclaimed immediately. It doesn't promise anything past that point. If the compiler makes other guarantees, then code relying on them is correct (if unportable). But code that cares about the particular movement of the stack pointer is already relying heavily on IB; the abstract machine's definition doesn't specify a stack.
    – cHao
    Commented Jul 11, 2013 at 16:34
  • You said, that you quote "(6.2.4, 1; emphasis mine)", your quotation is from 6.2.4, 2. Probably this is the reason, that I did not see it. I looked at 6.2.4, 1. (Somebody told me to do so.) And I did not say, that the standard guarantees the immediate release of the memory. This simple irrelevant to the application developer. And no, the program is not conforming, because it relies on something, that is not flagged as implementation-defined. The phrase "not guaranteed" is used to say to the implementation programmer, that he can decide, when to free. Commented Jul 11, 2013 at 17:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.