-1
/*
 * Recommended alloc parameters for "small" contexts that are never expected
 * to contain much data (for example, a context to contain a query plan).
 */

    #define ALLOCSET_SMALL_MINSIZE   0
    #define ALLOCSET_SMALL_INITSIZE  (1 * 1024)
    #define ALLOCSET_SMALL_MAXSIZE   (8 * 1024)
    #define ALLOCSET_SMALL_SIZES \
        ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

I don't understand last marco, I use

printf("%d", ALLOCSET_SMALL_SIZES);

then warning:

warning: too many arguments for format [-Wformat-extra-args]

and also return 0.

similar post I found: #define directive with multiple replacements?


Update: Jumped around source code, got it.
function like

foo(size_t,size_t,size_t)

can just use

foo(ALLOCSET_DEFAULT_SIZES)
Harith
  • 4,663
  • 1
  • 5
  • 20
jian
  • 4,119
  • 1
  • 17
  • 32
  • 3
    `ALLOCSET_SMALL_SIZES` expands eventually to `0, (1 * 1024), (8 * 1024)`, i.e. to 3 values, and your `printf` specifier mentions only 1 (the 1 `%d`). – wohlstad Jan 21 '23 at 08:55
  • ALLOCSET_SMALL_SIZES now is like a int array? @wohlstad – jian Jan 21 '23 at 08:56
  • No it's not an array. The preprocessor is simply replacing strings in your source code before the compiler gets it. The line `printf("%d", ALLOCSET_SMALL_SIZES);` becomes `printf("%d", 0, (1 * 1024), (8 * 1024));` after the preprocessor is doing macro replacement. I suggest you read about preprocessor macros and how they work. – wohlstad Jan 21 '23 at 08:59
  • The preprocessor is not aware of arrays whatsoever. It works with tokens. – kotatsuyaki Jan 21 '23 at 09:01
  • 2
    @jian No, just a list of comma separated values. What that means semantically is context dependent. Here it is a list of variadic arguments for `printf`, and you particular compiler helpfully checks for format specifier mismatch. The error is all about the resulting printf call, and nothing to do with the macro. Clearly you need `"%d, %d, %d"` to print all three values. – Clifford Jan 21 '23 at 09:02

1 Answers1

3

Given the following snippet:

#define ALLOCSET_SMALL_SIZES \
        ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

The macro ALLOCSET_SMALL_SIZES gets replaced with:

ALLOCSET_SMALL_MINSIZE, ALLOCSET_SMALL_INITSIZE, ALLOCSET_SMALL_MAXSIZE

which gets replaced with:

0,
(1 * 1024),
(8 * 1024)

which are separate arguments. Then in the call to printf ():

printf("%d", ALLOCSET_SMALL_SIZES);

ALLOCSET_SMALL_SIZES gets replaced with 0, (1 * 1024), (8 * 1024)

Or,

printf ("%d", 0, (1 * 1024), (8 * 1024));

Hence the warning.


Fix:

Replace it with:

printf ("%d %d %d", 0, (1 * 1024), (8 * 1024));

See: Why use Macros in C? and What are C macros useful for?

Harith
  • 4,663
  • 1
  • 5
  • 20