Sponsored Links
-->

Monday, January 29, 2018

3D Graphics and Animation Programming Tutorial in C/Linux #07 ...
src: i.ytimg.com

Blocks are a non-standard extension added by Apple Inc. to Clang's implementations of the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+, although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+ and non-Apple systems.

Apple designed blocks with the explicit goal of making it easier to write programs for the Grand Central Dispatch threading architecture, although it is independent of that architecture and can be used in much the same way as closures in other languages. Apple has implemented blocks both in their own branch of the GNU Compiler Collection and in the upstream Clang LLVM compiler front end. Language runtime library support for blocks is also available as part of the LLVM project. The Khronos group uses blocks syntax to enqueue kernels from within kernels as of version 2.0 of OpenCL.

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's 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.


Video Blocks (C language extension)



Examples

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

  • blocks-test.c

Compile and execute


Maps Blocks (C language extension)



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.


Chapter 4: Threads. - ppt video online download
src: slideplayer.com


See also

  • Closure (computer science)
  • Lexical scope
  • Lambda (programming)
  • Spaghetti stack
  • Thunk (functional programming)
  • XNU
  • C++11 (includes "lambda expressions")

Silberschatz, Galvin and Gagne ©2013Operating System Concepts â€
src: images.slideplayer.com


References


C/C++ Graphics Tutorial 1 | Downloading graphics.h header file for ...
src: i.ytimg.com


External links

  • "Clang Language Extensions: Blocks". LLVM Project. Retrieved 2013-01-20. 
  • ""compiler-rt" Runtime Library". LLVM Project. Retrieved 2013-01-20. 

Source of article : Wikipedia