Tag Info

New answers tagged

2

I'd use a tool to document the code. E.g. with doxygen you can write: /** * \brief Performs something and stores result in \a c and \a d * * \param[in] a ... * \param[in] b ... * \param[out] c ... * \param[out] d ... * * Detailed description here. */ void my_function(int a, int b, int *c, int *d) { /* do something */ } The special ...


3

You could note the flow of information in the function definition: void my_function( /* in */ int a, /* in */ int b, /* out */ int* c, /* out */ int* d) { /* do something */ } This way, it's easy to see what happens. If you export this function, you will also include the comments in the prototype so that everybody could see the ...


0

You appear to be asking for build automation software, of which there are many options available. In particular, most continuous integration tools should have no trouble doing this sort of thing. For example, at my job we have a Jenkins setup that automatically builds our software every time there's a new commit to master, does so on all platforms that we ...


1

I don't know about industry standards, but I have used state-machines in multiple layers of a communication protocol stack (sp. the PS/2 protocol) for the same reasons you gave; it worked pretty well: it allowed me to coordinate and control everything that was going on, esp. the interrupt-driven stuff. Even though it went ok for me, I think you should pay ...


0

A child calling  exec is not terminated, it is replaced by a new program (running in the same process). And the execve(2) syscall may fail for several reasons (so you always should handle such a failure). Then execution continues after the exec ! Read e.g. Advanced Linux Programming for details. It has at least a whole chapter about these issues.


4

Apart from machine code, there is no programming language in existence which executes directly on hardware, in the sense that you can't feed it the literal source text. All real implementations must translate the source program into the language of the "machine". For some implementations, it's translated statically. We usually call these implementations ...


11

In Java the virtual machine executes your code, but C compilers generate code that the real machine executes. To be precise, in both cases your program ends up being converted into real machine code, but in the case of Java there's a middle step of compiling to JVM bytecode. So Java programs are converted to real instructions by the JVM when you load them, ...


2

You say you have little real-world experience in this area. Learn from experts to become one. Look around at what professionals of this field are doing to solve your problem. Open source PMBus implementations are out there. The Linux kernel is an example. But Freescale also has a PMBus library out there that is likely better for your background. ...


0

In standard C11, you cannot make GUI code, because standard C11 does not know about graphical user interfaces, or screens (you only have  stdout) or keyboards, or mouses. Any GUI code would use some non-standard thing. A common way to provide such non-standard things is by providing an API thru some library. Details are of course horribly platform and ...


0

GUIs are generally a platform-specific affair. On Windows, the creation of windows and the widgets within them are managed by the Windows GDI library. On Unix-like systems running the X Window System, that is done through xlib by creating windows within windows, and drawing on them. Libraries like GTK+, Qt, wxWidgets, etc., exist to abstract away these ...


4

Sure it is. Just put all the code that was used to write the libraries into your own program, and you can do anything they can do. In other words, having libraries is a convenience. Smaller source files are easier to work with, you don't have to know the intricacies of X (or if graphics drivers if you also want to go without X), reusing widget-drawing ...


3

in my opinion, the easiest way of wrapping your head around recursion is starting from the end. In every recursive function, there's a stopping condition. In your case it's else return 0; so the method will return 0 if the argument is zero or lower. That's simple. Now let's go backwards. We know that num(0) returns 0 without doing anything else. So now we ...


4

To solve this type of thing, it's often best to take a pen and paper and "pretend to be the computer running the code". So, if we have a call of num(4), it will lead to: if (4 > 0) // Yupp, 4 > 0 num(3); // x - 1 = 3 if (3 > 0) // Yupp num(2) if (2 > 0) num(1) if (1 > 0) num(0) if (0 > 0) ...


2

One thing that neither thread brings up is this: char whopping_great[8192] = "foo"; vs. char whopping_great[8192]; memcpy(whopping_great, "foo", sizeof("foo")); The former will do something like: memcpy(whopping_great, "foo", sizeof("foo")); memset(&whopping_great[sizeof("foo")], 0, sizeof(whopping_great)-sizeof("foo")); The latter only does the ...


2

