-5

I was told by an experienced C developer that the concept of Static variables was not the same as that of static memory allocation. Is this correct? If so, what is the difference between the two concepts?

What is the formal definition of Static Memory Allocation and how does it differ from the formal definition of a Static variable?

This question refers to how these concepts map to the C programming language only.

brice
  • 24,329
  • 7
  • 79
  • 95
  • If we removed the word 'static' from the title, we'd have "What is the difference between variables and memory allocation?", which is an odd contrast to be making — variables are one thing, and memory allocation is related but difference. I'm not sure the contrast is really useful though. – Jonathan Leffler Apr 04 '13 at 05:16

1 Answers1

2

There is no essential difference in meaning between the two concepts.

Essentially, static variables are variables whose memory is allocated using static memory allocation.

This is supported by the following uses of the terms:

  • GNU's libc manual.

    • Static allocation is what happens when you declare a static or global variable. Each static or global variable defines one block of space, of a fixed size. The space is allocated once, when your program is started (part of the exec operation), and is never freed.
    • Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable. The space for an automatic variable is allocated when the compound statement containing the declaration is entered, and is freed when that compound statement is exited. In GNU C, the size of the automatic storage can be an expression that varies. In other C implementations, it must be a constant.
  • K&R second edition, Appendix A, section 4.1 "Storage Class"

    There are two storage classes: automatic and static. Several keywords, together with the context of an object's declaration, specify its storage class. Automatic objects are local to a block (Par.9.3), and are discarded on exit from the block. Declarations within a block create automatic objects if no storage class specification is mentioned, or if the auto specifier is used. Objects declared register are automatic, and are (if possible) stored in fast registers of the machine.

    Static objects may be local to a block or external to all blocks, but in either case retain their values across exit from and reentry to functions and blocks. Within a block, including a block that provides the code for a function, static objects are declared with the keyword static. The objects declared outside all blocks, at the same level as function definitions, are always static. They may be made local to a particular translation unit by use of the static keyword; this gives them internal linkage. They become global to an entire program by omitting an explicit storage class, or by using the keyword extern; this gives them external linkage.

    This seems to confirm the equivalence.

  • Compilers, Principles, Techniques & Tools Section 7.1.1 Static Versus Dynamic Storage Allocation

    The two adjectives static and dynamic distinguish between compile time and run time, respectively.

  • Operating Systems, A concept based approach D M Dhamdhere, page 194

    Definition 5.1 (Static and dynamic binding) Static binding is a binding performed before the operation of a program (or a software system) begins, while dynamic binding is a binding performed during its operation.

    [...]

    Static and dynamic memory allocation Static memory allocation can be performed by a compiler, linker or loader while reading a program for execution. Dynamic memory allocation is performed in a lazy manner. That is, memory allocation is allocated to an entity just before it is used for the first time during the execution of a program.

    [...]

    Static memory allocation does not require any memory allocation actions during program execution. By contrast, dynamic allocation incurs the overhead of memory allocation actions performed during the execution of a program. Some of these actions are even repeated several times during execution of a program.

  • Programming in C - A Practical approach by Ajay Mittal. Section 7.7, Conceptual Questions and Answers, No 25 Page 444

    The allocation of memory at the run time (i.e. as the program executes) is known as dynamic memory allocation. In C, memory can be allocated dynamically by calling malloc, calloc or realloc functions. The allocation of memory at the compile time is called static memory allocation

    This is followed by a table that explains the difference in more details. You can see it in google books

  • Memory as a Programming Concept in C and C++ page 15:

    [...] and thus we speak of static memory allocation or (memory allocation at compile time or memory allocation by the compiler) even though, strictly speaking, the compiler does not allocate any memory when the program is run. It may even be the case that, when a program is run, the compiler used to compile the program no longer exists.

  • Wikipedia Confirms the equivalence between the two concepts: Static memory allocation and Static Variable both refer each other and explicitly say that a static variable is a variable that has been allocated statically.

brice
  • 24,329
  • 7
  • 79
  • 95
  • 2
    Actually, no. The first two sources talk about them being equivalent, while the rest make the distinction. (although the phrasing of "memory allocated by the compiler" is done right only by *Memory as a Programming Concept in C and C++*). – Luchian Grigore Apr 04 '13 at 05:56