相关疑难解决方法(0)

AC程序使系统崩溃

几天前我接受了一次采访,我被要求在C中编写一个程序,该程序崩溃系统/关闭系统.毋庸置疑,我觉得非常愚蠢,不知道如何接近:(

我还是试了一下,编写了使用大量内存的程序.但我的采访者对我的任何技术都不满意.

c

10
推荐指数
1
解决办法
2519
查看次数

隐式地抛出一个浮点常数

请看一下这段代码:

#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;

c floating-point casting

10
推荐指数
1
解决办法
209
查看次数

无符号和签名扩展

有人可以向我解释以下代码输出:

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视为未签名?

c unsigned language-lawyer

10
推荐指数
2
解决办法
1816
查看次数

NULL函数指针

调用null函数指针的行为是什么?

void (*pFunc)(void) = NULL;  
pFunc();
Run Code Online (Sandbox Code Playgroud)

为什么将未使用的函数指针初始化为NULL是可取的?

c c++ c99

9
推荐指数
1
解决办法
1万
查看次数

没有std :: move,如何通过值返回unique_ptr?

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"应被删除.但是代码如何在没有任何访问冲突的情况下工作?

c++ move-semantics copy-elision c++11

9
推荐指数
2
解决办法
1万
查看次数

C++编译器中的可变长度数组(VLA)

我们已经知道,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]
    是否存在一种兼容性语法规则?

  • 标准说了什么?
    从生成的汇编代码中我看到编译器在循环中写入堆栈,就像普通数组一样,但我找不到任何关于标准行为的信息.

c++ dynamic-arrays

9
推荐指数
2
解决办法
1565
查看次数

C中有bool的格式说明符吗?

在下面的例子中,我试图扫描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

c boolean

9
推荐指数
1
解决办法
9424
查看次数

将地址传递给数组而不是数组会导致问题吗?

我遇到了这段代码:

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到指针而不是数组时(显然)不起作用.但是这会在使用数组时引起任何问题吗?

(我在这里讨论了一个答案,答案者不想改变他对正确方法的回答)

c arrays

9
推荐指数
1
解决办法
182
查看次数

Game Of Life计划中令人困惑的错误

我有一些工作生命游戏代码.它将每个群体保存为位图.这是输出的样子(裁剪):

期望的输出

清理代码时,我发现如果我注释掉或以其他方式删除第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)

c++ conways-game-of-life

8
推荐指数
1
解决办法
148
查看次数

在某些条件下非常奇怪的代码,包括优化

我有这段代码:

#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)

c c++ optimization gcc

8
推荐指数
1
解决办法
168
查看次数