Tag Info

New answers tagged

1

Do it both ways. Make one new Java class to match as closely to the original C as you can, using static variables in the same way the C code does. In most cases do a direct one-to-one translate of each line of code. Even use the same loop types, parameter names, method names and variable names and even keep the gotos. Test that code to the maximum ...


0

Consider a language L0 that does no implicit conversions like double to int, "default argument promotions", array conversions to "address of first object", etc. Code like foo(int); double x; foo(x); is then a compile time error. double x; foo(x); // Bad L0 code L0 is not relevant to this question other than it shows if L0 code wanted to call foo(int) ...


3

Like Basile said, 4K lines is a small program. It should be relatively straightforward for you to puzzle out what it does and how it does it.1 You said this was a simulator of some sort; I'm guessing it acts as a server or client to some other process for testing purposes. We have similar tools for testing our software - the simulator stands in for a ...


1

Naturally, the correct procedure would be to do it the right way, Java is an OO language after all, but whether you should mimic the current coding style or do it the right way an OO programmer would is not up to you, but up to your lead. Therefore discuss with your manager first, if they want to keep the structure as is, or make the code more agile ...


1

I see no reason to port the code from a procedural language to an object oriented language without making the code object oriented. Do you just wish to be able to write procedural code in an object oriented language? Object oriented code is a step forward. Procedural code in an object oriented language is for the least not forward if not exclusively ...


1

It seems you want to achieve two things: Create a Java program instead of a C program, and have an improved structure. Both represent work. However, it is less work to convert a C program with a good structure to Java than a badly structured C program. And improving the structure is easier with a known working application, so you can make one improvement, ...


11

