Clever Geek Handbook
📜 ⬆️ ⬇️

Blocks (C language extension)

Blocks ( English blocks ) - an extension of the programming languages C , C ++ , Objective-C , not described in the standards of these languages ​​and created by Apple . The extension allows you to create closures using lambda-like syntax.

“Blocks” were created to facilitate writing applications for the Grand Central Dispatch [1] [2] platform, but can also be used on other platforms. Apple implemented “blocks” in its own GCC compiler branch. A runtime library has been created for LLVM compilers.

Blocks are similar to functions :

  • can take arguments and return values;
  • may have local variables ;
  • can be called like regular functions;
  • have addresses that can be used as ordinary function pointers (that is, pointers to “blocks” can be stored in variables, can be passed to functions).

Unlike functions:

  • inside the "blocks" can be used variables that are available to the function within which the "block" was created.

To work with blocks, the compiler generates additional code. In the process of program execution for each created block, this code creates a hidden object. The object contains the following fields:

  • link to block code;
  • values ​​of local variables available to the function within which the block was created.

To tell the compiler that the address of the “block” (and not the regular function) will be stored in a variable, a special keyword should be used. The keyword is not required if the "block" and the variable are in the same scope .

Example

In the following example [3] , the MakeCounter function creates a block and returns a pointer to it.

  #include <stdio.h> 
 #include <Block.h> 

 // create an alias for the type "pointer to block"
 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 () {
	 IntBlock my_counter = MakeCounter ( 5 , 2 );
	 printf ( "First call:% d \ n " , my_counter () );
	 printf ( "Second call:% d \ n " , my_counter () );
	 printf ( "Third call:% d \ n " , my_counter () );
	
	 // free the memory allocated when creating the block for storing the hidden object
	 Block_release ( my_counter );
	
	 return 0 ;
 }

The program will print the following.

  First call: 5
 Second call: 7
 Third call: 9

The command to compile the example using the clang compiler:

  clang -fblocks blocks-test.c -lBlocksRuntime

See also

  • Short circuit
  • Lambda calculus
  • Lambda expressions
  • XNU

Links

  1. ↑ Apple Technical Brief on Grand Central Dispatch Archived June 12, 2009.
  2. ↑ Mac OS X 10.6 Snow Leopard: the Ars Technica review: Blocks
  3. ↑ Bengtsson, J., Programming with C Blocks on Apple Devices , < http://thirdcog.eu/pwcblocks/ >   Archived October 25, 2010 on Wayback Machine
Source - https://ru.wikipedia.org/w/index.php?title= Blocks_ ( C extension_language)&oldid = 99144609


More articles:

  • Kaminsky, Dina Isaakovna
  • Religion in the USA
  • Tokmok (Ukraine)
  • Coloninae
  • Kuosmanen, Sakari
  • Prison and Prison Workers Day
  • Sakharov, Vsevolod Ivanovich
  • Baleen Weavers
  • Safonovo (city)
  • Goldilocks

All articles

Clever Geek | 2019