What does #define __UNUSED__ do? - c

I am going through a C code and I found something like this:
#define __UNUSED__
char buf[MAX_BUF_LENGHT];
int errors=0;
What does this mean?

I am not aware that __UNUSED__ is a predefined preprocessor symbol. So it must be a user defined symbol.
I myself have sometimes (test) code or obsolete code in a c-file that I mark-out with #ifdef BLIEP (and BLIEP is normally not defined), but can put it back into compilation by placing a #define BLIEP. Probably the original author of this code did something similar with __UNUSED__.

Related

Use a macro both in implementation and header and then undefine it

I have a macro that should be used both in my source file and header one. However I don't wan't other code linked to the final object to access that macro (more than anything else I don't want the macro to go causing unexpected errors in other files). I thought about using a macro with a long and complicated name that will be unlikely used from other code, however this solution kinda looks ugly to me. Obviously the most simple solution would be to undefine the macro in some way, however if I define the macro in the header and then undefine it – I think – I won't be able to access it anymore from the source file. What should I do?
// hi.h
#define string char *
void greet(string x);
// hi.c
#include "hi.h"
void greet(string x) {
printf("Hi!");
}
Okay, don't kill me, this was just an example, i know #define string char * is horrible.
Last minute thought: Maybe I can underfine the macro at the end of the source file, is this acceptable to do?
I guess you could conditionally "undefine" macro at the end of the header when the a magic macro is not defined. The blessed source file would have to define this macro prior to including a header.
// header.h
...
#ifndef MAGIC_MACRO
#undef string
#endif
// common source
#include "header.h"
// blessed source
#define MAGIC_MACRO
#include "header.h"
This solution will work great as long as no macro defined inside the header uses string macro.
What should I do?
Pick option 1 a macro with a long and complicated name that will be unlikely used from other code as it's the simplest and most obvious. Do not use a complicated name - just use a name so that you and other developers will know it's a private symbol, that's all.
// hi.h
// this macro is private
#define _lib_string char *
Remember about reserved words. Example: https://github.com/GNOME/glib/blob/main/glib/glib-private.h#L32 .
he most simple solution would be to undefine the macro in some way, however if I define the macro in the header and then undefine it – I think – I won't be able to access it anymore from the source file
If you go this way, you'll end up with spaghetti code, where some global state affects what you have. For example:
// hi.h
#define string char *
void greet(string x);
#ifndef FROM_HI_C
#undef string
#endif
// hi.c
#define FROM_HI_C
#include "hi.h"
void greet(string x) {
printf("Hi!");
}
Maybe I can underfine the macro at the end of the source file, is this acceptable to do?
Other files see only the header file - they are unaffected by anything in the source file.

what does it mean define macro function as (0)?

I see in some source code this kind of definition
#define somemacro(a,b,c) (0)
And I see in the same source code:
#define anothermacro(a,b,c) (1)
Can any body explain this kind of macro definition? what is the purpose of such definition?
Usually non-used parameters of a function like macro occurs in the following situation:
#ifdef A_IS_GREAT_ALWAYS
#define anothermacro(a,b,c) (1)
#else
#define anothermacro(a,b,c) ((a)>(b)+(c))
#endif
Without parameters it would break the build in the following kind of places, when A_IS_GREAT_ALWAYS is not defined.
...
if (anothermacro(foo, bar, baz))
...
The precompiler translates the example code to:
if ((1))
or
if (((foo)>(bar)+(baz)))
depending is the A_IS_GREAT_ALWAYS defined or not.

Why does the Linux kernel #define a symbol as itself?

In the Linux kernel, there can be found a line of code that looks redundant to me:
#define __arch_swahb32 __arch_swahb32
What is the purpose of an idiom like this?
Consider the following code:
#ifdef foo
foo();
#endif
If you want a snippet like the above to call function foo, you need to define foo. However, if you just
#define foo
then the function foo name will be replaced with an empty token, and the first snippet is preprocessed to just ();. If, however, you
#define foo foo
then the first snippet will preprocess to foo(); as it should.
Trick to ensure #if defined(__arch_swahb32) passes but doesn't replace. (Often used to implement macro type functions)

Call from c code a kernel variable in freebsd

I found some online resources about this topic but still can not understand how it works.
Lets assume that I have a global variable with following specification
in file: /sys/sys/sysctl.h
#define USER_TZNAME_MAX 20 /*test var*/
and in file /usr/src/sys/kern/kern_mib.c a
SYSCTL_INT(_user, USER_TZNAME_MAX, tzname_max, CTLFLAG_RW, 0, 0, "something");
can anyone show practically how to change the variable value and set another value in a c source file?
Thank you
#define USER_TZNAME_MAX is not defining a global variable, it is a preprocessor macro.
Before the compiler compiles the code the preprocessor is run to expand macros and include/exclude code as defined by definitions.
In an example such as this, the preprocessor will replace all instances of the string "USER_TZNAME_MAX" in the source with the string "20":
// this
int i = USER_TZNAME_MAX;
// will be expanded to this:
int i = 20;
Therefore you can't change this variable at run time because a) it isn't a variable, and b) it's a constant.
If you're talking about changing the value used in your own code you can do this:
#ifdef USER_TZNAME_MAX
#undef USER_TZNAME_MAX
#endif
#define USER_TZNAME_MAX (32)
In programs, you should use sysctl(3) to get or set system information.

A macros in the C language (#define)

I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
What's the meaning of CUSTOM_PREFIX(malloc)(x)? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x)?
More code:
#ifndef __GNUC__
#error "This file requires the GNU compiler."
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#ifndef CUSTOM_PREFIX ==> here looks like it's a variable, so if it doesn't define, then define here.
#define CUSTOM_PREFIX
#endif
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) ===> what's the meaning of this?
#define CUSTOM_FREE(x) CUSTOM_PREFIX(free)(x)
#define CUSTOM_REALLOC(x,y) CUSTOM_PREFIX(realloc)(x,y)
#define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y)
In your code, since CUSTOM_PREFIX is defined to be nothing, the string CUSTOM_PREFIX(malloc)(x) will expand to
(malloc)(x)
which is equivalent to the usual
malloc(x)
However, the CUSTOM_PREFIX allows the developer to choose a different memory management function. For example, if we define
#define CUSTOM_PREFIX(f) my_##f
then CUSTOM_PREFIX(malloc)(x) will be expanded to
my_malloc(x)
CUSTOM_PREFIX is defined as nothing, so it will just disappear, leaving behind (malloc)(x), which is the same as malloc(x). Why? I don't know. Perhaps other places in the code set CUSTOM_PREFIX to something else.
At a guess, its a macro which changes calls to malloc(x) etc. into something like:
DEBUG_malloc( x );
You can choose to supply the macro yourself, to provide a customised prefix for the functions, or not in which case the names won't be changed.

Resources