I can speak as the main architect and implementor of GCC MELT, a Lispy domain specific language to customize the GCC compiler, translated to C++ code suitable for GCC internals. MELT has some tricks (devices like defprimitive, defciterator definitions) to formalize the generation of part of the generated C++ code. The generated C++ (mostly C-like) code is ...


2

There are some language translators whose intended usage is to take a piece of code and translate it, once, into a form which a human will be able to maintain from then on; for such translators, the human-readability of the translated code is extremely important. More often, however, the intended use of a code translator is to produce a file that will be ...


3

Yes, it can. The C language specification requires conforming compilers to assume that the programmer can do various aliasing things, that are in fact extremely brain-dead in 99% of cases, and generate correct code anyway. This makes things harder for the compiler writer and makes the resulting generated code slower. The FORTRAN language specification ...


2

Stroustrup gives a few hints about performance in his paper Myths of C++ (pdf) including one about performance of C being faster than C++, with code. I suppose you can say "anything you can do in C++ you can do in C therefore it can always be as fast", but that assumes optimisations that are either already done for you, or specialisms that would be ...


4

Imagine you're a random human that has an idea for improving a C compiler. I think the fact that gcc is open source and widely used is pretty much a magnet for you--why start from scratch when you can go there and improve something that already works? A high quality, well-known open source implementation of a difficult project may end up being a sink that ...


2

C/C++ is unique amongst compiled languages in that it has 3 major implementations of a common specification. Going by the rule of dismissing anything that's not used much, every other compiled language has 0 to 1. And I think javascript is the only reason you need to specify 'compiled'.


6

How many compilers do you need? If they have different feature sets, you create a portability problem. If they're commoditised you choose either the "default" (GCC, Clang or VS). If you care about the last 5% performance you have a benchmark-off. If you're doing programming language work recreationally or for research purposes, it's likely to be in a more ...


3

Do you have a specific need for make? Tools like make are all about automation - being able to repeat a process flawlessly, time after time, as efficiently as possible. Check out this thread on github regarding a JavaScript development framework and the potential choice of using make as the build tool: ...


1

Checkout the section on make in The Art of Unix Programming. It explains the motivation behind make and why we have tools that generate Makefiles. To summarize, the creator of make had a time when he wasted time debugging a working program because he forgot to manually recompile an object code. Make is an automated tool that determines what files need to be ...


59

I would like to contest your underlying assumption that there are only a small number of C implementations. I don't even know C, I don't use C, I am not a member of the C community, and yet, even I know far more than the few compilers you mentioned. First and foremost, there is the compiler which probably completely dwarfs both GCC and Clang on the ...


0

Not necessarily ,But it helps you -To ensure you are building only when you have made some changes -To forget the log command to build -Just build , what is required ,instead of building the whole translation unit . -Helps in including library and making code modular .


107

Today, you need a real C compiler to be an optimizing compiler, notably because C is no longer a language close to the hardware, because current processors are incredibly complex (out-of-order, pipelined, superscalar, with complex caches & TLB, hence needing instruction scheduling, etc...). Today's x86 processors are not like i386 processors of the ...


4

So what is your target language? SML compilers are often targeting C or something like LLVM (or as seen in your link, the JVM or JavaScript). If you're compiling C, it's not because you're going to the JVM. You're going to something worse than C. Far worse. And then you get to duplicate that minor hell a bunch of times for all your target platforms. And ...


3

If you use a modern IDE(*) you will probably never have to look at a makefile in your life, even though the IDE generally will generate one for you on the fly. Makefiles were a needed skill when I started out 30+ years ago. I rarely see them nowadays (and, if you do need one, there are makefile generators). In short, don't learn a skill until you need ...


13

