C ++宏乘法发生了什么 [英] What's going on with C++ macro multiplication

查看:203
本文介绍了C ++宏乘法发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define MAX 265

std::cout << 0 * MAX << std::endl; //to my surprise, the output is 9 rather than 0

这个C ++宏乘法?

EDIT

以下是完整版本。

#include <stdio.h>
#include <string.h>
#include <iostream>

#define NAME_BYTES 256
#define VERSION_BYTES 256
#define SIZE_BYTES 32
#define USED_LOCK_COUNT_BYTES 32
#define LOCK_NAME_BYTES 256
#define LOCK_TYPE_BYTES 1
#define PID_BYTES 4
#define TID_BYTES 4
#define LOCK_BYTES LOCK_NAME_BYTES + LOCK_TYPE_BYTES + PID_BYTES + TID_BYTES 
#define HEADER_BYTES NAME_BYTES + VERSION_BYTES + SIZE_BYTES + USED_LOCK_COUNT_BYTES

int main() {
  std::cout << "LOCK_BYTES: " << LOCK_BYTES << std::endl;
  std::cout << "HEADER_BYTES: " << HEADER_BYTES << std::endl;
  std::cout << "LOCK_BYTES * 0: " << 0 * LOCK_BYTES << std::endl;
}

这是我刚才得到的结果和编译器信息。

Here's the result I just got and the compiler info.


yifeng @ yifeng-Precision-WorkStation-T3400:〜/ Shared-Memory-Solution / examples / IMPL $
g ++ -v使用内置眼镜。 COLLECT_GCC = g ++
COLLECT_LTO_WRAPPER = / usr / lib / gcc / x86_64-linux-gnu / 4.6.1 / lto-wrapper
目标:x86_64-linux-gnu配置为:../src/configure - v
--with-pkgversion ='Ubuntu / Linaro 4.6.1-9ubuntu3'--with-bugurl = file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages = c,c ++,fortran,objc,obj-c ++,go --prefix = / usr --program- suffix = -4.6 --enable-shared --enable-linker-build-id --with-system-zlib - -libexecdir = / usr / lib --unhout-included-gettext --enable-threads = posix --with-gxx-include-dir = / usr / include / c ++ / 4.6 --libdir = / usr / lib --enable -nls --with-sysroot = / --enable-clocale = gnu --enable-libstdcxx-debug --enable-libstdcxx-time = yes --enable-plugin --enable-objc-gc --disable-werror - -with-arch-32 = i686 --with-tune = generic --enable-checking = release --build = x86_64-linux-gnu --host = x86_64-linux-gnu --target = x86_64-linux-gnu型号:posix gcc版本4.6.1(Ubuntu / Linaro 4.6.1-9ubuntu3)

yifeng@yifeng-Precision-WorkStation-T3400:~/Shared-Memory-Solution/examples/IMPL$ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

yifeng @ yifeng-Precision-WorkStation-T3400:〜/ Shared- / examples / IMPL $
./a.out LOCK_BYTES:265 HEADER_BYTES:576 LOCK_BYTES * 0:9

yifeng@yifeng-Precision-WorkStation-T3400:~/Shared-Memory-Solution/examples/IMPL$ ./a.out LOCK_BYTES: 265 HEADER_BYTES: 576 LOCK_BYTES * 0: 9

编辑:谢谢你们这么多人!我一直很高兴,我决定发布,虽然我得到这么多downvote。

EDIT: Thank you so much guys!! I've been most happy that I decided to post this, although I am getting so many downvotes. What a lesson to learn about MACRO!

推荐答案

您应该始终在宏定义的后面加上括号:

You should always put parentheses around macro definitions:

#define LOCK_BYTES (LOCK_NAME_BYTES + LOCK_TYPE_BYTES + PID_BYTES + TID_BYTES)

否则,代码会扩展为:

cout << 0 * LOCK_NAME_BYTES + LOCK_TYPE_BYTES + PID_BYTES + TID_BYTES

输出 LOCK_TYPE_BYTES + PID_BYTES + TID_BYTES

更好的是,不要使用宏,除非你真的需要。这些更好地表示为常量变量。

Better still, don't use macros unless you really have to. These are better represented as constant variables.

这篇关于C ++宏乘法发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