Tag Info

New answers tagged

1

void internal_merge(int* src, int *dst, int start, int end) Use 2 arrays alternatively. For example, in the case of (4, 3, 2, 1) A : (4, 3, 2, 1) Copy and divide A to B. A : (4, 3, 2, 1) B : (4, 3), (2, 1) Divide B to A. A : (4), (3), (2), (1) B : (4, 3), (2, 1) Merge A to B. A : (4), (3), (2), (1) B : (3, 4), (1, 2) Merge B to A. A ...


0

Signals in Unix when some other process sends your process Signal, your program will stop what it’s doing... 1) Run the handler code you wrote. You have no idea what your program might be doing when the signal arrives. That’s the idea with signals, they can be completely asynchronous. 2) When the signal handler is done, it typically just does a return, ...


8

The difference is mostly explained by the difference of ABI : the paper explains what happens in x86 (32 bit); you are using x86_64. In x86n the x86 ABI is used The parameters are passed on the stack by the caller. This means that the 3 values on the top will be a, b and c. The call instruction pushes the IP (the ret field). Usually, the caller frame ...


0

Could be one of many things but two spring to mind. Timing. Maybe the wait is not long enough for power to stabilize and not everything is synced to the clock. The NOP gets everything back in sync. Alignment. Perhaps the NOP gets your instructions aligned on a 32 or 64 bit boundary expected by the hardware. (we used to do this a lot on mainframe ...


3

What you need for that is called operator overloading, which is not supported in C. See the question here: http://stackoverflow.com/questions/3417413/operator-overloading-in-c You could write it in C++ though ...


0

I would recommend the use of Autorevision. You can get output in a variety formats, for example a c style header. There are also a few examples (in the contribs dir) of how you can hook things up so that no matter who is building and how they are doing it they will always get the same version information, even if they are building from a tarball. Also, ...


3

English (and I presume other languages as well) has a lot of words which can be used in a generic sense, but are no longer correct in a context which requires more distinction. For example, you can use the word "goose" to refer to a goose of any gender or age when speaking in the more common generic sense, but if a distinction must be made, a male goose is ...


0

First, let's recap: "parameter = the variable's name" "argument = the expression we pass to the function" Note too that "argument" isn't the variable's name. Therefore, it is everything else. Figuratively speaking, you can think of "argument" as a pipe/line. A line that connects the text in the source code holding the "actual argument" with the text in ...


1

The term "argument" is often used rather loosely to refer to either actual arguments or formal arguments without giving the actual or formal adjective. Most often, the context makes it clear if actual arguments or formal arguments/parameters is meant. In this case, it should be clear that the authors meant formal arguments or parameters. As to why they ...


1

From what you wrote, I don't see a reason why original information about the hashtable and the buckets has to be used - it just overcomplicates things. So flatten all buckets into a list (complexity O(N)) and apply an arbitrary sorting algorithm with complexity O(N * log(N)), for example, quick sort or merge sort. Then write the results into a file.


2

I believe the second half of the mergesort algorithm is what you're looking for, since the first half is splitting the numbers into subranges, and the second half is merging those subranges. Mergesort as a whole has a complexity of O(n log n), and I believe the second half by itself would also be O(n log n). To get you started, here's a reasonably short and ...


1

You can make a min-heap where you store all buckets sorted by the first element. Then you pop an element from the first bucket and re-heapify. If you are not allowed to change the buckets then keep an index per bucket of which element still has to be popped from it.


5

Since you specifically mentioned getchar, putchar and EOF, I am going to explain those. putchar is a function that prints a character to the console. Here is Hello World using putchar: #include <stdio.h> int main() { putchar('h'); putchar('e'); putchar('l'); putchar('l'); putchar('o'); putchar('\n'); putchar('w'); ...


1

C may be selected as a development language for any number of reasons: Ecosystem C is the development language of choice on some operating systems - notably Unix platforms and their variants. Low level integration If you need to inline assembler for whatever reason, C would be a natural choice. Cross-platform C can be used to write platform independent ...


3

C nowadays is used primarily in situations in which a fast compiled language is required that is not overly complicated (classes not required). This includes: Hardware drivers Pixel shaders (variant of C, Cg) In general, programs in which speed is critical Knowing C well is paramount for knowing how programming languages work with the operating system, ...


1

The first declaration also allows you write the function differently: int accumulate(int n, int *array) { int sum = 0; while (n-- > 0) sum += *array++; return sum; } so you don't need the variable i. Whatever's idiomatic to the code base should be preferred, followed by whatever's the easiest to understand, followed at some distance by ...


1

You say that there are 3 ways in C and C++, but C++ actually makes a fourth available: template<std::size_t n> void arrayFunction(std::array<int,n> &array) { ...} This has several advantages over the solutions you suggest: The parameter for the size of the array will be automatically determined on use by the compiler, meaning you don't ...


5

In practice, you'll see int accumulate( int n, int *array) most often. It's the most flexible (it can handle arrays of different sizes) and most closely reflects what's happening under the hood. You won't see int accumulate( int (*array)[N] ) as often, since it assumes a specific array size (the size must be specified). If your compiler supports ...


2

In C, when the array notation is used for a function parameter, it is automatically transformed into a pointer declaration, so declaring parameter as int* array and int array[] are equivalent. I tend to use second one because it is more clear that function expects an array as an argument. Declaring function parameter as int (*array)[] is not equivalent to ...


2

Learning C or something similar, such as Pascal, may help you to understand how the compiler, linker, and run-time libraries work together, so you can better understand the low level details of computer systems. This was my experience when comparing the output of a C compiler to assembly code. C will not help much with higher level language ideas, which are ...


3

Learning C will only help you in learning languages which are very much like C. But then, what do you gain from you learning them? The further away from C the language you want to learn is, the less learning C will help you. In particular, C is missing a lot of concepts, paradigms and ideas that are present in more modern languages: first-class procedures, ...


1

The short answer is C needs types because of history / representing the hardware. History: C was developed in the early 1970s and intended as a language for systems programming. Code is ideally fast and makes the best use of the capabilities of the hardware. Inferring types at compile time would have been possible, but the already slow compile times would ...


8

Simple objects are just a struct containing all the fields. Methods typically take a pointer to that struct as their first argument (like python does). This simple form of OOP is done all the time in C. Inheritance makes things more complicated. You can add a pointer to the parent object from the child object, or include all the parent's fields. Virtual ...


0

As TommyA pointed out, it is safe, at least under normal circumstances. But caution is necessary if you deal with unchecked assignments of bool values (from input), for example when copying memory portions (naive IPC approaches using structs), I expected this to be possibly a problem. So I created this simple test case, #include <iostream> #include ...


5

According to the C++ standard, §4.5 ad. 6 (On integral promotions): A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one. According to the C++ standard, §4.7 ad. 4 (On integral conversions): If the destination type is bool, see 4.12. If the source type is bool, the value false is ...


9

Using macros to create code that's syntactically identical to C but semantically different is almost always evil, especially in an unsafe language like C where the fact that something compiles in no way guarantees it actually has defined behavior at run time. If you must use a macro for something, you generally want to make it clear that a macro is being ...


4

What you suggest is possible with more modern languages. C was invented at a time when compiler technology was far too primitive to allow this kind of programmable compilation pipeline. Furthermore, most programmers knew of no use for such a mechanism. Even today it's difficult to construct a compiler that can permit such programming. It's easy to specify ...


1

I see two reasons why a separate initializiation makes sense. First, there is the the "Single Level of Abstraction" principle, which helps to make code more readable and more evolvable. This means, if type_init and type_free are a pair of associated functions, it makes perfectly sense to call them at the same level whenever possible. The second reason is ...


0

Yes, it makes sense to have a separate init function. The first case is great: define a variable and initialize in 1 step. Easy to use paradigm. In the 2nd case, if used, the code should be tolerant of initialization with 0 and/or NULL and acceptable to a subsequent type_free(). Rational: that what users will do as there is so much blanketed ...



Top 50 recent answers are included