我正在查看一些C++代码并找到以下构造:
if('A' == 0x41) {
// ...
} else if('A' == 0xc1) {
// ...
} else {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我得到一个Visual Studio警告说:
警告C4127条件表达式是常量.
Visual Studio显然是正确的 - 当然'A'被定义为0x41.为什么作者编写这段代码,因为三个分支中有两个是死代码?
给定以下程序:
#include <stdio.h>
int main(void)
{
int i = 1, j = 2;
int val = (++i > ++j) ? ++i : ++j;
printf("%d\n", val); // prints 4
return 0;
}
Run Code Online (Sandbox Code Playgroud)
的初始化val似乎可能隐藏了一些未定义的行为,但是我看不到对象被多次修改或在其间没有序列点的情况下被修改和使用的任何地方。有人可以对此进行纠正或证实吗?
我是C和C ++编程的新手,所以我从基础开始。我为c和c ++编写了相同的Fibonacci循环程序,以测试相对速度。我以为它们对于简单的东西差不多一样,但是C ++版本慢60倍。他们所做的只是循环遍历并打印前14个斐波那契数字10,000次。这是C版本:
#include <stdio.h>
int main (){
int c = 0;
int x, y, z;
while(c < 10000)
{
x = 0;
y = 1;
while(x < 255)
{
printf("%d\n", x);
z = x + y;
x = y;
y = z;
}
c++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是c ++版本:
#include <iostream>
using namespace std;
int main()
{
int c = 0, x = 0, y = 0, z = 0;
while(c < 10000)
{
x = 0; …Run Code Online (Sandbox Code Playgroud) 我目前正在使用一个旧的C库(在90年代早期制作),以下函数声明使我感到困惑:
#define bland_dll
typedef unsigned short int usint;
typedef unsigned char uchar;
int bland_dll Read_Chan (usint channel);
Run Code Online (Sandbox Code Playgroud)
什么是bland_dll函数的名称,它的返回类型之间在做什么?
谢谢你的灯!
该malloc()函数返回一个类型的指针void*.它根据size_t作为参数传递给它的值以字节为单位分配内存.生成的分配是原始字节,可以与C中的任何数据类型一起使用(无需转换).
可以char在返回的函数内声明类型的数组void *,可以与任何数据类型一起使用,例如生成的分配malloc?
例如,
#include <stdio.h>
void *Stat_Mem();
int main(void)
{
//size : 10 * sizeof(int)
int buf[] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = Stat_Mem();
memcpy(p, buf, sizeof(buf));
for (int n = 0; n < 10; n++) {
printf("%d ", p[n]);
}
putchar('\n');
return 0;
}
void *Stat_Mem()
{
static char Array[128];
return Array;
}
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个框架来解析合适大小的文本文件,这些文件可以放在内存RAM中,而且现在情况还顺利.我没有抱怨,但如果遇到我必须处理大文件的情况,比如大于8GB(这是我的大小)怎么办?处理这些大文件的有效方法是什么?
我的框架:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int Parse(const char *filename,
const char *outputfile);
int main(void)
{
clock_t t1 = clock();
/* ............................................................................................................................. */
Parse("file.txt", NULL);
/* ............................................................................................................................. */
clock_t t2 = clock();
fprintf(stderr, "time elapsed: %.4f\n", (double)(t2 - t1) / CLOCKS_PER_SEC);
fprintf(stderr, "Press any key to continue . . . ");
getchar();
return 0;
}
long GetFileSize(FILE * fp)
{
long f_size;
fseek(fp, 0L, SEEK_END);
f_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return f_size;
}
char *dump_file_to_array(FILE …Run Code Online (Sandbox Code Playgroud) 假设程序员忘记初始化他的一个自动变量,并且他使用了它的值,从而调用了未定义的行为.
...
int i = 0, j;
...
printf("value of 'j': %d\n", j);
...
...
char buf[256];
fputs("Enter query:", stdout);
fgets(buf, sizeof(buf), stdin);
... //process input
... perform other tasks
Run Code Online (Sandbox Code Playgroud)
程序员注意到屏幕上出现了乱码,并意识到他的程序是错误的,但它并没有崩溃,反正还在继续.
假设在此之后,程序提示用户输入并期望处理它,显示结果并执行所有独立于未初始化变量的其他任务,鼓励程序员停止使用该程序,修复错误,重新编译和运行?该计划的其余部分是否会不一致?
我有两个功能。一种复制文本文件,使用"r"和"w"模式进行读写,另一种复制二进制文件,使用"rb"和"wb" 模式。
但如果我要使用这些函数,我必须首先确定我要复制的文件是文本文件还是二进制文件。我想要一个解决方案,使我能够在事先不知道文件性质的情况下成功复制文件。处理这个问题的单个函数。有任何想法吗?
PS:我注意到ctrl + c复制比我的功能更快,因此也欢迎提高功能速度的建议。
#include <stdio.h>
void copyfile_txt(const char *inputfile, const char *outputfile);
void copyfile_bin(const char *inputfile, const char *outputfile);
int main(void)
{
copyfile_txt("file_input.txt", "file_output.txt");
copyfile_bin("ubuntu-14.04.3-desktop-amd64.iso", "Ubuntu.iso");
return 0;
}
long GetFileSize(FILE *fp)
{
fseek(fp, 0L, SEEK_END);
long sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
return sz;
}
void copyfile_txt(const char *inputfile, const char *outputfile)
{
static FILE *fp_I;
static FILE *fp_O;
fp_I = fopen(inputfile, "r");
if (!fp_I) { …Run Code Online (Sandbox Code Playgroud) 我正在使用 Visual Studio 2017 在 Windows 操作系统上工作,并且我获得了以下函数来从 SO 的答案之一确定文件的大小:
__int64 FileSize(const char *filename)
{
HANDLE hFile = CreateFile(filename, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return -1; // error condition, could call GetLastError to find out more
}
LARGE_INTEGER size;
if (!GetFileSizeEx(hFile, &size)) {
CloseHandle(hFile);
return -1; // error condition, could call GetLastError to find out more
}
CloseHandle(hFile);
return size.QuadPart;
}
Run Code Online (Sandbox Code Playgroud)
因此,我使用它来确定文件大小,以便使用malloc(). 由于函数malloc()接受一个size_t类型,我将FileSize()函数的返回值赋给了一个 size_t 变量,但是我收到了以下警告: …
我正在执行一些测试,gcc以了解该规则可以智能地排除未使用的符号。
// main.c
#include <stdio.h>
void foo()
{
}
int main( int argc, char* argv[] )
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
。
// bar.c
int bar()
{
return 42;
}
Run Code Online (Sandbox Code Playgroud)
。
> gcc --version
gcc (GCC) 8.2.1 20181215 (Red Hat 8.2.1-6)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> gcc -c bar.c
> gcc …Run Code Online (Sandbox Code Playgroud)