6

http://linux.die.net/man/3/pthread_mutex_init

In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.

I know about dynamic allocation. What is the meaning of "statically allocated"?

My question here is to understand the meaning of "statically" allocated. I posted the quote from the man page to provide a context, only.

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • It allows file-scope variables to be initially initialised, as in their example `static pthread_mutex_t foo_mutex = PTHREAD_MUTEX_INITIALIZER; `, – zch Jun 02 '15 at 00:45
  • possible duplicate of [PTHREAD\_MUTEX\_INITIALIZER vs pthread\_mutex\_init ( &mutex, param)](http://stackoverflow.com/questions/14320041/pthread-mutex-initializer-vs-pthread-mutex-init-mutex-param) – zch Jun 02 '15 at 00:47

1 Answers1

6

Statically allocated means that the variable is allocated at compile-time, not at run-time. In C, this can be a global variable at the file scope or a static variable in a function.

A good overview is found here: http://en.wikipedia.org/wiki/Static_memory_allocation

Variables on the stack (i.e., local variables in functions that do not have the static keyword) are allocated when the function is called, sometimes multiple times when a function is called recursively. So they are conceptually different from static memory allocation (which only happens once per program).

mdd
  • 685
  • 5
  • 11
  • 2
    Confusingly, people sometimes say "statically allocated" to include automatic allocation (aka. stack variables) – M.M Jun 02 '15 at 01:52
  • Which means that these variables (like string literals) consume memory when the program is compiled? – Spikatrix Jun 02 '15 at 03:29
  • 1
    @CoolGuy: Basically yes. Their size is determined at compile time. Space for them will be allocated when the binary is loaded into memory. – mdd Jun 02 '15 at 12:08
  • @MattMcNabb So, they aren't same as variables on stack? – Aquarius_Girl Jun 03 '15 at 00:09
  • @TheIndependentAquarius what? I'm saying that people sometimes describe stack variables as "statically allocated" – M.M Jun 03 '15 at 00:30
  • 2
    variables on the stack (i.e., local variables in functions that do not have the ```static``` keyword) are allocated when the function is called, sometimes multiple times when a function is called recursively. So they are conceptually different from static memory allocation (which only happens once per program). – mdd Jun 03 '15 at 00:30
  • It's more accurate to say that they're allocated at program startup, not at compile time. – Keith Thompson Jun 06 '15 at 01:36
  • @MattMcNabb: *Confusingly, people sometimes say "statically allocated" to include automatic allocation* -- Hmm. I've never encountered that usage (which would be completely wrong). – Keith Thompson Jun 06 '15 at 01:36
  • @KeithThompson I've seen it on here from time to time, they take the term to mean anything that is not dynamic allocation (which is stuff obtained by malloc and friends). For example they'd say `char *p = malloc(10)` is dynamic, and `char a[10]` is static – M.M Jun 06 '15 at 01:45
  • @MattMcNabb: Hmm. I'll look out for that, and smite those who do it. – Keith Thompson Jun 06 '15 at 02:11