Hot answers tagged

38

In C, void is used for multiple unrelated things. Depending on what it's used for, its meaning may be a unit type, an empty type, or something else. When void is used by itself (as opposed to void*, a pointer to void), it's a unit type, i.e. a type with a single value. Functions that return void are said to “return nothing”, but what this really means is ...


35

I'm not sure but I think the answer is no, for rather subtle reasons. I asked on Theoretical Computer Science a few years ago and didn't get an answer that goes beyond what I'll present here. In most programming languages, you can simulate a Turing machine by: simulating the finite automaton with a program that uses a finite amount of memory; simulating ...


27

The array indexing operation a[i] gains its meaning from the following features of C The syntax a[i] is equivalent to *(a + i). Thus it is valid to say 5[a] to get at the 5th element of a. Pointer-arithmetic says that given a pointer p and an integer i, p + i the pointer p advanced by i * sizeof(*p) bytes The name of an array a very quickly devolves to a ...


14

Arrays are simply laid out as contiguous chunks of memory. An array access such as a[i] is converted to an access to memory location addressOf(a)+i. This the code a[-1] is perfectly understandable, it simply refers to the address one before the start of the array. This may seem crazy, but there are many reasons why this is allowed: it is expensive to check ...


13

The C language has typing rules. For example, you can't divide two pointers, and when you call a procedure accepting a pointer, you can't use a double. A C compiler analyzes its source in several phases: first there is lexical analysis, then the source is parsed, and so on. These phases are abstraction, and in fact additional information is passed from phase ...


11

The name “empty type” is perhaps confusing. What this means is, as you say yourself, the type contains no values. The “empty” refers not to any individual values of the type, it refers to the type as a whole, considered as a ~set of possible values. So this does not say something like “a function returning void returns no information”, but “there exists no ...


7

Since you asked on a computer science site, I'll give you a computer science answer. This might not be the most directly helpful from a programmer's point of view, though understanding this will definitely make you a better programmer. The first argument to the printf function is a char *, i.e. a pointer to a byte¹. However, not all pointers to bytes are ...


6

C99's addition of va_copy to the variadic argument API may give us a back door to Turing-completeness. Since it becomes possible to iterate through a variadic arguments list more than once in a function other than the one that originally received the arguments, va_args can be used to implement a pointerless pointer. Of course, a real implementation of the ...


4

It's not equivalent, and I suspect there will be a loss of statistical randomization/mixing. The core step that offers mixing of the bits is multiplication by FNV_PRIME modulo $2^{32}$. The original version (operating on bytes) does this once for each byte of the input. The alternative version (operating on integers) does this once for each integer, i.e., ...


4

As the other answers explain, this is undefined behaviour in C. Consider that C was defined (and is mostly used) as a "high level assembler". C's users value it for its uncompromising speed, and checking stuff at runtime is (mostly) out of the question for the sake of sheer performance. Some C constructs that look nonsensical for people comming from other ...


3

You don't need books. You need to write lots of programs. Since you are interested in cryptography, you should practice writing programs that involve mathematics. A good way to do so is to join Project Euler and just go down the list. Good luck!


3

A standard for a programming language is a document defining the syntax and semantics for that language. Usually, for real-world languages, this document involves a (hopefully) precise description in intuitive terms, rather than a formal semantics, written in mathematical terms. Still, this document acts as a contract between the programmer and the language ...


3

One can use such a feature to write memory allocation methods that access memory directly. One such use is to check the previous memory block using a negative array index to determine if the two blocks can be merged. I've used this feature when I develop a non-volatile memory manager.


3

The C language standard has provisions for hosted and freestanding runtime environments and makes clear which language features are required for each. Hosted environments are required to provide the standard C library, freestanding environments are not. Operating system kernels and embedded systems are typically built for freestanding environments and so ...


3

The kernel of the system doesn't use the C library (at least not it's I/O facilities). The kernel does offer system calls like read(3) and write(3) in Unix, and translates them into whatever magic the underlying hardware requires to do this. For example, as a very rough description of how this goes, to write to a serial port, the kernel copies the ...


3

