background preloader

C/C++

Facebook Twitter

Development blog: Sensible Error Handling: Part 1. To err is human. But it is also quite computery. Unfortunately, error handling tends to bring out the worst in APIs. Error handling is what makes your program go from something nice, clear and readable such as: Stuff s = open_something(x); int len = get_size(s); ... To some horrible monstrosity such as: Stuff s; int err = open_something(x, &s); if (err == E_STUFFNOTFOUND) { fprintf(stderr, ”Something was not found”); goto exit; } else if (err == E_INVAL) { fprintf(stderr, ”Something was invalid”); goto exit; } else if (err == E_RETRY || err = E_COMPUTERNOTINTHEMOOD) goto exit; int len = 0; err = get_size(s, &len); if (err == E_HULLNOTPOLARIZED) goto close_and_exit; ...

In this article (and the follow-up) I’m going to discuss how I think you should design systems so that the error handling is as sensible as possible and the burden on the callers is minimized. Types of Errors An unexpected error is an error that happens when the caller has no reason to assume that something might go wrong. Revenge of the Nerds. May 2002 In the software business there is an ongoing struggle between the pointy-headed academics, and another equally formidable force, the pointy-haired bosses. Everyone knows who the pointy-haired boss is, right? I think most people in the technology world not only recognize this cartoon character, but know the actual person in their company that he is modelled upon. The pointy-haired boss miraculously combines two qualities that are common by themselves, but rarely seen together: (a) he knows nothing whatsoever about technology, and (b) he has very strong opinions about it.

Suppose, for example, you need to write a piece of software. Why does he think this? Well, this doesn't sound that unreasonable. But all languages are not equivalent, and I think I can prove this to you without even getting into the differences between them. Presumably, if you create a new language, it's because you think it's better in some way than what people already had. So, who's right? Catching Up with Math. Herb Sutter: (Not Your Father’s) C++ | Lang.NEXT 2012.

Life of an instruction in LLVM. LLVM is a complex piece of software. There are several paths one may take on the quest of understanding how it works, none of which is simple. I recently had to dig in some areas of LLVM I was not previously familiar with, and this article is one of the outcomes of this quest. What I aim to do here is follow the various incarnations an "instruction" takes when it goes through LLVM’s multiple compilation stages, starting from a syntactic construct in the source language and until being encoded as binary machine code in an output object file.

This article in itself will not teach one how LLVM works. It assumes some existing familiarity with LLVM’s design and code base, and leaves a lot of "obvious" details out. Note that unless otherwise stated, the information here is relevant to LLVM 3.2. Input code I want to start this exploration process at the beginning – C source. Int foo(int aa, int bb, int cc) { int sum = aa + bb; return sum / cc; } Clang Here is the LLVM IR created for the function : Polymorphism in C. Introduction Polymorphism is by far the most important and widely used concept in object oriented programming.

Some of the widely used technologies and libraries like COM, MFC etc. have polymorphism as their foundation. If you look at all the original design patterns, almost every pattern uses polymorphism in its structure. In this article I hope to unveil the work done by the C++ compiler in implementing polymorphism. Some of the internals of C++ like virtual table, virtual table pointer etc. will also be touched upon while we implement polymorphism using the C language. The Problem I have taken a simple hierarchy of three classes to implement polymorphism. The class diagram of the problem is as follows: X is the base class which has three virtual functions – One, Two and Three.

Some C++ concepts Here we will take a look at some of the C++ concepts that are not available in C and discuss how to implement the same in C. Constructor and Destructor C++ implementation Our implementation Inheritance. Incompatibilities Between ISO C and ISO C++ The C programming language began to be standardized some time around 1985 by the ANSI X3J9 committee. Several years of effort went by, and in 1989 ANSI approved the new standard. An ISO committee ratified it a year later in 1990 after adding an amendment dealing with internationalization issues.

The 1989 C standard is known officially as ANSI/ISO 9899-1989, Programming Languages - C, and this document refers to the 1989 C standard as C89. The 1990 ISO revision of the standard is known officially as ISO/IEC 9899-1990, Programming Languages - C, which is referred to in this document as "C90". The next version of the C standard was ratified by ISO in 1999. Officially know as ISO/IEC 9899-1999, Programming Languages - C, it is referred to in this document as "C99". The C++ programming language was based on the C programming language as it existed shortly after the ANSI C standardization effort had begun. This document lists only the incompatibilities between C99 and C++98. C as an intermediate language. Here's a Forth program debugged in KDevelop - a graphical debugger without Forth support: Cool stuff, not? The syntax highlighting for Forth files - that's someone else's work that comes with the standard KDevelop installation.

But the rest - being able to run Forth under KDevelop, place breakpoints, and look at program state - all that stuff is something we'll develop below. I'll show all the required code; we don't have to do very much, because we get a lot for free by using C as an intermediate language. A high-level intermediate language is not unusual. A lot of compilers target an existing high-level platform instead of generating native code - for instance, by generating JVM bytecode or JavaScript source code. PortabilityOptimizationSome degree of library interoperabilitySome degree of tools interoperability (IDEs, debuggers, etc.) Which languages of this kind are the most successful? I think C is an awesome intermediate language for a compiler to emit. Then it dawned on me: Forth! . 60+ Free programming books for C, C++, C# | IT Discover. Understanding C by learning assembly.

Last time, Alan showed how to use GDB as a tool to learn C. Today I want to go one step further and use GDB to help us understand assembly as well. Abstraction layers are great tools for building things, but they can sometimes get in the way of learning. My goal in this post is to convince you that in order to rigorously understand C, we must also understand the assembly that our C compiler generates. I'll do this by showing you how to disassemble and read a simple program with GDB, and then we'll use GDB and our knowledge of assembly to understand how static local variables work in C. Note: All the code in this post was compiled on an x86_64 CPU running Mac OS X 10.8.1 using Clang 4.0 with optimizations disabled (-O0*) Learning assembly with GDB Let's start by disassembling a program with GDB and learning how to read the output.

Int main() { int a = 5; int b = a + 6; return 0; } Now compile it with debugging symbols and no optimizations and then run GDB: Registers Back to the code Conclusion. Jason Mooberry » Blog Archive » What I learned from my first c coding challenge.