Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm currently working on a system where features are enabled by a database check.
if(feature_db(id)) {
//new logic
else {
//old logic
}
I want to replace this with a MACRO.
FEATURE_ENABLE(feature_db, id)
//new logic
FEATURE_DISABLE
//old logic
END_FEATURE
I want to do this because it becomes more clear where these occur and its more explicit what is happening. I want to know if there is just a general more elegant way to to do this?
No, your resulting code is less clear for any competent C programmer: C programmers know about if and function calls. They do not know about the syntax and semantics of your custom macros that do not follow general convention. As a consequence, they first need to discover what the macros do, and ensure that using them in this way is in fact correct and safe — neither of which is obvious from your code.
However, you can make your initial code clearer by naming the function better and adjusting its parameters, e.g.:
if (feature_enabled(feature_db, id)) {
…
} else {
…
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have experienced a strange problem when using assert in my program.
The program does not terminate even when I add a line of codeassert(false).
But the assert works when I write several lines of sample code. Anybody know why it happened?
If you have:
#define NDEBUG
this turns all assert's into nop's.
If you have differing behaviour, depending on the amount of code, then I guess you don't have NDEBUG defined and I would guess the compiler is simply compiling out the redundant code.
More details about environment are required, however, you give a definitive answer.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I had some fun with imitating OOP in C, but I got somewhat discouraged when I understood that I'll have to call member methods like obj->method(obj, ...). Then I thought about cloning and modifying functions at runtime. Can I implement a function like strdup but for functions using a simple parser to identify the return opcode to stop copying and then modify a value in the function to point to the object the member method refers to so that I can use just obj->method(...)?
No, at least from what it sounds like you are asking, which is to modify functions at run-time. Modifying functions at run-time is possible but is difficult and requires considerable knowledge (and is system-specific). However, you seem to be asking to be able to execute a function and modify the function so that it does something with the object it is associated with. However, by the time the function is executing, there is generally no information about that object available: In a call like obj->method(…), there is generally no reference to obj included in the arguments. So even if you could modify the function at run-time, it does not have the information needed to do the job you want.
There are ways to do it at compile-time. That is how C++ developed. If that is a feature you want, the best approach is to use C++.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Shorted the title to make it easier for people to understand the problem.
When I've finished adding data to an array, I need to open Activity Monitor and close two processes (Swift and SourceKitService).
Because of the array, Swift and SourceKitService use all available RAM and they have to be closed.
How can I keep adding to the array in the swift file without having to continually close processes?
The Swift compiler is probably getting tied up in knots because your array is long AND it can’t determine the type so needs to rely on implicit checking which is expensive.
The longer the array the bigger the problem.
You may need to provide an explicit type to your declaration.
E.g
let foo: [String] = [“boa”,”fool”,”zoo”,...
Vs
let foo = [“boa”,”fool”,”zoo”,...
Whatever the type of the array elements you should declare this explicitly to assist the compiler when you encounter this issue.
Yes, it should just work, but this is real life and the Swift compiler is young.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
This code can be compiled in C:
#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
printf("shouldnt allow this line");
return 1; //also this line
}
the line printf("shouldnt allow this line"); and return 1; are unreachable. Is there any ways to check this during compilation with warning messages? Also, why does the compiler allows this?
Unreachable code is not an error because:
It's often useful, especially as the result of macro expansion or functions which are only ever called in a way that makes some paths unreachable due to some of their arguments being constant or limited to particular range. For instance, with an inline version of isdigit that's only ever called with non-negative arguments, the code path for an EOF argument would be unreachable.
In general, determining whether code is unreachable is equivalent to the halting problem. Sure there are certain cases like yours that are trivial to determine, but there is no way you can specify something like "trivial cases of unreachable code are errors, but nontrivial ones aren't".
Broadly speaking, C does not aim to help a developer catch mistakes; rather, C trusts the developer to do a perfect job, just as it trusts the compiler to do a perfect job.
Many newer languages take a more active stance, aiming to protect the developer from his or herself — and plenty of C compilers will emit compile-warnings (which can typically be "promoted" to errors via command-line flags) — but the C community has never wanted the language to stop trusting developers. It's just a philosophical difference. (If you've ever run into a case where a language prevents you from doing something that seems wrong but that you actually have a good reason for, you'll probably understand where they're coming from, even if you don't agree.)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
As part of an assignment I was asked to implement a list of simple functions in C and whenever possible replace those functions with a macro for runtime efficiency reasons.
Is there a general rule that helps me identify if I can replace a simple function in C with a macro? And why exactly does this replacement optimise runtime efficiency?
Thank you for your time.
EDIT: As it has been pointed out by many voices here, the goal of this assignment is counter productive concerning quality of code and even efficiency in some cases.
The only general rule is: avoid function-like macros as far as possible, because they are often unreadable and/or unsafe. You should always use a function when you can.
Function-like macros usually only make sense when dealing with compile-time issues such as constants, identifiers, declarations etc.
Replacing functions with macros for the sake of performance is not something you should even consider. It once was, some 20 years ago, because then compilers were so horrible at optimizing code that programmers did that job better than the compilers. Nowadays it is the other way, leave such things to the compiler.