C/C++

From Things and Stuff Wiki
Jump to: navigation, search

Things and Stuff Wiki - an organically evolving knowledge base personal wiki with a totally on-the-fly taxonomy containing topic outlines, descriptions and breadcrumbs, with links to sites, systems, software, manuals, organisations, people, articles, guides, slides, papers, books, comments, screencasts, webcasts, scratchpads and more. use the Table of Contents for navigation on longer pages. see About for further information. / et / em

total mess / mix

C

1972 / 1979

to separate out

  • http://en.wikipedia.org/wiki/C_(programming_language) - a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.

C was originally developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs, and used to re-implement the Unix operating system. It has since become one of the most widely used programming languages of all time, with C compilers from various vendors available for the majority of existing computer architectures and operating systems. C has been standardized by the American National Standards Institute (ANSI) since 1989 (see ANSI C) and subsequently by the International Organization for Standardization (ISO).

Varieties


  • http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html a reference manual for the C programming language as implemented by the GNU Compiler Collection (GCC). Specifically, this manual aims to document: The 1989 ANSI C standard, commonly known as “C89”, The 1999 ISO C standard, commonly known as “C99”, to the extent that C99 is implemented by GCC, The current state of GNU extensions to standard C


Guides


  • Learn C The Hard Way will fill in the "beginning programmer" gap in the literature on the C Programming language. It will teach good modern C programming practices and avoid habits that lead to buffer overflows, security defects, and other problems that even experienced programmers cause. This book is designed for: Students who have read LPTHW or LRTHW. Programmers with skill in one other "scripting" language. Programmers who need to relearn C. Objective-C programmers who want better C skills. People who like a challenge.
  • C programming language coding guidelines - Eric Laroche, 1998 - This paper gives a comprehensive insight into the author's guidelines in C programming. Many aspects that are needed to define these guidelines are thoroughly discussed. The paper provides much background information needed for the decisions about do's and don't's in C coding.


Runtime

  • https://en.wikipedia.org/wiki/crt0 - a set of execution startup routines linked into a C program that performs any initialization work required before calling the program's main function. It generally takes the form of an object file called crt0.o, often written in assembly language, which is automatically included by the linker into every executable file it builds.


Syntax

By Dan Gookin from C All-in-One Desk Reference For Dummies


C programs consist of variables and functions. The language consists of expressions and statements. Expressions are used to compute new values which can be either used directly – as in function arguments – or assigned to a variable. Statements are assignments, flow control, declarations, or blocks. Flow control is conditional (if then else), looping (for, while, or do..while), or multi-way conditional (switch). Blocks are enclosed in braces ( { … } ) and define a local scope. Declarations create variable instances.

C is a pretty old language so there weren’t a lot of neat ideas in play to steal. Block structuring was and is and is very nice. Any collection of statements within a function which is wrapped in braces is a block. The block creates a local scope which exists until the closing brace. This means you can define local variables which shadow external variables quite freely. This makes you code easier to read and safer to develop because you can use your locals locally and know that anything else which is used must be defined in a higher level scope.

One thing you cannot do in C’s blocks is define functions. C does not support nested functions, so you don’t get closures. [of course you can implement closures in C by writing a lisp interpreter and implementing closures in lisp and then . . . – which is how closures are implemented in Ruby and Python and a lot of other languages (which are actually implemented in C or some other similarly lower level language)]

Pointers – variables which contain addresses.

C has an implicit call stack and a heap for dynamically managed memory. The call stack is managed automatically: it get’s bigger when a function is called and smaller when the function returns. The heap is managed by you.

You allocate memory by calling a function which grabs some and reserves it for you. You get a pointer to the memory. You specified how big it is supposed to be and you’re supposed to be smart enough to remember that.

You free memory by calling a function (‘free(pointer)’) which returns the memory to the free list. If you forget to do this and forget the address of the allocated memory, you create a ‘memory leak’ while your program is running. If you free some memory which is still in use – either continuously or at some later time – interesting and bad things can happen.