Nonstandard arithmetic, maybe? So, it seems that the issue is the finite size of sizeof(t). However, I think I know a work around. As far as I know, C does not require an implementation to use the standard integers for its integer type. Therefore, we could use a non-standard model of arithmetic. Then, we would set sizeof(t) to some nonstandard number, and ...


3

To declare a class (or a function) a friend allows the friend to access private members [the naming in C++ is not my fault!] (data or function) directly. They aren't "inherited" in any meaningful way. Yes, it creates a interdependency, but very different and a lot weaker than inheritance does.


3

Your first answer is good (assuming the scope to assign is in order as declarations go). Each declaration is in the scope, where it is declared. In inner scope (say B2) variable $b$ is declared shadowing previous declaration. Now you cannot use outer scope variable (read, write) as is by name without scope resolution operator. Your second table suggests ...


2

As noted by Tom van der Zanden in a comment, and given you already know what's wrong with the code, think of an input that will cause the program to fail, just run the program with that input and you have your instance. Does it make more sense if you think of the phrase, "for instance"? You may be overthinking it (a good sign actually), if this is just a ...


2

C is not strongly typed. A standard C compiler wouldn't check array bounds. The other thing is that an array in C is nothing but a contiguous block of memory and indexing starts at 0 so an index of -1 is the location of whatever bit-pattern is before a[0]. Other languages exploit negative indices in a nice way. In Python, a[-1] will return the last element,...


2

Cryptopals challenges I would really suggest going over the practical exercises on http://cryptopals.com/ which will (hopefully) illustrate practical known weaknesses of existing cryptosystems and ways to attack them.


2

I always read *y =1 as: assign the value $1$ to the memory location pointed by y. Why? y is a pointer type variable, so it has an address as a value. It another words, it points to this address. When we want to, indirectly, manipulate the memory area pointed by y we use *y. Hence *y=1 tells that the value $1$ should be stored in the memory location ...


2

In other words, you are given a directed graph, and you want to know: Whether there are any directed cycles, i.e., whether your graph is a DAG. Assuming there are no directed cycles, whether the corresponding partial order is a linear order. As KWillets comments, this is just topological ordering. In your case, to find whether your digraph is a DAG ...


2

Usually, everything is compiled in advance: the kernel and userland applications. Roughly, the kernel is written in a special place on the disk (or other permanent memory), which the hardware will access at boot time, copy in RAM, and execute. The kernel includes the disk driver and the filesystem driver, and will look for specific files on the disk, using ...


2

MacOS does a very, very small portion of this: During boot time, it determines which exact processor model is used by your computer, and then for a very small number of important functions, processor dependent code is written to a reserved memory location. This is code for the memcpy and memset functions (where you really want the fastest possible ...


2

Let me define $$f(x) = \begin{cases} 0 & x \ne 0 \\ 1 & x = 0\end{cases}$$ Now it is $$ \sum _{i=0}^{{N\over2}+1}\:f(N \text { mod } 2^i)$$ The code calculates how many times number is divisible by two (which by the way could be simplified to checking number of trailing zeros in binary representation, say hacky method x & -x). You can define {N,...


2

Casting of primitive datatypes works in a very simple way: Under the hood, values don't have types. Casting does absolutely nothing. As a simple example, let us consider casting 65 to an ASCII character, thus obtaining the symbol A. What happens under the hood? Absolutely nothing. The only thing that changes is how the program treats the value. When it ...


1

Standards are simply a list of software guidelines that a project will need to conform to. These standards can be a mixture of requirements. There could be special format and documentation of the code needed. You many need to program the flow of routines in a certain way. How you name functions, files, and variables may need to fit into certain schemes. ...


1

In the Row-major order the elements of rows are continous, so you have already described the Column-major order. X-major order means that n-dimensional array when stored in the memory is flat for easy, sequential access if the X entries. To find address of any element of the Column-major order: the first is plane, than row and the last is column.


1

For the first question, you can calculate an address for it -- languages that don't do array bounds checking will gladly calculate an address and read/write there, even if it is out of bounds. Of course, if the program is accessing an out-of-bounds element, that's typically a bug of some sort. For your second question, you are right. You probably won't ...


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