ArticleslgStudy

science

Blocks (C language extension)

Blocks (C language extension) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Blocks (C language extension) rather than just read about it. In short: Blocks are a non-standard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Using this feature requires support from the compiler as well as a runtime library.

Key takeaways

  • Blocks (C language extension) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Blocks (C language extension) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Blocks (C language extension) from memory before moving on to harder problems.

Reference excerpt

Blocks are a non-standard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Using this feature requires support from the compiler as well as a runtime library. Like function definitions, blocks can take arguments, and declare their own variables internally. Unlike ordinary C function definitions, their value can capture state from their surrounding context. A block definition produces an opaque value which contains both a reference to the code within the block and a snapshot of the current state of local stack variables at the time of its definition. The block may be later invoked in the same manner as a function pointer. The block may be assigned to variables, passed to functions, and otherwise treated like a normal function pointer, although the application programmer (or the API) must mark the block with a special operator (Block_copy()) if it is to be used outside the scope in which it was defined. Given a block value, the code within the block can be executed at any later time by calling it, using the same syntax that would be used for calling a function. Blocks were originally introduced in 2009 with the explicit goal of making it easier to write programs for the Grand Central Dispatch (libdispatch) threading architecture. It does not, however, rely on libdispatch; instead it depends on its own (much simpler) runtime library to work. A blocks runtime library is preinstalled for Mac OS X 10.6+ and iOS 4.0+.

Scope of support There are only two families of compilers that implement blocks: Apple's own branch of the GNU Compiler Collection (Apple GCC 4.2.1 and LLVM-GCC) and the upstream Clang LLVM compiler front end. Apple GCC is abandoned since circa 2011 (XCode 4.2). The most up-to-date description of this language extension is found in Clang documentation. As mentioned above, Apple operating systems released after 2009 have a built-in blocks runtime. The LLVM project provides a "target-independent" runtime that should work for every operating system supported by LLVM.

Applications As mentioned before the main user of blocks is libdispatch, which is used in a variety of Apple products such as the Swift standard library. Blocks syntax is part of the OpenCL C standard in versions 2.0 and 3.0. It is used to enqueue kernels from within kernels. The Khronos Group also defines C++-based heterogenous programming languages including SYCL and C++ for OpenCL. Both use C++'s standard lambda expressions instead.

Examples A simple example capturing mutable state in the surrounding scope is an integer range iterator:

Compile and execute

The blocks runtime is not part of the C library(s) linked by default on some systems. If this is the case, it is required to explicitly link to this library:

The runtime is a part of Clang's runtime, but is sometimes not installed with the clang package. A standalone runtime extracted from compiler-rt is available.

Relation to GCC nested functions Blocks bear a superficial resemblance to GCC's extension of C to support lexically scoped nested functions. However, GCC's nested functions, unlike blocks, must not be called after the containing scope has exited, as that would result in undefined behavior. GCC-style nested functions currently use dynamic creation of executable thunks on most architectures when taking the address of the nested function. On most architectures (including x86), these thunks are created on the stack, which requires marking the stack executable. Executable stacks are generally considered to be a potential security hole. Blocks do not require the use of executable thunks, so they do not share this weakness. On the other hand, blocks introduces a completely new type for the pointer, while pointers to nested functions in GCC are regular function pointers and can be used directly with existing code.

See also Closure (computer programming) Lexical scope Anonymous function Parent pointer tree XNU C++11, which introduces lambda expressions to C++

References

External links

"Plausible Blocks (PLBlocks)". Google Code. 2009. Retrieved 2021-01-26. runtime and toolchain for using blocks in iPhone 2.2+ and Mac OS X 10.5 (of historical relevance only)

Worked examples

Example 1 — a first encounter with Blocks (C language extension)

Start with the simplest possible case. Write down what Blocks (C language extension) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Blocks (C language extension) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Blocks (C language extension) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Blocks (C language extension)

In research
Blocks (C language extension) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Blocks (C language extension) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Blocks (C language extension) is common in secondary-school and first-year university syllabi. It links to neighbouring topics C (programming language), Subroutines, so understanding it makes those chapters shorter.
In everyday life
Look for Blocks (C language extension) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Blocks (C language extension)” →

How to study Blocks (C language extension) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Blocks (C language extension) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Blocks (C language extension) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Blocks (C language extension) in simple terms?

Blocks are a non-standard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Using this feature requires support from the compiler as well as a runtime library.

Why does Blocks (C language extension) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Blocks (C language extension)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Blocks (C language extension).

Tags

  • C (programming language)
  • Subroutines

Keep exploring