小编Ben*_*sen的帖子

强制编译时计算C.

假设我有一个文件,我在其中对某些数据进行了一些计算.它可能是(非常psedo计算)像这样:

void hash_value(unsigned char* value){
    unsigned char i;
    for(i + 0; i < 10; i++){
        value[i] ^= (0x1b+i)
    }
}

void break_value(unsigned char* value){
    unsigned char i;
    for(i = 0; i < 10; i++)
        value[i] &= 0x82;
}

void affect_value(unsigned char* value){
    hash_value(value);
    break_value(value);
}
Run Code Online (Sandbox Code Playgroud)

在我的主要内容中,我会做以下事情:

#include "smart_calculations.h"

int main() {
    unsigned char value[16] = {'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S', 'S'};
    affect_value(value);
    // Do other stuff

}
Run Code Online (Sandbox Code Playgroud)

由于我不打算更改值数组的内容,但我需要先进行计算才能继续执行其他操作.我想一些编译器会识别并优化代码,以便在编译时计算数据.

我的问题是,我如何(尽可能)强制编译器在编译时进行此优化,这样"Smart_calculations"文件中的方法不会浪费最终产品中的空间,并且数组的初始值不是'汇编到程序中?

c compiler-optimization

6
推荐指数
1
解决办法
120
查看次数

如何处理ExecutorService任务抛出的异常?

我正在使用 ExecutorService 在系统上运行测试。当测试由于某种原因失败时就会出现问题。这些原因可能有很多,当然我会记录这些错误。

但我想实时通知用户错误。因此,当错误发生时,到达顶部的时间越长,它就变成TestFailedException带有消息和原因的错误。我想捕获这个异常并通知用户,但我似乎无法做到这一点。

这是实际启动运行测试的线程的方法示例:

public void asyncRunTestfixture(Concretetest concretetest) {

    ExecutorService executor = Executors.newCachedThreadPool();
    FutureTask<Boolean> futureTask_1 = new FutureTask<Boolean>(new Callable<Boolean>() {
        @Override
        public Boolean call() throws TestFailedException {
            return RunTestInFixture(concretetest);          
        }

      });
      executor.execute(futureTask_1);
      executor.shutdown(); 
}
Run Code Online (Sandbox Code Playgroud)

但即使 call 抛出异常,我也无法在其他地方捕获它,无论是现在还是如果我说 asyncRunTestfixture 方法抛出异常。

我该如何解决这个问题?

java multithreading exception

3
推荐指数
1
解决办法
5416
查看次数

将指针传递给typedef'ed数组

初始化堆栈上的4x4无符号字符数组后,我想将指向该数组的指针传递给另一个函数.我不明白为什么会失败.

从我的角度来看,它应该与将指针传递给任何其他数组(数组的起始地址)相同.

虽然在尝试传递它时,似乎只有阵列中的第一个位置在正确的位置,并且我在所有其他位置访问随机存储器.

我在这做错了什么?任何解释为什么会这样?

#include <stdio.h>

typedef unsigned char multi_array[4][4];


void print_array(multi_array* arr){
    unsigned char i,j;

    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            printf("%d ", *arr[i][j]);
        }
    }
    printf("\n");
}

int main() {
    unsigned char i,j,z;
    multi_array arr;    // Aren't we allocating memory on the stack for a 4x4 u_char array?

    z = 0;

    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            arr[i][j] = z++;
        }
    } …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

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