Standard library

  • https://en.wikipedia.org/wiki/C_standard_library - the standard library for the C programming language, as specified in the ANSI C standard.[1] It was developed at the same time as the C library POSIX specification, which is a superset of it.[2][3] Since ANSI C was adopted by the International Organization for Standardization,[4] the C standard library is also called the ISO C library.

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.


  • https://en.wikipedia.org/wiki/struct_(C_programming_language) - a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address. The struct can contain many other complex and simple data types in an association, so is a natural organizing type for records like the mixed data types in lists of directory entries reading a hard drive (file length, name, extension, physical (cylinder, disk, head indexes) address, etc.), or other mixed record type (patient names, address, telephone... insurance codes, balance, etc.).

The C struct directly corresponds to the assembly language data type of the same use, and both reference a contiguous block of physical memory, usually delimited (sized) by word-length boundaries. Language implementations which could utilize half-word or byte boundaries (giving denser packing, using less memory) were considered advanced in the mid-eighties. Being a block of contiguous memory, each variable within is located a fixed offset from the index zero reference, the pointer. As an illustration, many BASIC interpreters once fielded a string data struct organization with one value recording string length, one indexing (cursor value of) the previous line, one pointing to the string data.




to sort


  • The Descent to C - This article attempts to give a sort of ‘orientation tour’ for people whose previous programming background is in high (ish) level languages such as Java or Python, and who now find that they need or want to learn C.


  • https://github.com/j3ffhubb/libshds - provides a single header library of useful data structures with good performance and scalability characteristics, while being generic and object-oriented. The implementation is intended to be portable across as many platforms, compilers and C standards as possible.

Wherever practical, the library avoids implementing functions for data structures if the function does not scale well, and instead focuses on implementing functions that scale in O(1) if possible, or at least algorithms that scale in O(log n) (for searching, etc...) or O(n log n) (for sorting). libshds is not meant to be a full-blown framework to make C into a high(er) level language, but rather a lightweight and useful tool to ease the development of low-level code that scales.



int      foo[5];     // foo is an array of 5 ints
char    *foo;        // foo is a pointer to char
double   foo();      // foo is a function returning a double
  • The ``Clockwise/Spiral Rule - There is a technique known as the ``Clockwise/Spiral Rule which enables any C programmer to parse in their head any C declaration! [5]

C has little namespacing functionality at the language level. In the case of identifiers (functions and variables), there is one namespace, subject to scoping rules. And any identifier that must be shared between two compilation units is necessarily shared between them all: when you “extern” an identifier, you are using the link-level namespace.

The common workaround to C’s lack of namespaces is to use a standard name prefix for each module. The prefix indicates the app, library or module the name belongs to. For example, in Subversion you have names like svn_fs_initialize — this indicates that the function belongs to the svn_fs library. Subversion describes several layers of namespacing conventions. svn_fs_initialize is an example of a name in the public API. Names that need to visible to other modules within a single Subversion library use a double underscore to separate the namespace, for example, svn_fs_base__dag_get_node. Finally, non-exported functions used within a single module do not use a prefix.



  • Talloc is a hierarchical, reference counted memory pool system with destructors. It is built atop the C standard library and it defines a set of utility functions that altogether simplifies allocation and deallocation of data, especially for complex structures that contain many dynamically allocated elements such as strings and arrays.


  • Comprehensive C Archive Network (CCAN) - That nice snippets of C code should be moved out of junkcode directories and exposed to a wider world, where they can become something useful.



Contrib

  • musl provides standard C/POSIX library and extensions, consistent quality and implementation behaviour from tiny embedded systems to full-fledged servers. Minimal machine-specific code means less chance of breakage on minority architectures and better success with “write once run everywhere” C development.

