Tag Info

Hot answers tagged

62

C has a much, much simpler interface, and its rules for converting a source code interface into a binary interface are straightforward enough that generating external interfaces to bind to is done in a well-established manner. C++, on the other hand, has an incredibly complicated interface, and the rules for ABI binding are not standardized at all, neither ...


29

If you're trying to communicate with a speaker of another language, pidgin is easier than Shakespearean English. C's concepts - function calls, pointers, NULL-terminated strings - are very straightforward, so other languages can easily implement them well enough to call C functions. For historical reasons, many other languages are implemented in C, which ...


20

C is one of the oldest languages still around. Its ABI is simple, and virtually every operating system still in use today has been written in it. While some of those OS's may have added stuff e.g. in C#/.NET or whatever on-top, down below they're very much steeped in C. That means that, in order to use the functionality provided by the OS, virtually every ...


7

Asking why microcontrollers like the AVR family let you twiddle the pins directly when an operating system like Linux requires device drivers is an apples-to-suspension-bridges comparison. Microcontrollers are typically single-threaded free-for-alls with no supervisor (OS) and all code has full access to memory, I/O and anything else the chip offers. This ...


6

Leaving out the details other answers already provide: The reason so many languages provides a C binding is that all *nix and Windows operating systems expose most of their OS API via a C interface. So the language implementation already needs to interface with C to be able to run on the major Oses. Therefore, it is straightforward to also offer directly ...


6

There is no reason. If the semantics you're trying to express are fundamentally C-compatible and not something like templates, there is no reason you can bind easier if the implementation is written in C. In fact, it's pretty much by definition that a C interface can be filled out by any implementation that can meet the binary contract- including an ...


5

Start from data structures. Write functions operating on those data structures. If you want encapsulation, do it at the level of modules, not objects. If you want polymorphism, use higher-order functions, not virtual dispatch. That’s about it. OOP as practiced is not a significant departure from procedural programming. It’s primarily a set of reasonable ...


5

Microcontroller programs consist of a number of tasks. Let's say you wanted to make a computer-controlled telescope mount. The tasks would be: Retrieve a new byte of input from the USB serial buffer. Check if we've received a complete command. If so, execute that command. Read the sensors for the current telescope position. Set the proper output to ...


4

Does it really help to have a very specific rule set? No. Quite frankly, no. Or, to put it more constructively - take a look at any source code off github or sourceforge, can you read tit? If the answer is "yes" then you've just demonstrated that a single style is not important for readability. What does help readability is readable code, which may ...


4

What formats of executables and libraries does Embox require, and how can I tell? By reading the documentation. Note that some (most?) kernels designed for embedded use might not even support loading executables from files. They may require applications to be compiled-in to the kernel image. [...] how do I develop on one platform/architecture ...


3

Sounds like the code in the middle is changing the value of the state variable, giving you different execution in the second switch statement. Presumably, this matters; create a new variable, store the value of state into it and use this in the second switch statement: int savedState = state ; switch( savedState ) { ... } // 0, 1, 2, 3 // Other Code ...


3

The main answer is uniformity - if your device is a webcam, you can expose a C library and tell every application that to receive images from your cam, they can easily write to your custom API. I guarantee you will not have a working webcam in any application other than the ones you write! That's typically why you abstract hardware behind a device driver - ...


3

What formats of executables and libraries does Embox require, and how can I tell? Embox is a configurable OS and one can create separate kernel image and user application. But since MCU has rather small memory size, most embedded RTOSes including Embox link all software (kernel, apps etc.) to a single image by default. How does one compile a C ...


3

What is the deployable unit(s) of Embox? As most of RTOS for MCU Embox offers deployable unit as a single image. For Embox It has ELF format and is placed in "./build/base/bin/embox" file. How do you deploy these files to an MCU? It depends on MCU not RTOS. The most popular hardware interface for loading images is JTAG. Here is an example how load ...


3

I wrote my own co-operative multi-threading library for ARM Cortex-M0. It was barely a couple pages of code, and the first version of it didn't take longer than a day to write and debug. The big advantage of roll-your-own is you know the code and you can port it to chips that the RTOS might not support. Also, you spend less time thinking about questions ...


3

After fidgeting around with the code a bit, I've got some better results. I went back to the original paper and ignored the wikipedia page. I've compared the algorithm to other quick select routines with some great results. Ok, here are the methods I have been playing with. Note these are for floating point and also that I changed my method from a void. ...


2

To get the 4 bits (1001) from your example 11001111 (= 0xCF), you need to shift it to the right by 3 and then do a bitwise AND with 00001111b, like so: (x >> 3) & 0x0F. The shift causes the 4 bits you want to be in the least significant position (0000000011001111 >> 3 = 00000000000011001), but then you still have to get rid of the most ...


2

I think its fairly clear that a consistent programming style can help some things especially if you are publishing your code. Prevents unusual style Limited code difference between versions on check in to source control Consistent style across large code base eases understanding However, I would say its definitely an expensive thing to have. You have ...


2

There are two major axes when interfacing with another language: the concepts that the interface can carry over: just values? references? generics? how the interface is implemented in "binaries" (called ABI) C has an advantage over C++ on those two fronts: C only has mostly simple concepts, which appear in most every other language1 The ABI of C ...


1

Here's a program that implements a similar code in C. Note that I removed all error checking for brevity: #include <sys/timerfd.h> #include <stdlib.h> #include <sys/epoll.h> static int pending_jobs; static int efd; struct cb_data { int fd; void (*fn)(void); }; static void set_timeout(void (*fn)(void), int secs) { struct ...


1

What is this object representing? What can this object do? What do others need to be able to do to it? What do I expose, what do I hide? Etc When you create a struct, the only thing you need to answer is what its data structure should be. It can't do anything, it can't hide anything, all it is is a convenient method of accessing plain memory. It may be ...


1

No. Strings are always null-terminated by definition, the string length is redundant. Non-null-terminated character data should never be called a "string". Processing it (and throwing lengths around) should usually be encapsulated within a library, and not part of the API. Requiring the length as a parameter just to avoid single strlen() calls is likely ...


1

TL;DR Functional programming is about closures and their applications. Unless someone is able to show you a descent closure library for C, forget about using C to learn functional programming. What is functional programming? The cardinal concept of functional programming is the notion of closures which roughly speaking, captures a function together with ...


1

When asking "why," it is important to ask two sub-questions. What does this gain us, and what does this lose us? What does this gain us? Nothing, actually. I would guess you want to cut down on copy paste. That's an admirable goal, but the idiomatic C# way to write this is actually: var drbObj = (DerivedClass)obj; What does this lose us? ...



Only top voted, non community-wiki answers of a minimum length are eligible