First, a Makefile for make is really useful when you build a program from several translation units (i.e. several *.c or *.cc files which are #include-ing some other header files) which are linked together (it is not very useful for a single source file tiny program). It organizes the various compilation steps of the translation units (and avoid running ...


0

My experience is that beyond having a Makefile (or sometimes cmake :-(), it varies according to the personal tastes of the individual. Personally, I use a editor that runs from the command line (xterm), and have editor macros (created over a couple of decades) that more or less imitate many of the features of an IDE without the baggage. For instance, one ...


30

I was asked by a student if & and * were chosen because they were next to each other on the keyboard (something I had never noticed before). Much googling led me to B and BCPL documentation, and this thread. However, I couldn't find much at all. It seemed like there were lots of reasons for * in B, but I couldn't find anything for &. So following ...


1

Valid indices for s are from 0 to lim-1, inclusive, seeing how it is allocated in main as line[MAXLINE]. Also, you have an undeclared (and uninitialized) variable j in getline. I assume it's initialized to 0 because not much else would make sense. When the for loop exits, the maximum value that j can have is lim-2 because we increment it only while ...


13

At least for free software on Linux, you usually use some builder like make. You could use some other builder program, like scons or omake For some (mostly historical) reasons, the Makefile may be generated by utilities like autoconf or cmake; these generators also deal with configuration issues (e.g. they disable some features of the software if a ...


2

Some languages such as Java have “static methods” that live in a class's namespace, but do not use dynamic dispatch. Instead, they are dispatched statically at compile time (hence their name). Dynamic dispatch is less efficient than static dispatch, since we have to search for the method in all classes of the object we're calling a method on. A central point ...


-2

I think your understanding of the code is correct (or at least it maches mine). Print a character at a time, unless the last character was white space. But I think both versions suffer from not being clearly stated. I would prefer the below, which is shorter and more explicit. #include <stdio.h> main() { int c, lastC; while ((c = getchar()) != ...


0

You should keep an eye on two aspects here: The first one is how components you cannot change are already arranged. Third party components or some legacy components you don't want to change anymore Components you or your company wrote, which can be merged or split One of my Best Practices when deciding how to divide any code in packages is to let the ...


2

At our company, the dividing line between approach one and approach two comes down to the external dependencies of the component(s). For instance, we have hundreds of "core" components with no dependencies whatsoever, and those are grouped into only a single library. On the other hand, the one component that we use to talk to a specific type of database gets ...


0

There are also some other libraries used for data exchange, e.g. protobuf or MessagePack. These libraries can better fit your needs - depends on data you plan to exchange


1

Parsing chunks from a stream is usually an issue. One of the issues is that you often need to know the size of the packet to allocate the buffer needed to hold it while parsing. Most text based formats don't have that instead they are delimited by a special set of characters (JSON it's the closing brace/bracket). If you elect for a binary transmission I ...


0

Don't see why not, its juts another format you have to handle, and there are thousands of them in the wild already. At least JSON is a well-known format. A quick google says libjso, cJSON, and NxJSON are available as libraries to help parse and manage your data payloads so there shouldn't be much issue with the complexity of parsing it, no ore so than XML ...


1

The best back end webserver language for an embedded C program is... C! There are plenty of embedded C webservers that are trivially easy to use, I added civetweb to my service a while ago and it was too easy. Civetweb is a non-GPL fork of Mongoose, but there are others such as NxWeb. Civetweb has some examples, including ones that include Lua support ...


4

The methods I've seen most are 2 and 3. The user supplied buffer is actually quite simple to use: char[128] buffer; mytype_to_string(mt, buffer, 128); Though most implementations will return the amount of buffer used. Option 2 will be slower and is dangerous when using dynamically linked libraries where they may use different runtimes (and different ...


3

It also depends on the architecture. In ARM the first few locals are stored in registers only. As long as you do not call any other functions then these will never go on to the stack. You need to go the opposite direction. Rather than using globals you should start by limiting the scope if the variables as much as possible. Not only is this good coding ...


-2

The choice of what variable to use between float and double depends on the accuracy of the data required. If an answer is required to have negligible difference from the actual answer, number of decimal places required will be many thus will dictate that double to be in use.Float will chop off some decimal places part thus reducing the accuracy.


15

Allocating a variable on the stack and deallocating it is a simple addition and subtraction of the stack pointer. Given that it happens anyway when entering a function means that local variables are so cheap that trying to optimize them to anything else will generally incur more cost. Putting it in the data segment will incur a cache cost, the stack will ...


8

Yes Yes It doesn't have to be dynamically linked. In detail: When creating a library (doesn't have to be in C, but I assume this means it needs to expose its functionality in the form of exported C functions) then the source code is turned into the equivalent machine instructions. While a skilled hacker could turn this machine code back into a higher ...



Top 50 recent answers are included