A 4Kline long C program is a small C program. It generally needs a few weeks or months to be written by a single person. If you are familiar with the domain of the program, you should be able to understand it entirely quite quickly, and write a tiny documentation describing the design and purpose of the original program (in particular, the communication ...


1

Notice that LISP languages (e.g. Common Lisp, Scheme, Clojure and many specific dialects inspired by them like AutoLISP, Emacs-LISP, MELT, etc...) are all using a prefix-syntax: every expression starts with a left parenthesis, then the operator, then the operands, then the right parenthesis. These expressions are called S-expressions. For example 1+2*3 is ...


4

Both pre- and postfix have basically the same advantages over infix notation. The most important of these are: much easier to translate to a format that is suitable for direct execution. Either format can trivially be turned into a tree for further processing, and postfix can be directly translated to code if you use a stack-based processor or virtual ...


0

In C++ you have function overloading, that is given a series of functions with the same name but different input parameters (the return value is not considered) the compiler must look for the better pick, taking into account implicit conversions and namespace resolution. So functionally the compiler finds a function call with a given input, and then looks ...


0

Conversion is always done by the calling code, not in the called function (in particular because types get erased at runtime: the implementation does not know about types at runtime, only at compile-time). A C function which is declared (at least those which are not variadic, e.g. without ... ending there argument type list, like printf) has a well defined ...


1

I expect in every implementation of C, the conversion happens first. The psuedo-assembly for bar: store 2 in argument-register-0 call foo store 3.1 in temporary-register-0 convert-to-int temporary-register-0 into temporary-register-1 store temporary-register-1 in argument-register-0 call foo Your optimizer is probably smart enough to eliminate ...


9

There seems to be a confusion on both sides here: using a handle approach does not requiring using a single handle type for all handles exposing the struct name does not expose its details (only its existence) There are advantages to using handles rather than bare pointers, in a language like C, because handing over the pointer allows direct ...


1

I believe that the attitude stems from a long-time philosophy to defend a C library API from abuse by beginners. In particular, Library authors know it's a pointer to the struct, and the struct's details is visible to library code. All experienced programmers who use the library also knows it's a pointer to some opaque structs; They had enough hard ...


1

I suspect the real reason is inertia, that's what they've always done and it works so why change it? The main reason I can see is that the opaque handle lets the designer put anything at all behind it, not just a struct. If the API returns and accepts multiple opaque types they all look the same to the caller and there's never any compilation problems or ...


24

The "simple is better" mantra has become too much dogmatic. Simple is not always better if it complicates other things. Assembly is simple - each command is much simpler than higher-level languages commands - and yet Assembly programs are more complex than higher-level languages that do the same thing. In your case, the uniform handle type enh makes the ...


1

The most obvious benefit of the handles approach is that you can modify the internal structures without breaking the external API. Granted, you still have to modify the client software, but at least you're not changing the interface. The other thing it does is provide the ability to choose from many different possible types at runtime, without having to ...


10

Historically (perhaps by rewriting parts of it), it was the contrary. On the very first computers of the early 1970s (perhaps PDP-11) running a prototypical embryonic C (perhaps BCPL) there was no MMU and no memory protection (which existed on most older IBM/360 mainframes). So every byte of memory (including those handling literal strings or machine code) ...


2

Global state, including extern variables and non-const static variables in file scope or in functions can frequently be an easy solution to a given problem, but there are three issues: static makes code untestable, because static variables tend to be non-replaceable dependencies. Or in more OOP-y words: you aren't following the Dependency Inversion ...


5

In a well-design C program, a file-static variable is similar to a private static member of a class: It can only be accessed by functions in that file, similar to how a private static member variable can only be accessed by functions in the class in which it is defined. There is only one copy of the variable. Its lifetime is the program lifetime. An ...


0

I do not consider variables with file-scope as bad as global variables. After all, all accesses to these variables are confined to one single source file. With that restriction, file-scope variables are pretty much as good or bad as a C++ private static data member, and you don't forbid their use, do you?


3

There are existing protocols for performing discovery of devices; it would be better to use one of these than roll your own. The leading competitors are mDNS (aka Bonjour) and SSDP (aka Universal Plug and Play). I'd suggest reading up on these, and then picking up one of the many open source implementations to start working from.


1

Java References and C pointers differ in exactly two points: There's no pointer-arithmetic for the former. And you cannot create a Java reference to whatever you want, you can only copy those saved somewhere accessible (static fields, fields of objects, local variables) or returned by function-invocations (like constructor-calls), which thus all refer to ...


0

They are slightly different. In Java a copy of the reference is copied to the stack of a called function, pointing to the same object as the calling function and allowing you to manipulate that object. However you cannot change the object the calling function refers to. Consider the following java code public static void changeRValue(StringBuffer sb){ ...


4

A function that receives a pointer does not know of the length of the corresponding array. You must pass in as a parameter yourself explicitly: void myFunc(int *yourArray, size_t yourArrayLen) Once you've done that, throwing an error is trivial. Of course, this still leaves the possibility that your caller might give you the wrong length. You can't ...


4

The answer is really fairly simple: if you want safety, use something that actually provides it--and that's not C, and not raw C-style arrays. Without departing too far from the basic style of C and raw arrays, you can use C++ and an std::vector with [i] replaced by .at(i), and get bounds checking. Using std::vector instead makes most of the problems with ...


0

Use a custom wrapper for malloc (or write your own) that keeps additional information about the blocks it allocates. The one I use adds a few "guard bytes" to every allocation, embeds the length of the allocation as the a[-1], and checks the guard bytes and other things upon deallocation.


1

There is no way in C(++) to get the length of an array from a pointer to its first element. (There are platform-specific functions like _msize in MSVCRT, but that only works on malloced pointers.) What's typically done when passing arrays to functions is to pass the length along with the pointer so that bounds-checking can be done at runtime. void ...


7

Checking array bounds like you want is implementation specific, because buffer overflow is an example of undefined behavior (and this explains why UB can be really bad). It is also an undecidable problem in general. You can easily show that statically finding (by static program analysis, e.g. of the C++ source code, without actually running the program) ...


2

I would suggest to avoid using this as a formal name in C, because this is a C++ keyword. You could use e.g. self (or me or recv, or ipse -it is self in Latin) instead of this If you code your C code without using C++ keywords, you are much more likely to be able to use your code with a C++ compiler. In particular, quite often a C header file foo.h (with ...


7

If they are supposed to be singletons, and you really want to do things in your (conceptual) style, you might want to use Context object. (First and foremost: apologies. C is not my native language; C++ is.) Basically, struct Context { Engine * engine; Window * window; // any other things that are considered singletons // according to ...


2

if it is the same, it should look the same This is not how naming works. Your are the same however people call you. Granted, you have a name on a birth certificate, but that is not sufficient. You are John Doe, yes, but the one who lives at such and such place, who was born that year. Someone might call you son, and someone else dad, whereas I call you ...


3

Most processors provide different instructions for working with data of different types, so type information is usually "baked in" to the generated machine code. There's no need to store additional type metadata. Some concrete examples might help. The machine code below was generated using gcc 4.1.2 on an x86_64 system running SuSE Linux Enterprise ...


0

Short answer, the type is encoded in the CPU instructions the compiler generates. Although the information about the type or size of the information is not directly stored, the compiler does keep track of this information when accessing, modifying and storing values in these variables. how does the execution know that a is a char and x is an int? It ...


1

You have to distinguish between compiletime and runtime on the one hand and code and data on the other hand. From a machine perspective it is no difference between what you call code or instructions and what you call data. It all comes down to numbers. But some sequences - what we would call code - do something what we find usefull, others would simply ...


-4

The other answers are correct in that essentially every consumer device you will encounter doesn't store type information. However, there have been several hardware designs in the past (and the present day, in a research context) that use a tagged architecture -- they store both the data and the type (and possibly other information as well). These would most ...


5

Historically, C regarded memory as consisting of a number of groups of numbered slots of type unsigned char (also called "byte", though it need not always be 8 bits). Any code which used anything stored in memory would need to know which slot or slots the information was stored in, and know what should be done with the information there [e.g. "interpret the ...


1

My answer here is somewhat simplified and will refer only to C. No, type information does not get stored in the program. int or char are not type indicators to the CPU; only to the compiler. The exe created by the compiler will have instructions to manipulate ints if the variable was declared as an int. Likewise, if the variable was declared as a char, ...


0

Concerning this: &B->data[B->start] you assert in comments that: If I read the tokens from left to right, the operators are ->, [], -> But those are not the operators reading left to right: you're including the closing ] with the opening [, even though the -> comes between them. That doesn't make any sense. When you see [expression], ...


0

At the lowest level, in the actual physical CPU there are no types at all (ignoring the floating point units). Just patterns of bits. A computer works by manipulating patterns of bits, very, very fast. That's all the CPU ever does, all it ever can do. There's no such thing as an int, or a char. x = 4 + 5 Will execute as: Load 00000100 into register 1 ...


1

You shouldn't need to check for data to be received from a socket. You want to ask the OS to tell you when data is received. With poll (or select), you can pass it a bunch of sockets at once, and a large (or infinite) timeout. poll will return some data's been received on any of the sockets you passed it. If nothing is received, it will keep waiting without ...


1

Actually, it doesn't matter whether B->start or B->data is "evaluated" first. The result of interest is address of the referenced element, which is the sum of the base address and the product of the index and the size of the element. The two addends are independent, meaning that the order of their evaluation is irrelevant. The result of the addition is an ...


2

Let us keep this discussion to the C language only. The program you are referring to is written in a high level language like C. The computer understand machine language only. Higher level languages gives the programmer the ability to express logic in a more human friendly way which is then translated into machine code which the microprocessor can decode ...


34

I think your main question seems to be: "If the type is erased at compile-time and not retained at runtime, then how does the computer know whether to execute code wich interprets it as an int or to execute code which interprets it as a char?" And the answer is … the computer doesn't. However, the compiler does know, and it will have simply put the correct ...


8

The computer doesn't "know" what addresses are what, but the knowledge of what's what is baked into the instructions of your program. When you write a C program that writes and reads a char variable, the compiler creates assembly code that writes that piece of data somewhere as a char, and there is some other code somewhere else that reads a memory address ...


101

To address the question you've posted in several comments(which I think you should edit into your post): What I don't understand is how does the computer know lets when it reads a variable's value from and address such as 10001 if is an int or char. Imagine I click on a program called anyprog.exe. Immediately the code starts executing. Does this exe file ...


7

It doesn't. Once C is compiled to machine code, the machine just sees a bunch of bits. How those bits are interpreted depends on what operations are being performed on them as opposed to some additional metadata. The types you enter in your source code are just for the compiler. It takes what type you say the data is supposed to be and, to the best of its ...


3

In C, it is not. Other languages (e.g., Lisp, Python) have dynamic types but C is statically-typed. That means that your program must know what type the data is to properly interpret is as a character, an integer, etc. Usually the compiler takes care of this for you, and if you do something wrong, you'll get a compile-time error (or warning).



Top 50 recent answers are included