musl's efficiency is unparalleled in Linux libc implementations. Designed from the ground up for static linking, musl carefully avoids pulling in large amounts of code or data that the application will not use. Dynamic linking is also efficient; by integrating the entire standard library implementation, including threads, math, and even the dynamic linker itself into a single shared object, most of the startup time and memory overhead of dynamic linking have been eliminated.

  • Cello is a library that brings higher level programming to C. By acting as a modern, powerful runtime system Cello makes many things easy that were previously impractical or awkward in C such as: Generic Data Structures, Polymorphic Functions, Interfaces / Type Classes, Constructors / Destructors, Optional Garbage Collection, Exceptions, Reflection [8]
  • https://github.com/attractivechaos/klib Klib is a standalone and lightweight C library distributed under MIT/X11 license. Most components are independent of external libraries, except the standard C library, and independent of each other. Klib strives for efficiency and a small memory footprint. Some components, such as khash.h, kbtree.h, ksort.h and kvec.h, are among the most efficient implementations of similar algorithms or data structures in all programming languages, in terms of both speed and memory use.


  • libevent – an event notification library. The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts. libevent is meant to replace the event loop found in event driven network servers. An application just needs to call event_dispatch() and then add or remove events dynamically without having to change the event loop. Currently, libevent supports /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4).


  • libev - A full-featured and high-performance (see benchmark) event loop that is loosely modelled after libevent, but without its limitations and bugs. It is used in GNU Virtual Private Ethernet, rxvt-unicode, auditd, the Deliantra MORPG Server and Client, and many other programs.

Features include child/pid watchers, periodic timers based on wallclock (absolute) time (in addition to timers using relative timeouts), as well as epoll/kqueue/event ports/inotify/eventfd/signalfd support, fast timer management, time jump detection and correction, and ease-of-use. It can be used as a libevent replacement using its emulation API or directly embedded into your programs without the need for complex configuration support. A full-featured and well-documented perl interface is also available.


  • Dyad.c is an asynchronous networking library which aims to be lightweight, portable and easy to use. It can be used both to create small standalone servers and to provide network support to existing project [9]


  • FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) in one or more dimensions, of arbitrary input size, and of both real and complex data (as well as of even/odd data, i.e. the discrete cosine/sine transforms or DCT/DST). We believe that FFTW, which is free software, should become the FFT library of choice for most applications.


  • SWIG is a compiler that integrates C and C++ with languages including Perl, Python, Tcl, Ruby, PHP, Java, C#, D, Go, Lua, Octave, R, Scheme (Guile, MzScheme/Racket, CHICKEN), Scilab, Ocaml, Modula-3, Common Lisp (CLISP, Allegro CL, CFFI, UFFI) and Pike. SWIG can also export its parse tree into XML and Lisp s-expressions.

SWIG reads annotated C/C++ header files and creates wrapper code (glue code) in order to make the corresponding C/C++ libraries available to the listed languages, or to extend C/C++ programs with a scripting language.



  • CCAN - nice snippets of C code should be moved out of junkcode directories and exposed to a wider world, where they can become something useful. CCAN is loosely modelled after the successful CPAN project for Perl code development and sharing.


  • offbrand - collection of reference counted generic data structures written in pure C for the C programmer. The library is intended to relieve the common need to reimplement a standard data structures in an application. Never again need to write an automatically resizing array for your application specific datatype and use the vector included in offbrand instead.
  • GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface. The main target applications for GMP are cryptography applications and research, Internet security applications, algebra systems, computational algebra research, etc.
  • https://github.com/ned14/uthash - a hash table, implemented in C, supporting constant-time add/find/remove of C structures. Any structure having a unique, arbitrarily-typed key member can be hashed by adding a UT_hash_handle member to the structure and calling these macros
  • c4 - C in four functions. An exercise in minimalism. char, int, and pointer types; if, while, return, and expression statements; just enough features to allow self-compilation and a bit more [11]


to sort

  • MetaC language extends C in a 100% backward compatible way with reflective features and techniques for refactoring, reconfiguring and modifying arbitrary C source code. Therefore, the extensions provide special metadata types for working with source code information, syntactical structures for the definiton of code templates, and metafunctions to gather information about source code and refactor, modify, delete, or insert code.
  • clib(1) - Package manager for the C programming language.



Compiling

cc source.c   -o execurtablename

CFLAGS=-Wall -g

clean:

  rm -f ex1
  • QBE aims to be a pure C embeddable backend that provides 70% of the performance of advanced compilers in 10% of the code. Its small size serves both its aspirations of correctness and our ability to understand, fix, and improve it. It also serves its users by providing trivial integration and great flexibility. [15]

