New answers tagged

3

The existing rendering capabilities of the hardware may not always suit your needs, so sometimes you just have to paint the pixels yourself. So, your question boils down to "why allow direct VRAM access when you can always paint the pixels in RAM and then have them Blted from RAM to VRAM?" The answer is that back in the nineties we did not have the vast ...


3

Both compile fine, as I'd expect. Both do not compile fine. I get compiler warnings on the first (the one that is wrong) from multiple compilers if I set the compiler warning level to a reasonable value such as -Wall with gcc and clang. As a general rule, it's best to always enable a high level of compiler warnings, and only disable them when you're using ...


3

In your first attempt, "num" is an uninitialized pointer, and fscanf (which is expecting a pointer) is attempting to store an int through that pointer. But "num" is probably pointing into protected memory (probably location 0), since it has not been initialized to point anywhere. In your second attempt, "num" is an int, and memory has been allocated for it ...


5

Is there any advantage here that I am missing? Yes, there is. I, along with a large number of software projects, used to follow the practice outlined in the question. Many projects, including modern ones I work on, now take the opposite approach in a source file: The header file that declares the functions being defined in the source file is the very ...


0

No, your ordering of includes is not bad practice. One advantage of putting the local header file first, especially the file that declares the functions whose definition is in the current file, is that you can ensure that your header files are self-contained (they don't depend on any other header files being included before them). Other than that, the ...


3

Yes. The Windows NT kernel API (which is traditionally accessed by using the functions defined in ntdll.dll) can be accessed directly by use of the int 2e instruction. However this is not a supported way of using the system, and details of the implementation (including function codes) are likely to change between Windows versions. The basic approach is: ...


0

If speed is the issue, and you only need NumPy (not SciPy or Matplotlib), try PyPy. It has a JIT that is very powerful. If you don't need to use Python, try Julia. Julia was designed to be fast — and so it is fast. For example, a typical loop in Julia compiles (via LLVM) to code that is the same (bar bounds checks) as what a C or Fortran compiler would ...


1

... perhaps there would be issues that would take time to resolve Most certainly there would be issues. Python is a general purpose language. Matlab is specialized. This keeps you out of the weeds. Sure you can't wander off anywhere you want but that is what keeps things moving. There may be many Python libraries that approximate what matlab does to ...


4

Professional programmer's answer: Anything you can do easily, you can do in a hard way. Sometimes in a very hard way that only masochists would consider. Anything you can do with an excavator you can do with a shovel. Anything you can do with a shovel you can do with a teaspoon. Yes, everything you can do with pointers you can do without pointers. Thanks ...


3

Depends on what you mean by doing. Strictly speaking, any computer algorithm can be done with a turing machine or for the matter with something that simulates it. And it's not like a turing machine needs pointers, or variables, or even code for the matter. So yes it can be done, but it would be come with things like slower execution and clarity problems.


4

You are seeing connections that don't exist. "Write an assembler" is a programming task just like any other programming task. You use the tools to handle that task that are best for that task. There is nothing special about writing an assembler; there is no reason at all not to write it in a high level language. C is actually at a quite low level, and I ...


1

One thing has absolutely nothing to do with the other. Are web browsers strictly to be written using html or php or some other web content language? No, why would they? Can cars only be driven by other cars and not by humans? Converting one blob of bits (some ascii) to another blob of bits (some machine code) is just a programming task, the programming ...


-2

I am not a C programmer but what I am a professional php, javascript and java programmer/developer. In programing there are a lot of possible ways of designing your program and solving any problem. For example sometime you can use array instead of using lot of variables or functions. Now even though you can solve the same problem using lot of variables, ...


3

I don't think you could use the heap (via malloc, or even stack allocation via alloca) without pointers in C as these functions return pointers; so, you'd give up memory allocation. Without memory allocation, you're crippled; might as well write in Fortran '66 -- which by the way is all about writing code using compile-time fixed-size arrays, what fun! (...


4

You can use a huge byte-array and replace pointers by array indices. Apart from interacting with external code/hardware that expects pointers, this is equivalent to using pointer. (Though in C indexing into the array is defined through array to pointer degradation, so it technically still involves a pointer)


1

It's important to remember that the point of the chapter (and the book) was how to deconstruct the problem to find a solution optimized for its constraints, not how to sort arbitrary data. As such, the example using qsort is a throwaway. However, it's very poorly written, even from the perspective of the 1970s; I'm guessing the task was handed to one of ...


1

Addressing specifically this part of the question only: "By the way I'm aware that GNU Assembler and Netwide Assembler have been written in C. I am also wondering why they are written in C?" Speaking as part of the team that originally wrote the Netwide Assembler, the decision seemed so obvious to us at the time that we basically didn't consider any other ...


6

What were they doing in the past while the absence of C language? Were they writing Assembler in Machine Code? Assembly is essentially a mnemonic for machine code; each opcode in the machine language is given an assembly mnemonic i.e. in x86 NOP is 0x90. This makes assembler's rather simple (n.b. most assemblers have two passes, one to translate, and a ...


2

Among the reasons to write an assembler in C (or any other higher level language) are all the reasons you might use to justify writing any other program in that higher level language. Chief among those in this case are probably portability and usability. Portability: If you write your assembler in a native language you have an assembler on that platform. If ...


11

People have written assemblers in machine code. They've also written then in assembly language--often a subset of the language they translate themselves, so they start with a simple "bootstrap" version of the assembler, then add features to it as they need them for the assembler itself. However, none of this is particularly a necessity. In the end, an ...


1

An array with 257 entries will be fast, portable, and again fast. Many compilers provide a very fast built-in function to count leading or trailing zero bits in a number, for example __builtin_clz in gcc and Clang, which is likely about hundred times faster than a call to the log2 function.


3

SCI is not a standard term. Usually it's called a UART, and that's the interface you would use to communicate between the dev board and the PC. SPI is used for communication between chips on a board, supports higher speeds, and has a single master clock. UARTs are used for longer distances, each side asynchronously transmits their own data such that the ...


2

Expanding on my comment. Here's the language from the online C standard: 6.2.5 Types ... 19     The void type comprises an empty set of values; it is an incomplete object type that cannot be completed. ... 6.3.2.2 void 1     The (nonexistent) value of a void expression (an expression that has type void) ...


1

The linux style guide gives specific reasons to use goto's which are in line with your example: https://www.kernel.org/doc/Documentation/CodingStyle The rationale for using gotos is: unconditional statements are easier to understand and follow nesting is reduced errors by not updating individual exit points when making modifications are ...


4

(just a guess) Probably that every code example has been indeed tested on a system with a C compiler, and pretty-printed in the book (perhaps using troff or whatever document formatter was used by K&R) by some automatic tool (I don't know what tool was used by K&R. Today, you'll use e.g. vgrind or pygments or LaTeX's listings package, or some other ...


1

We don't have to write a C Compiler for every new architecture. For example the Lisp Machine and the Java VM can do very well without a C compiler. However, if you really want to, C is portable even to these architectures as they are Turing complete. For the common, C-friendly architectures an often used approach is to start writing and using a C cross-...


4

How C language is portable to any instruction set (I mean for new architecture). It is not, but C is portable to most reasonable instruction sets close to existing ones. As an hypothetical counter-example, you might define a computer architecture using ternary (not binary) or decimal. Both did happen in the past (1960s: IBM/1620 was decimal, Russian ...


2

Yes. The instruction set is defined, machine code, an assembly language syntax is defined along with an assembler. Most likely a linker, and then you are ready for a port of the C compiler. And then you can start with bootloaders, operating systems etc (naturally after design verification or as part of it). On rare occasions does anyone vary from this ...


4

in general, every new architecture needs a new port of the C compiler (along with the rest of the C tool chain) Usually this starts with the development of a 'cross compiler' on a known architecture to compile C for the new architecture. (the compiler, itself, can be what is being compiled, from source, for the new architecture.) even a related new ...


1

(I am guessing you are on some POSIX system, like Linux or MacOSX) You obviously should look into GCCJIT, as mentioned by Ciro Santilli. You'll then build some AST-like representation of the generated code. Of course you might consider LLVM instead, or even some simpler JIT library like libjit or GNU lightning (but libjit and lightning are emitting code ...


2

libgccjit, which was introduced in GCC 5 and is still experimental as of GCC 6 is going in that direction. Docs: https://gcc.gnu.org/onlinedocs/jit/ Related questions: http://stackoverflow.com/questions/8144793/gcc-for-parsing-code http://stackoverflow.com/questions/1735360/compiling-program-within-another-program-using-gcc


3

It's largely a matter of personal preference, although some places may make it a formal coding standard. C itself doesn't care. For my part, if I'm defining multiple functions in a single source file, I will define the called functions before the caller: void foo( void ) { ... } void bar( void ) { ... foo(); ... } int main( void ) { ... ...


1

The best place is somewhere that people can find it. In the middle of a very large file is bad. At the top is good, but others might prefer right at the bottom. Forward declaring main() doesn't help, and is often pointless anyway - because it's very unusual to explicitly call main() from within your code.


1

While I don't think that this sort of pre-processor-level “comment” is evil, I'm also not very excited about it. Your tools won't interpret it in any way and won't be able to catch any bugs caused by accidentally using a function in a way it wasn't intended to. The tag is also invisible at the call-site. Since it is really just a comment, you could equally ...


4

You can (see the Labels as Values gcc extension). It's rather doubtful that you should. Dijkstra taught us that goto was harmful. You are proposing not only using goto, and a jump table, but also a function that returns to the passed label. Understand that a function is essentially a goto (jump) with some pushing onto the stack (the stack frame). That ...


7

In standard C99 or C11 you cannot. But GCC has a language extension, label as values, which might help (and Clang/LLVM also accepts it). Even with the computed gotos that extension provides, jumping into another routine is (nearly) undefined behavior. You can jump indirectly to a label inside the same routine. You could return a label to jump into; that is,...


3

It is not possible, and if it were, it would probably be considered bad practice1. Gotos within a function make code impossible to trace by inspection; gotos across functions would make the situation much worse. Here's an example: i = 4; label: printf( "%d\n", i ); What value actually gets printed? Is it 4? Is it ever 4? I can't know until ...



Top 50 recent answers are included