我知道使用==检查浮点变量的相等性不是一个好方法。但我只想通过以下陈述来了解这一点:
float x = ...
float y = x;
assert(y == x)
Run Code Online (Sandbox Code Playgroud)
既然y是从 复制的x,那么这个断言是真的吗?
如何将文件读入a std::string,即一次读取整个文件?
文本或二进制模式应由调用者指定.该解决方案应符合标准,便携且高效.它不应该不必要地复制字符串的数据,它应该避免在读取字符串时重新分配内存.
实现此目的的一种方法是统计文件大小,调整大小std::string和fread()进入std::string's const_cast<char*>()' data().这要求std::string数据是连续的,这是标准不需要的,但似乎是所有已知实现的情况.更糟糕的是,如果在文本模式下读取文件,则其std::string大小可能与文件大小不同.
一个完全正确的,符合标准的和便携式解决方案,可以构建使用std::ifstream的rdbuf()进入std::ostringstream,并从那里进入std::string.但是,这可能会复制字符串数据和/或不必要地重新分配内存.所有相关的标准库实现是否足够智能以避免所有不必要的开销?还有另一种方法吗?我是否错过了一些已经提供所需功能的隐藏Boost功能?
请显示您的建议如何实施.
void slurp(std::string& data, bool is_binary)
Run Code Online (Sandbox Code Playgroud)
考虑到上面的讨论.
我经常看到人们不鼓励其他人使用scanf并说有更好的选择。但是,我最终看到的只是“不要使用scanf”或“这里是正确的格式字符串”,并且从来没有提到“更好的替代方案”的任何示例。
例如,让我们看一下这段代码:
scanf("%c", &c);
Run Code Online (Sandbox Code Playgroud)
这将读取最后一次转换后留在输入流中的空白。通常建议的解决方案是使用:
scanf(" %c", &c);
Run Code Online (Sandbox Code Playgroud)
还是不使用scanf。
由于scanf不好,用于转换scanf通常无需使用即可处理的输入格式(例如整数,浮点数和字符串)的ANSI C选项有哪些scanf?
我从同事那里了解到,无需编写函数即可编写和执行C程序main().它可以在下面完成
withoutMain.c
/* Compile this with gcc -nostartfiles */
#include <stdlib.h>
void _start() {
int ret = my_main();
exit(ret);
}
int my_main() {
puts("This is a program without a main() function!");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将其编译为:
my_main.c
运行方式为:
main()
我的问题是,何时需要做这种事情?一些现实世界的场景?
在#include.c文件中,.c文件是否可以(或者甚至是推荐/良好实践)?将它们包含在项目文件中会发生什么?
c code-organization project-organization include c-preprocessor
我正在阅读一本使用 C的书(Butenhof 的POSIX 线程编程,1997 年),我遇到了以下行:
(void)free(data);
Run Code Online (Sandbox Code Playgroud)
这里,data只是一个指向已分配结构的指针,
data = malloc(sizeof(my_struct_t));
Run Code Online (Sandbox Code Playgroud)
为什么是free被强制转换的结果void?
根据我对 C 的理解,这似乎没有意义,原因有两个:
void这本书写于 1997 年。这是某种遗产吗?
作者提到这些示例是在 Digital Unix 4.0d 上运行的,但如果您不打算使用该结果,我仍然无法想象有理由强制转换该函数的结果。
当了解如何原始的运营商,如+,-,*并/用C实现,我发现从下面的代码片段一个有趣的答案.
// replaces the + operator
int add(int x, int y) {
while(x) {
int t = (x & y) <<1;
y ^= x;
x = t;
}
return y;
}
Run Code Online (Sandbox Code Playgroud)
似乎此函数演示了如何+在后台实际工作.但是,理解它对我来说太困惑了.我相信这样的操作是使用编译器生成的汇编指令很长时间完成的!
我的问题是:+运算符是否作为MOST实现上发布的代码实现?这是否利用了两个补码或其他依赖于实现的功能?如果有人能解释它是如何工作的,我会非常感激.
嗯...也许这个问题在SO上有点偏离主题,但我认为通过这些运算符来看是有点好的.
我不太明白为什么我不能除以零例外:
int d = 0;
d /= d;
Run Code Online (Sandbox Code Playgroud)
我本来希望得到除以零的除法运算,但是反而d == 1。
为什么在什么时候不d /= d将被零除d == 0?
c++ division divide-by-zero compiler-optimization undefined-behavior
我正在尝试使用以下代码(test.c)"mmap"二进制文件(~8Gb).
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
const char *memblock;
int fd;
struct stat sb;
fd = open(argv[1], O_RDONLY);
fstat(fd, &sb);
printf("Size: %lu\n", (uint64_t)sb.st_size);
memblock = mmap(NULL, sb.st_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
if (memblock == MAP_FAILED) handle_error("mmap");
for(uint64_t i = 0; i < 10; i++)
{
printf("[%lu]=%X ", i, memblock[i]);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.c是使用 …
C语言中未指定行为的一个示例是评估函数参数的顺序.它可能是左右或左右,你只是不知道.这会影响评估方式foo(c++, c)或foo(++c, c)得到评估.
还有什么其他未指明的行为可以让不知情的程序员感到惊讶?