Blocks from Wiki
来源:互联网 发布:手机淘宝怎样修改星评 编辑:程序博客网 时间:2024/05/01 13:12
Blocks are a nonstandard extension added by Apple Inc. to the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closureswithin these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+,[1] although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+.[2]
Apple designed blocks with the explicit goal of making it easier to write programs for the Grand Central Dispatch threading architecture,[3][4] 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[5] and in the 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.[6]
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.
Examples[edit]
A simple example capturing mutable state in the surrounding scope is an integer range iterator:[7]
#include <stdio.h>#include <Block.h>typedef int (^IntBlock)(); IntBlock MakeCounter(int start, int increment) { __block int i = start; return Block_copy( ^ { int ret = i; i += increment; return ret; }); } int main(void) { IntBlock mycounter = MakeCounter(5, 2); printf("First call: %d\n", mycounter()); printf("Second call: %d\n", mycounter()); printf("Third call: %d\n", mycounter()); /* because it was copied, it must also be released */ Block_release(mycounter); return 0;}/* Output: First call: 5 Second call: 7 Third call: 9*/
Relation to GCC nested functions[edit]
Blocks bear a superficial resemblance to GCC's extension of C to support lexically scoped nested functions.[8] However, GCC's nested functions, unlike blocks, cannot be called after the containing scope has exited.
GCC-style nested functions also require dynamic creation of executable thunks 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.
See also:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html
- Blocks from Wiki
- CODE::BLOCKS 使用手册 及 WIKI
- 802.1x(from Wiki)
- Super Scaler-- From Wiki
- IEEE 802.11 from wiki
- FrameBuffer from Wiki
- Color depth (from Wiki)
- Bit plane from Wiki
- Linux framebuffer from Wiki
- Screenshot from Wiki
- Film frame from Wiki
- Bluetooth stack from wiki
- Business Rules(From Wiki)
- memcache--定义from wiki
- GStreamer---From Huihoo Wiki - Open Source Wiki
- Pixel Aspect Ratio from Wiki
- Linux Kernel Basics from wiki
- rails resource matome ---from wiki
- HDU 3377 Plan 解题报告(插头DP)
- java读取配置文件里的信息
- JBOSS优化
- thinksns
- linux中遇到的问题
- Blocks from Wiki
- 程序员实践经验
- JAVA学习笔记一:JAVA基础JAVA SE
- 黑马程序员_String 对象
- RSA算法原理(一)
- 【分享】8500本豆瓣图书的基本信息、评分和书评
- Oracle字符串类型char,nchar,varchar2,nvarchar2区别
- 如何使用VSS对PB8.0下进行版本控制详解
- grid control端口号7788和7799的含义