小编Moh*_*ain的帖子

在发布模式下,代码行为不符合预期

以下代码在调试模式和发布模式下生成不同的结果(使用Visual Studio 2008):

int _tmain(int argc, _TCHAR* argv[])
{

    for( int i = 0; i < 17; i++ ) 
    { 
        int result = i * 16;

        if( result > 255 )
        {
            result = 255;
        }

        printf("i:%2d, result = %3d\n", i, result) ; 
    } 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

调试模式的输出,如预期的那样:

i: 0, result =   0
i: 1, result =  16
(...)
i:14, result = 224
i:15, result = 240
i:16, result = 255
Run Code Online (Sandbox Code Playgroud)

释放模式的输出,其中i:15结果不正确:

i: 0, result =   0
i: 1, result …
Run Code Online (Sandbox Code Playgroud)

c c++ optimization visual-studio-2008 compiler-bug

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

什么是cout <<"\n"[a == N]; 做?

在以下示例中:

cout<<"\n"[a==N];
Run Code Online (Sandbox Code Playgroud)

我不知道该[]选项的作用cout,但是当值a等于时,它不会打印换行符N.

c++ cout string-literals

60
推荐指数
5
解决办法
3419
查看次数

Xrandr错误.BadName(命名颜色或字体不存在)

我成功地使用xrandr在我的工作场所使用这个小"脚本"扩展我的桌面.

#!/bin/sh

xrandr --newmode 1920x1080  220.64  1920 2056 2264 2608  1080 1081 1084 1128  -HSync +Vsync
xrandr --addmode VGA 1920x1080
xrandr --output VGA --mode 1920x1080
Run Code Online (Sandbox Code Playgroud)

一切都很顺利,直到有一天神奇地停止工作.当我尝试使用它.我刚收到这条消息.

X请求失败的错误:BadName(命名颜色或字体不存在)

笔记本电脑显示屏以奇怪的方式调整大小,但扩展显示器没有任何反应.

我已经恢复了gnome桌面默认配置.我改变了字体配置,我尝试了其他布局和监视器,但总是发生相同的.

screen xrandr

49
推荐指数
2
解决办法
6万
查看次数

如何将参数或参数传递给gradle任务

我有一个gradle构建脚本,我试图包含Eric Wendelin的css插件 - http://eriwen.github.io/gradle-css-plugin/

它很容易实现,因为我只想缩小(而不是组合和gzipping),我已经得到了构建脚本的相关部分,如下所示:

minifyCss {
    source = "src/main/webapp/css/brandA/styles.css"
    dest = "${buildDir}/brandA/styles.css"
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute()
}
Run Code Online (Sandbox Code Playgroud)

这很完美 - 当我运行gradle war任务时,它调用minifyCss任务,获取源css文件,并在buildDir中创建一个缩小版本

但是,我有一些css文件需要缩小,但不能合并到一个文件中(因此我没有使用combineCss任务)

我希望能够做的是使用某种类型的minifyCss任务引用变量的源和目标属性(假设这是正确的术语吗?) - 传递给签名中的任务的变量,或全局变量,或者东西......

我觉得这样的东西(这不起作用):

minifyCss(sourceFile, destFile) {
    source = sourceFile
    dest = destFile
    yuicompressor {
        lineBreakPos = -1
    }
}

war {
    baseName = 'ex-ren'
}

war.doFirst {
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandA/styles.css", "${buildDir}/brandA/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandB/styles.css", "${buildDir}/brandB/styles.css")
    tasks.myTask.minifyCss.execute("src/main/webapp/css/brandC/styles.css", "${buildDir}/brandC/styles.css")
}
Run Code Online (Sandbox Code Playgroud)

这也不起作用:

def sourceFile = null
def destFile = null

minifyCss …
Run Code Online (Sandbox Code Playgroud)

variables parameters groovy gradle

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

37
推荐指数
6
解决办法
2万
查看次数

功能优化为'gcc -O2'的无限循环

背景
我被一位朋友问过以下谜题:

void fn(void)
{
  /* write something after this comment so that the program output is 10 */
  /* write something before this comment */
}

int main()
{
  int i = 5;
  fn();
  printf("%d\n", i);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道可以有多个解决方案,一些涉及宏,一些假设有关实现和违反C.

我感兴趣的一个特定解决方案是对堆栈做出某些假设并编写以下代码:(我知道它是未定义的行为,但可能在许多实现中按预期工作)

void fn(void)
{
  /* write something after this comment so that the program output is 10 */
  int a[1] = {0};
  int j = 0;
  while(a[j] != 5) ++j;  /* Search stack until you find 5 */
  a[j] …
Run Code Online (Sandbox Code Playgroud)

c optimization gcc undefined-behavior

34
推荐指数
3
解决办法
3203
查看次数

for循环的increment语句中的奇数位运算符

鉴于此for循环:

for(++i; i < MAX_N; i += i & -i)
Run Code Online (Sandbox Code Playgroud)

它应该是什么意思?该声明的i += i & -i成就是什么?

c c++ for-loop bit-manipulation

29
推荐指数
1
解决办法
1340
查看次数

使用xor解释在数组中找到两个非重复的整数

鉴于[1,1,4,5,5,6]我们可以找到46成为非重复整数.

有一个解决方案使用XOR.

这是作者提出的算法:

#include <stdio.h>
#include <stdlib.h>

/* This finction sets the values of *x and *y to nonr-epeating
 elements in an array arr[] of size n*/
void get2NonRepeatingNos(int arr[], int n, int *x, int *y)
{
  int xor = arr[0]; /* Will hold xor of all elements */
  int set_bit_no;  /* Will have only single set bit of xor */
  int i;
  *x = 0;
  *y = 0;

  /* …
Run Code Online (Sandbox Code Playgroud)

c arrays algorithm

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

是'警告C4127`(条件表达式是不变的)有用吗?

回答这篇文章时,我建议使用do {...} while(0)多行宏.

在MSVC上,我发现此代码抛出:

warning C4127: conditional expression is constant
Run Code Online (Sandbox Code Playgroud)

为了使代码无警告,我需要选择以下丑陋的替代方案之一:

选项1

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
#endif
code_using_macro_that_generates_C4217;
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Run Code Online (Sandbox Code Playgroud)

选项2
将我的宏定义为:

#define MULTI_LINE_MACRO do { ... } while(0,0)
Run Code Online (Sandbox Code Playgroud)

要么

#define MULTI_LINE_MACRO do { ... } while((void)0,0)
Run Code Online (Sandbox Code Playgroud)

一些程序员也称它为"猫头鹰",(0,0)看起来像一只猫头鹰.

选项3
定义一个新的宏WHILE_0,它不会生成警告而是使用它而不是while(0)

问题
我相信所有选择都或多或少都是可怕的.为什么MSVC会为看似正确的代码生成此警告,并激励我在代码中添加一些丑陋以保持代码警告免费?

我相信条件中的常量表达式是完全有效的,特别是在基于编译器优化代码的能力的构造中.

此外,我没有得到warning C4127像这样的代码:

void foo(unsigned bar)
{
    while (bar >= 0)
        ;
} 
Run Code Online (Sandbox Code Playgroud)

我的问题是:是不是warning C4127: conditional expression is constant完全没用,是不是它激发了丑陋的代码?这个警告是否有助于编写更好的代码?

c c++ visual-c++ c4127

17
推荐指数
1
解决办法
3310
查看次数

在不同平台上使用void指针

我听说指针首先应该转换为void以确保不同平台上的值的一致性,并且应该使用%p格式说明符.它为什么以及究竟是什么问题?

int x=100;
int *pi=&x;
printf("value of pi is: %p",(void*)pi);
Run Code Online (Sandbox Code Playgroud)

c pointers void-pointers format-specifiers

13
推荐指数
3
解决办法
976
查看次数