background preloader

C

Facebook Twitter

Deep C. Project Blog: What Every C Programmer Should Know About Undefined Behavior #1/3. People occasionally ask why LLVM-compiled code sometimes generates SIGTRAP signals when the optimizer is turned on. After digging in, they find that Clang generated a "ud2" instruction (assuming X86 code) - the same as is generated by __builtin_trap(). There are several issues at work here, all centering around undefined behavior in C code and how LLVM handles it. This blog post (the first in a series of three) tries to explain some of these issues so that you can better understand the tradeoffs and complexities involved, and perhaps learn a few more of the dark sides of C. It turns out that C is not a "high level assembler" like many experienced C programmers (particularly folks with a low-level focus) like to think, and that C++ and Objective-C have directly inherited plenty of issues from it. Introduction to Undefined Behavior Translation available in: Japanese Both LLVM IR and the C programming language have the concept of "undefined behavior".

For (i = 0; i <= N; ++i) { ... } Eli Bendersky’s website » Blog Archive » The context sensitivity of C’s grammar, revisited. A few years ago I’ve written about the context sensitivity of the grammar of C – the nasty typedef-name problem that makes the grammar of C ambiguous and requires a hack in the lexer to make it possible for YACC to correctly parse the grammar. Since then, I have implemented this technique in pycparser, and it successfully parses real-world C99 code. However, it turns out that when mixed with the scope rules of C, the typedef-name problem rears its ugly head again, causing even more trouble. The problem The C standard states that names defined with typedef behave in a manner similar to other names in the language. In particular, they should obey the lexical scoping rules. The following is invalid: Since AA is first defined as a type and then redefined as a variable name, in the same scope.

Typedef int AA; int main() { int AA; /* OK - redefining AA in internal scope */ int BB = AA * 2; /* OK - AA is an identifier in this scope! Complications Here’s another example. Possible solutions [P.S. Stephen Gallagher's Tech Blog » Why you should use talloc for your next project. Memory management is hard. This is one of the first things a programmer learns (usually by trial and much error) when they leave academia and get out into the real world. It is very easy to make mistakes when managing memory, especially when a particular piece of data needs to live beyond the life of the function that created it.

It can become difficult to know when the memory is safe to destroy, as well as when it is optimal to destroy it. In standard C, a programmer would use malloc() and free() to manage their memory. The problem with this is that every section of memory is allocated independently. There are no inherent relationships between bits of data. Enter talloc, which is a hierarchical memory-management tool wrapped around C’s malloc(). To provide a trivial example, consider that you wanted to create a new struct containing student data: struct student { char *name; } In a traditional C approach, you would allocate memory for a new student in this manner: talloc_free(student1); The Definitive C++ Book Guide and List.

IGCC - IGCC - Andy Balaam Home. Introduction | Downloading and using | Developing | Links | Copyright Interactive GCC (igcc) is a real-eval-print loop (REPL) for C/C++ programmers. It can be used like this: $ . /igcc g++> int a = 5; g++> a += 2; g++> cout << a << endl; 7 g++> --a; g++> cout << a << endl; 6 g++> It is possible to include header files you need like this: $ . /igcc g++> #include <vector> g++> vector<int> myvec; g++> myvec.push_back( 17 ); g++> printf( "%d\n", myvec.size() ); 1 g++> myvec.push_back( 21 ); g++> printf( "%d\n", myvec.size() ); 2 g++> Compile errors can be tolerated until the code works: $ . Extra include directories can be supplied: $ . Libs can be linked: $ . Your own libs can be linked too: $ . The cstdio, iostream and string headers are automatically included, and the std namespace is automatically in scope.

Downloading and using Download the IGCC tarball from the Sourceforge download area. Untar it like so: tar -xjf igcc-0.1.tar.bz2 And then start the program like this: cd igcc-0.1 . Developing Links Copyright. Rob Pike: Notes on Programming in C. February 21, 1989 Introduction Kernighan and Plauger's The Elements of Programming Style was an important and rightly influential book. But sometimes I feel its concise rules were taken as a cookbook approach to good style instead of the succinct expression of a philosophy they were meant to be.

If the book claims that variable names should be chosen meaningfully, doesn't it then follow that variables whose names are small essays on their use are even better? Isn't MaximumValueUntilOverflow a better name than maxval? What follows is a set of short essays that collectively encourage a philosophy of clarity in programming rather than giving hard rules. Your comments are welcome. Issues of typography A program is a sort of publication. Typographic conventions consistently held are important to clear presentation, of course - indentation is probably the best known and most useful example - but when the ink obscures the intent, typography has taken over. Variable names Ah, variable names. Vs. vs. C Craft - Chapter 1. Introduction. C is the desert island language. This is my favourite statement from a talk Rob Pike gave in 2001. Despite its age, despite many flaws, C is still the de facto standard, the lingua franca.

Why? As with other older languages, inertia is partly to blame, but this cannot be the only reason. C must possess a near-perfect balance of vital language features. Or as Linus Torvalds puts it, “C is the only sane choice”. One of C’s designers, Dennis Ritchie, gives a modest account of the evolution of C. The canonical reference, Kernighan and Ritchie’s "The C Programming Language" is slim, yet contains much more than the language specification. Not only is C easy for humans to understand, but machines too can pick up the language quickly. I can roughly envision the assembly generated by a C statement, so I can make educated guesses about time and space efficiency.

Brevity is the soul of wit. In my Eiffel days, I was encouraged to write "integer", not "int", "character", not "char", and so on.