Tag Info

Hot answers tagged

37

I understand the book is written to describe C languages in general. The book is written to teach you ANSI C, not 'C languages in general' Does the book expect me to be able to translate the first program ... to a C# or C++ program? No, those are different languages, and the book does not expect you to do anything with those languages. The ...


36

As said in the other answer, most compilers will automatically optimize multiplications to be done with bitshifts. This is a very general rule when optimizing: Most 'optimizations' will actually misguide the compile about what you really mean, and might even lessen the performance. Only optimize when you have noticed a performance problem and measured ...


25

There are two 'forces' here, in tension: Performance vs. Readability. Let's tackle the third problem first though, long lines: System.out.println("Good morning everyone. I am here today to present you with a very, very lengthy sentence in order to prove a point about how it looks strange amongst other code."); The best way to implement this and keep ...


20

The compiler recognizes constants and converts multiplies to shifts where appropriate.


14

You are right that code such as float a = malloc(size); a--; yields undefined behavior, per the ANSI C standard, section 3.3.6: Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined For code like this, the quality of the C code in the ...


14

"Static" is overloaded to mean two things in C: At the file level, it restricts scope to the current file. (I think this is what you're seeing.) Within a function, it transforms variables such that their values are preserved between calls. "Static" has little to do with performance in C. In the context in which you saw it used, it relates to ...


12

Whether shifting is faster than multiplication depends on the architecture of your CPU. Back in the days of the Pentium and earlier, shifting was often faster than multiplication, depending on the number of 1 bits in your multiplicand. For example, if your multiplicand was 320, that's 101000000, two bits. a *= 320; // Slower a = (a<<7) + ...


10

Officially, it's undefined behavior to have a pointer point outside the array (except for one past the end), even if it's never dereferenced. In practice, if your processor has a flat memory model (as opposed to weird ones like x86-16), and if the compiler doesn't give you a runtime error or incorrect optimization if you create an invalid pointer, then the ...


10

The issue I would wager is that C arrays are just pointers to the beginning of an allocated chunk of memory. Having a 0 size would mean that you have a pointer to... nothing? You can't have nothing, so there would have had to be some arbitrary thing chosen. You can't use null, because then your 0 length arrays would look like null pointers. And at that ...


10

Using fscanf() in itself probably explains most of it. fscanf() has to interpret the passed-in format string, and then has to scan the input stream from the file, trying to match the specified pattern. That's actually a huge amount of work. read() just has to read in the specified number of bytes from the file and doesn't have to do any parsing of the input. ...


6

What you see is the effect of the optimiser. The optimisers job is to make the resulting compiled code either smaller, or faster (but rarely both at the same time... but like many things... IT DEPENDS on what the code is). In PRINCIPLE, any call to a multiplication library, or, frequently, even use of a hardware multiplier will be slower than just doing a ...


6

You've got several problems with your test program. First, you're not actually using the value of test. There is no way, within the C standard, that the value of test matters. The optimizer is this completely free to remove it. Once its removed it, your loop is actually empty. The only visible effect would be to set runs = 100000000, but runs also isn't ...


6

If you don't provide a way to dispose the object, you are passing a clear message that they have "infinite" lifetime once created. If this makes sense to your application, I say: do it. Glampert is right; there is no need for destructors here. They would just create code bloat and a pitfall for users (using an object after its destructor is called is ...


5

Firstly, let me agree that K&R is a great place to start with the C family. It is a really wonderfully written book. Importantly, it is quite concise. Be aware that C++ is much more closely related to C that C#, although C# does borrow heavily from both. Java in fact does the same, and indeed C# followed from Java. You can't learn C# or C++ just using ...


5

In C, const isn't "implemented". It's a message to the compiler about whether to generate errors. gcc compiles this: const char *cp = "Hello"; to .section __TEXT,__cstring,cstring_literals L_.str: ## @.str .asciz "Hello" .section __DATA,__data .globl _cp ## @cp .align 3 and this: ...


4

As a simple fix you can swap the max element with first and then pop as normal. However I suggest using a max-heap structure. This has the advantage of having only O(log n) time complexity for push and pop and can fit in your fixed size array. Popping a value involves putting the last element in the head and shifting it down. Pushing a value involves ...


4

Every function or variable in a C program which is not declared static is required to have a unique name. If two or more programmers writing different parts of a system each write a function called "fred", and both functions are declared static, then the fact that there are two "fred" functions will be completely irrelevant (and thus not pose a problem). ...


4

Static, when applied to a function, means that the function has local scope or file scope, which will disallow someone from declaring a prototype to that function in a header file and using it somewhere else than where it was declared. This has no performance implications, but is a good practice nevertheless. Why? Because it reduces scope. When the ...


4

Yes, it's the difference between the prefix and postfix increment operator. The prefix operator uses the modified value of the variable in the expression. The postfix uses the original value in the expression. As JohnBode points out, the modification to the variable itself is a side effect. The precise moment that the side effect occurs is not defined ...


4

This is just the way string literals work in C. String literals like "name" are arrays of characters, it is equivalent to the five element array {'n', 'a', 'm', 'e', '\0'}. For the code char *c; c="name"; the environment reserves memory for the above array already at initialization time, when the program is loaded from disk into memory. At run time, the ...


4

Let's look at how an array is typically laid out in memory: +----+ arr[0] : | | +----+ arr[1] : | | +----+ arr[2] : | | +----+ ... +----+ arr[n] : | | +----+ Note that there isn't a separate object named arr that stores the address of the first element; when an array appears in an ...


4

The answer to this kind of question is always the same: if the language doesn't provide a useful abstraction, you must emulate it. (Whether doing this is worth the additional effort depends on the specific circumstances of your program, which you know better than we do, but for the purposes of this answer I'm assuming that it is.) Polymorphism is the ...


