Linux types.h头文件min宏

本文深入解析了C语言中一种巧妙的宏定义min的实现原理,该宏能够比较并返回两个参数中的较小值,同时确保参数类型一致,避免编译警告。通过地址指针比较的技巧,该宏在类型不匹配时会触发编译器警告,从而帮助开发者检查潜在错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define min(x,y) ({ \
	typeof(x) _x = (x);	\
	typeof(y) _y = (y);	\
	(void) (&_x == &_y);	\
	_x < _y ? _x : _y; })

       typeof(x) _x = (x); typeof(y) _y = (y);定义一个和 x 相同类型的变量 _x,定义一个和 y 相同类型的_y,并将 x和y的值分别赋值给他们。

       (void) (&_x ==  &_y)在计算两个数的最小值之前,希望去判断一下两个值的类型是否一致,而由于C语言本身不支持我们去做类似于这样的操作typeof(_x)==typeof(_y),所以在此,通过故意判断他们2个的地址指针是否相等,而显然&_x,即x的地址,是不可能等于&_y的,但是这句话(void) (&_x == &_y);使得,如果_x和_y的类型不一样,其指针类型也会不一样,2个不一样的指针类型进行比较操作,则会引起编译器产生一个编译警告,提示你这两个值的类型不同。

比如,如果你编译下面这段代码:

int x = 2;char y = 3;int m;m = min(x,y);

编译的时候,经过预处理后,就会有这样的判断操作:

int * == char *;

因此编译器就会提示你:

warning: comparison of distinct pointer types lacks a cast

所以,这个宏的巧妙之处就在于此。

所以,总结起来就是:

(void) (&_x == &_y); 用于判断输入的两个值的类型是否是一致的。如果不一致,那么编译器就会做出如下警告:warning: comparison of distinct pointer types lacks a cast

卷 share 的文件夹 PATH 列表 卷序列号为 00000231 0A51:3C3D V:. │ common.h │ common_types.h │ config.h │ tree.txt │ typedefs.h │ ├─common_audio │ │ common_audio.sln │ │ common_audio.vcxproj │ │ common_audio.vcxproj.filters │ │ │ ├─include │ │ audio_util.h │ │ │ ├─resampler │ │ │ sinc_resampler_neon.cc │ │ │ sinc_resampler_sse.cc │ │ │ sinusoidal_linear_chirp_source.cc │ │ │ sinusoidal_linear_chirp_source.h │ │ │ │ │ └─include │ │ resampler.h │ │ │ ├─signal_processing │ │ │ complex_bit_reverse.c │ │ │ complex_bit_reverse_arm.S │ │ │ complex_bit_reverse_mips.c │ │ │ complex_fft.c │ │ │ complex_fft_mips.c │ │ │ complex_fft_tables.h │ │ │ copy_set_operations.c │ │ │ cross_correlation.c │ │ │ cross_correlation_mips.c │ │ │ cross_correlation_neon.S │ │ │ division_operations.c │ │ │ dot_product_with_scale.c │ │ │ downsample_fast.c │ │ │ downsample_fast_mips.c │ │ │ downsample_fast_neon.S │ │ │ energy.c │ │ │ filter_ar_fast_q12_armv7.S │ │ │ filter_ar_fast_q12_mips.c │ │ │ get_scaling_square.c │ │ │ min_max_operations_mips.c │ │ │ min_max_operations_neon.S │ │ │ randomization_functions.c │ │ │ real_fft.c │ │ │ refl_coef_to_lpc.c │ │ │ resample.c │ │ │ resample_48khz.c │ │ │ resample_by_2.c │ │ │ resample_by_2_internal.c │ │ │ resample_by_2_internal.h │ │ │ resample_by_2_mips.c │ │ │ resample_fractional.c │ │ │ splitting_filter.c │ │ │ spl_init.c │ │ │ spl_sqrt.c │ │ │ spl_sqrt_floor.c │ │ │ spl_sqrt_floor_arm.S │ │ │ spl_sqrt_floor_mips.c │ │ │ vector_scaling_operations.c │ │ │ vector_scaling_operations_mips.c │ │ │ vector_scaling_operations_neon.S │ │ │ │ │ └─include │ │ real_fft.h │ │ signal_processing_library.h │ │ spl_inl.h │ │ spl_inl_armv7.h │ │ spl_inl_mips.h │ │ │ └─vad │ │ vad_core.c │ │ vad_core.h │ │ vad_filterbank.c │ │ vad_filterbank.h │ │ vad_gmm.c │ │ vad_gmm.h │ │ vad_sp.c │ │ vad_sp.h │ │ webrtc_vad.c │ │ │ └─include │ webrtc_vad.h │ ├─modules │ │ audio_processing.vcxproj │ │ audio_processing.vcxproj.filters │ │ │ └─audio_processing │ ├─aec │ │ │ aec_core.c │ │ │ aec_core.h │ │ │ aec_core_internal.h │ │ │ aec_core_sse2.c │ │ │ aec_rdft.c │ │ │ aec_rdft.h │ │ │ aec_rdft_sse2.c │ │ │ aec_resampler.c │ │ │ aec_resampler.h │ │ │ echo_cancellation.c │ │ │ echo_cancellation_internal.h │ │ │ │ │ └─include │ │ echo_cancellation.h │ │ │ ├─aecm │ │ │ aecm_core.c │ │ │ aecm_core.h │ │ │ aecm_core_c.c │ │ │ aecm_core_mips.c │ │ │ aecm_core_neon.c │ │ │ aecm_core_neon.S │ │ │ aecm_core_neon_offsets.c │ │ │ aecm_defines.h │ │ │ echo_control_mobile.c │ │ │ │ │ └─include │ │ echo_control_mobile.h │ │ │ ├─agc │ │ │ analog_agc.c │ │ │ analog_agc.h │ │ │ digital_agc.c │ │ │ digital_agc.h │ │ │ │ │ └─include │ │ gain_control.h │ │ │ ├─include │ │ audio_processing.h │ │ mock_audio_processing.h │ │ │ ├─ns │ │ │ defines.h │ │ │ noise_suppression.c │ │ │ noise_suppression_x.c │ │ │ nsx_core.c │ │ │ nsx_core.h │ │ │ nsx_core_neon.c │ │ │ nsx_core_neon.S │ │ │ nsx_core_neon_offsets.c │ │ │ nsx_defines.h │ │ │ ns_core.c │ │ │ ns_core.h │ │ │ windows_private.h │ │ │ │ │ └─include │ │ noise_suppression.h │ │ noise_suppression_x.h │ │ │ └─utility │ delay_estimator.c │ delay_estimator.h │ delay_estimator_internal.h │ delay_estimator_wrapper.c │ delay_estimator_wrapper.h │ fft4g.c │ fft4g.h │ ring_buffer.c │ ring_buffer.h │ └─system_wrappers ├─interface │ compile_assert_c.h │ cpu_features_wrapper.h │ cpu_info.h │ └─source cpu_features.cc system_wrappers.vcxproj system_wrappers.vcxproj.filters 这是一个代码工程,在linux x86架构上,交叉编译arm平台的动态库,写一个makefile文件
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值