问题
I just noticed the macros named "_POSIX_*" in limits.h are (namewise) similar to the parameter of sysconf function. For example, there is the macro named "_POSIX_ARG_MAX", and I can also call sysconf with the argument of "_SC_ARG_MAX". Why do we need sysconf in the first place when we're totally free to use the macros in limits.h?
回答1:
The _POSIX_*
values are the minimum requirement to be POSIX compliant. They will have the same value on all platforms. The particular value supported by the implementation may be higher.
From man sysconf
:
For variables or limits, typically, there is a constant
_FOO
, maybe defined in<limits.h>
, or_POSIX_FOO
, maybe defined in<unistd.h>
. The constant will not be defined if the limit is unspecified. If the constant is defined, it gives a guaranteed value, and a greater value might actually be supported. If an application wants to take advantage of values which may change between systems, a call tosysconf()
can be made. Thesysconf()
argument will be_SC_FOO
.
For example, _POSIX_ARG_MAX
is 4096. But sysconf(_SC_ARG_MAX)
may return a larger number if the system supports it.
来源:https://stackoverflow.com/questions/42652794/posix-limits-h-vs-sc-sysconf