几天前我接受了一次采访,我被要求在C中编写一个程序,该程序崩溃系统/关闭系统.毋庸置疑,我觉得非常愚蠢,不知道如何接近:(
我还是试了一下,编写了使用大量内存的程序.但我的采访者对我的任何技术都不满意.
请看一下这段代码:
#include <stdio.h>
int main(void)
{
short s;
int i = 65696;
float f = 65696.0F;
printf("sizeof(short) = %lu\n", sizeof(short));
s = i;
printf("s = %hd\n", s);
s = f;
printf("s = %hd\n", s);
s = 65696;
printf("s = %hd\n", s);
s = 65696.0F;
printf("s = %hd\n", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了输出:
sizeof(short) = 2
s = 160
s = 160
s = 160
s = 32767
Run Code Online (Sandbox Code Playgroud)
在最后一行为什么它是32767而不是160?说f = 65696.0F; s = f;和之间有什么区别s = 65696.0F;?
有人可以向我解释以下代码输出:
void myprint(unsigned long a)
{
printf("Input is %lx\n", a);
}
int main()
{
myprint(1 << 31);
myprint(0x80000000);
}
Run Code Online (Sandbox Code Playgroud)
输出gcc main.c:
Input is ffffffff80000000
Input is 80000000
Run Code Online (Sandbox Code Playgroud)
为什么被(1 << 31)视为签名并被0x80000000视为未签名?
调用null函数指针的行为是什么?
void (*pFunc)(void) = NULL;
pFunc();
Run Code Online (Sandbox Code Playgroud)
为什么将未使用的函数指针初始化为NULL是可取的?
std::unique_ptr<int> ptr() {
std::unique_ptr<int> p(new int(3));
return p; // Why doesn't this require explicit move using std::move?
} // Why didn't the data pointed to by 'p' is not destroyed here though p is not moved?
int main() {
std::unique_ptr<int> a = ptr(); // Why doesn't this require std::move?
std::cout << *a; // Prints 3.
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,函数ptr()返回一个副本p.如果p超出范围,数据"3"应被删除.但是代码如何在没有任何访问冲突的情况下工作?
我们已经知道,VLA(在C99中标准化)不是C++标准的一部分.
所以下面的代码在C++中是"非法的":
void foo(int n) {
int vla[n];
for (int i = 0; i < n; ++i) {
vla[i] = i;
}
}
Run Code Online (Sandbox Code Playgroud)
尽管编译器(g ++和clang ++)接受代码作为有效语法,但只有在启用情况下才会生成警告 .-pedantic
ISO C++禁止变长数组'vla'[-Wvla]
我的问题是:
为什么编译器接受该声明?
编译器不能只拒绝一个长度为的数组[is-no-know-at-compile-time]?
是否存在一种兼容性语法规则?
标准说了什么?
从生成的汇编代码中我看到编译器在循环中写入堆栈,就像普通数组一样,但我找不到任何关于标准行为的信息.
在下面的例子中,我试图扫描boolean变量类型的值.当我在GCC编译时,我得到以下警告,
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘_Bool *’ [-Wformat=]
scanf("%d",&b);
Run Code Online (Sandbox Code Playgroud)
码:
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool b;
scanf("%d",&b);
printf("%d\n",b);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,bool中是否有任何格式说明符C?
我遇到了这段代码:
char str[600];
scanf("%s", &str);
Run Code Online (Sandbox Code Playgroud)
当然,这会发出此警告:
a.c:6:17: warning: format specifies type 'char *' but the argument has type
'char (*)[600]' [-Wformat]
scanf("%s", &str);
~~ ^~~~~~~
Run Code Online (Sandbox Code Playgroud)
我知道正确的方法是删除&和键入scanf("%s", str).但它确实有效,所以我的问题是这是否会导致任何问题.是UB吗?当我切换str到指针而不是数组时(显然)不起作用.但是这会在使用数组时引起任何问题吗?
(我在这里讨论了一个答案,答案者不想改变他对正确方法的回答)
我有一些工作生命游戏代码.它将每个群体保存为位图.这是输出的样子(裁剪):

清理代码时,我发现如果我注释掉或以其他方式删除第60行:
cout << "Survivor: " << x << ", " << y << "\n";
Run Code Online (Sandbox Code Playgroud)
它完全搞砸了程序,而不是生产像它应该的滑翔机,它产生了这个:

我已经四处寻找,试图找出可能导致这种情况的原因,但我迄今为止都没有成功.这是我目前的代码:
//Bitmap Library from http://partow.net/programming/bitmap/
#include "bitmap_image.hpp"
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
#define WIDTH 160
#define HEIGHT 128
bool population[WIDTH][HEIGHT];
bool survivors[WIDTH][HEIGHT];
int check_survivors();
int check_neighbors(int x, int y);
int write_population(char* file);
int main() {
int i, populations;
cout << "Enter number of populations: ";
cin >> populations;
//Glider
survivors[28][100] = true;
survivors[29][100] = true;
survivors[29][101] = true; …Run Code Online (Sandbox Code Playgroud) 我有这段代码:
#include <stdio.h>
void optimization_headache()
{
int t = 11;
int l[1047] = {0};
int u_lu[23] = {0};
int u = 0;
l[0] = 0;
l[1] = 0;
do {
u++;
//printf("Incrementing u, now u is %d\n", u);
u_lu[u + 1] = u - l[u + 1];
} while ((u < t * 2) && (l[u + 1] <= t));
printf("u = %d, l[u] = %d, t = %d, u_lu[u] = %d\n", u, l[u], t, u_lu[u]);
}
int main()
{ …Run Code Online (Sandbox Code Playgroud)