C++

1983



The C++ standard provides some standard algorithms collected in the <algorithm> standard header. A handful of algorithms are also in the <numeric> header. All algorithms are in the std namespace.

  • https://en.wikipedia.org/wiki/Functional_(C%2B%2B) - refers to a header file that is part of the C++ Standard Library and provides a set of predefined class templates for function objects, including operations for arithmetic, comparisons, and logic. Instances of these class templates are C++ classes that define a function call operator, and the instances of these classes can be called as if they were functions.[1] It is possible to perform very sophisticated operations without writing a new function object, simply by combining predefined function objects and function object adaptors.

The class template std::function provided by C++11 is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any callable target—functions, lambda expressions (expressions defining anonymous functions), bind expressions (instances of function adapters that transform functions to other functions of smaller arity by providing values for some of the arguments), or other function objects. The algorithms provided by the C++ Standard Library do not require function objects of more than two arguments. Function objects that return Boolean values are an important special case. A unary function whose return type is bool is called a predicate, and a binary function whose return type is bool is called a binary predicate.




using namespace std

After you write this instruction, if the compiler sees string it will know that you may be referring to std::string, and if it sees vector, it will know that you may be referring to std::vector. (Provided that you have included in your compilation unit the header files where they are defined, of course.)

If you don't write it, when the compiler sees string or vector it will not know what you are refering to. You will need to explicitly tell it std::string or std::vector, and if you don't, you will get a compile error. [16]




Libraries

  • Boost provides free peer-reviewed portable C++ source libraries.



to sort

  • Hoard is a fast, scalable, and memory-efficient memory allocator that can speed up your applications. It’s much faster than built-in system allocators: as much as 2.5x faster than Linux, 3x faster than Windows, and 7x faster than Mac.
  • Memory Pool System is a flexible and adaptable memory manager. Among its many advantages are an incremental garbage collector with very low pause times, and an extremely robust implementation. It's both open source and commercially licensed.
  • Simple DirectMedia Layer is a cross-platform development library designed to provide low level access to audio, leopard, mouse, joystick, and graphics hardware via OpenGL and Direct3D. It is used by video playback software, emulators, and popular games including Valve's award winning catalog and many Humble Bundle games.
  • openBliSSART is a C++ framework and toolbox that provides "Blind Source Separation for Audio Recognition Tasks". Its areas of application include, but are not limited to, instrument separation (e.g. extraction of drum tracks from popular music), speech enhancement, and feature extraction. It features various source separation algorithms, with a strong focus on variants of Non-Negative Matrix Factorization (NMF).


  • http://libcinder.org/ Cinder is a C++ library for programming with aesthetic intent - the sort of development often called creative coding. This includes domains like graphics, audio, video, and computational geometry. Cinder is cross-platform, with official support for OS X, Windows, iOS, and WinRT. Cinder is production-proven, powerful enough to be the primary tool for professionals, but still suitable for learning and experimentation. Cinder is released under the 2-Clause BSD License.
  • cdecl - C gibberish ↔ English
    • cdecl, c++decl - Compose C and C++ type declarations


openFrameworks

  • openFrameworks is an open source C++ toolkit designed to assist the creative process by providing a simple and intuitive framework for experimentation. The code is written to be massively cross-compatible. Right now we support five operating systems (Windows, OSX, Linux, iOS, Android) and four IDEs (XCode, Code::Blocks, and Visual Studio and Eclipse). The API is designed to be minimal and easy to grasp. openFrameworks is distributed under the MIT License. The toolkit is designed to work as a general purpose glue, and wraps together several commonly used libraries, including:
  • OpenGL, GLEW, GLUT, libtess2 and cairo for graphics
  • rtAudio, PortAudio, OpenAL and Kiss FFT or FMOD for audio input, output and analysis
  • FreeType for fonts
  • FreeImage for image saving and loading
  • Quicktime, GStreamer and videoInput for video playback and grabbing
  • Poco for a variety of utilities
  • OpenCV for computer vision
  • Assimp for 3D model loading


Compiling