4

I think it would be more helpful for the questioner to have a more differentiated answer, because I see several unexamined assumptions in the questions and in some of the answers or comments. The resulting relative runtime of shifting and multiplication has nothing to do with C. When I say C, I do not mean the instance of a specific implementation, such as ...


3

You can't make new functions "built-in" to C without changing the compiler. However, you can write and extend libraries with new functions that your programs can then reuse wherever they want. For that, you need to compile a library with the functions, your program needs to include the library header file, and the linker has to include the object code for ...


3

First, it's undefined behaviour. Some optimising compilers nowadays get very aggressive about undefined behaviour. For example, since a-- in this case is undefined behaviour, the compiler could decide to save an instruction and a processor cycle and not decrement a. Which is officially correct and legal. Ignoring that, you might subtract 1, or 2, or 1980. ...


3

Try timing it with: for (runs = 0; runs < 100000000; runs++) { ; } The compiler should be recognizing that the value of test is unchanged after each iteration of the loop, and the final value of test is unused, and eliminating the loop entirely.


2

The answer to your question is here: http://stackoverflow.com/questions/4958384/what-is-the-format-of-the-x86-64-va-list-structure The va_list type is an array containing a single element of one structure containing the necessary information to implement the va_arg macro. The C definition of va_list type is given in figure below typedef struct { ...


2

Embedded software is very different. On a desktop app, abstractions and libraries save you a lot of development time. You have the luxury of throwing another couple megabytes or gigabytes of RAM or some 2+GHz 64-bit CPU cores at a problem, and someone else (users) is paying for that hardware. You may not know what systems the app will run on. In an ...


2

When applied to a function definition, the static keyword prevents the function name from being exported to the linker; code in other translation units (files) cannot call that function by name. To abuse some terminology, you're making the function definition "private" to that source file. For example, assume I have a file like static int ...


2

The problem with using an array (circular or not) as underlying data structure for a priority queue is that you must always copy elements around to create a hole where a new element should be inserted or to fill the hole created by removing an element. In a very naive implementation, using your circular array, you could use the following scheme: When ...



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