- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想到了 C 和 bool< 中的
在 C++ 中。_Bool
/bool
(stdbool.h
) 类型
我们使用 boolean 类型来声明对象,该对象只能保存 0 或 1 的值。例如:
_Bool bin = 1;
或
bool bin = 1;
(注:bool
是stdbool.h
头文件中_Bool
的宏。)
在 C 语言中,
或
bool bin = 1;
在 C++ 中。
<小时/>但是 _Bool
和 bool
的 boolean 类型真的有效吗?
我做了一个测试来确定内存中每个对象的大小:
对于C:
#include <stdio.h>
#include <stdbool.h> // for "bool" macro.
int main()
{
_Bool bin1 = 1;
bool bin2 = 1; // just for the sake of completeness; bool is a macro for _Bool.
printf("the size of bin1 in bytes is: %lu \n",(sizeof(bin1)));
printf("the size of bin2 in bytes is: %lu \n",(sizeof(bin2)));
return 0;
}
输出:
the size of bin1 in bytes is: 1
the size of bin2 in bytes is: 1
对于 C++:
#include <iostream>
int main()
{
bool bin = 1;
std::cout << "the size of bin in bytes is: " << sizeof(bin);
return 0;
}
输出:
the size of bin in bytes is: 1
<小时/>
因此, boolean 类型的对象确实存储在内存中的 1 个字节(8 位)内,而不是通常只需要一个 1 位。
这里讨论原因:Why is a char and a bool the same size in c++? 。这不是我的问题。
<小时/>我的问题是:
为什么我们在 C 和 bool< 中使用
在 C++ 中,如果它们不提供内存存储方面的好处,因为它是专门假装使用这些类型的?_Bool
/bool
(stdbool.h
) 类型
为什么我不能只使用 int8_t
或 char
类型(假设 char
包含 8 位(其中通常是这样)在具体实现中)改为?
是否只是为了给代码读者提供明显的印象,即各个对象用于 0
或 1
/true
或仅用于 false
目的?
非常感谢您的参与。
最佳答案
Why do we use the types of _Bool/ bool (stdbool.h) in C and bool in C++, if they do not provide a benefit in memory storage, as it is specificially pretended for use these types?
您已经在问题中提到了原因:
We use the boolean types to declare objects, that only shall hold the values of 0 or 1
专门使用 boolean 数据类型的优点是因为它只能表示 true 或 false。其他整数类型具有更多可表示的值,当您只需要两个时,这是不希望的。
Why can´t I just use the types of int8_t or char (assuming char is contained of 8 bit (which is usually the case) in the specific implementation) instead?
你可以。事实上,C 直到 C99 标准才出现 boolean 数据类型。请注意使用 int8_t
的缺点是不保证所有系统都提供。和问题 char
是它可以是签名的或未签名的。
但您不需要这样做,因为您可以使用 boolean 数据类型。
<小时/>This implies that there is difference when I use trueor false with boolean types in comparison to when I use these with char or int8_t. Could you state this difference?
考虑以下简单示例:
int8_t i = some_value;
bool b = some_value;
if (i == true)
if (i)
if (b == true)
if (b)
对于int8_t
,这两个条件有不同的行为,如果选择了错误的形式,就会为错误的行为创造机会。对于 boolean 值,它们具有相同的行为,并且没有错误的选择。
附注如果你想紧凑地存储多个 boolean 值(以每次读写多条指令为代价),你可以使用 std::bitset
或std::vector<bool>
例如。在 C 中,没有类似的标准库实用程序,但此类功能可以通过移位和屏蔽来实现。
关于c++ - 为什么使用 _Bool 和 bool 而不是 int8_t 或 char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59861976/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!