相关疑难解决方法(0)

在x86和x64上读取同一页面内的缓冲区末尾是否安全?

如果允许在输入缓冲区末尾读取少量数据,则可以(并且)简化在高性能算法中找到的许多方法.这里,"少量"通常意味着W - 1超过结束的字节,其中W是算法的字节大小(例如,对于处理64位块中的输入的算法,最多7个字节).

很明显,写入输入缓冲区的末尾通常是不安全的,因为您可能会破坏缓冲区1之外的数据.同样清楚的是,在缓冲区的末尾读取到另一页面可能会触发分段错误/访问冲突,因为下一页可能不可读.

但是,在读取对齐值的特殊情况下,页面错误似乎是不可能的,至少在x86上是这样.在该平台上,页面(以及因此内存保护标志)具有4K粒度(较大的页面,例如2MiB或1GiB,可能,但这些是4K的倍数),因此对齐的读取将仅访问与有效页面相同的页面中的字节缓冲区的一部分.

这是一个循环的规范示例,它对齐其输入并在缓冲区末尾读取最多7个字节:

int processBytes(uint8_t *input, size_t size) {

    uint64_t *input64 = (uint64_t *)input, end64 = (uint64_t *)(input + size);
    int res;

    if (size < 8) {
        // special case for short inputs that we aren't concerned with here
        return shortMethod();
    }

    // check the first 8 bytes
    if ((res = match(*input)) >= 0) {
        return input + res;
    }

    // align pointer to the next 8-byte …
Run Code Online (Sandbox Code Playgroud)

c optimization performance x86 assembly

33
推荐指数
2
解决办法
2027
查看次数

C循环优化有助于最终分配

因此,对于我在计算机系统课程中的最终作业,我们需要优化这些forloops,使其比原始版本更快.使用我们的linux服务器,基本等级不到7秒,完整等级不到5秒.我在这里的代码大约需要5.6秒.我想我可能需要以某种方式使用指针来使它更快,但我不是很确定.任何人都可以提供我的任何提示或选项吗?非常感谢!

QUICKEDIT:文件必须保持50行或更少,我忽略了教师所包含的那些注释行.

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

// You are only allowed to make changes to this code as specified by the comments in it.

// The code you submit must have these two values.
#define N_TIMES     600000
#define ARRAY_SIZE   10000

int main(void)
{
    double  *array = calloc(ARRAY_SIZE, sizeof(double));
    double  sum = 0;
    int     i;

    // You can add variables between this comment ...
    register double sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, …
Run Code Online (Sandbox Code Playgroud)

c optimization loops compiler-optimization debug-mode

8
推荐指数
2
解决办法
5650
查看次数

在printf语句中连接字符串

gcc 4.7.2
c89
Run Code Online (Sandbox Code Playgroud)

你好,

#define LOG_ERR(fmt, ...)                                               \
    fprintf(stderr, "[ERROR] %s:%d: error [%s] " fmt "\n", __func__, __LINE__, strerror(errno), ##__VA_ARGS__)
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

LOG_ERR("Failed to connect to message queue [ %d ]", msg_id);
Run Code Online (Sandbox Code Playgroud)

fmt已在fprintf语句中连接.这怎么可能?

我试着用下面的代码来测试这个概念,但是因为编译错误而失败:

/* Using char array */
const char name[] = "Joe";

printf("Hello how " name " how are you today?\n");

Using constant string literal
const char *name = "Joe";

printf("Hello how " name " how are you today?\n");
Run Code Online (Sandbox Code Playgroud)

两个游戏我都有以下错误:

expected ')' before name
Run Code Online (Sandbox Code Playgroud)

非常感谢任何建议,

c printf

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

如何连接两个C风格(以null结尾)的字符串?

我想连接两个定义如下的字符串:

char hello[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char world[] = { ',', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0' };
Run Code Online (Sandbox Code Playgroud)

我明白我应该跑过第一个,找到'\0'标志而不是它开始第二个字符串.功能是否strcat以相同的方式工作?

我正在使用的代码:

for (int i = 0; i < 6; i++) {
    if (hello[i] == '\0') {
        for (int j = 0; j < 9; j++) {
            int index = 5 + j;
            hello[index] = world[j];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编译后我得到这样一个错误:

*堆栈粉碎检测到*:./ run终止

我究竟做错了什么?

c concatenation null-terminated

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