小编xiv*_*r77的帖子

putc和putchar的表现?

我一直认为多次调用putc比puts或printf更快.要打印"hello",例如,在一个真实的程序中,我总是使用puts或printf,但现在我正在编写一个生成C代码的程序,所以我想知道是否生成代码putchar('h'); putchar('e') ...因为我首先认为它应该更多快点.但我进行了一项测试,结果非常有趣.编译器是GCC.

#include <stdio.h>
#include <time.h>

int main() {
    time_t timer;
    FILE *f;
    int i;

    f = fopen("out.txt", "w");

    #define START_TIMER(TIMER) TIMER = clock()
    #define ELAPSED_TIME(TIMER)\
    (double)(clock() - TIMER) / (double)CLOCKS_PER_SEC

    enum { NUM_ITERS = 9999999 };
    START_TIMER(timer);
    for (i = 0; i < NUM_ITERS; i++) {
        putc('h', f);
        putc('e', f);
        putc('l', f);
        putc('l', f);
        putc('o', f);
        putc('\n', f);
    }
    printf("%.3f\n", ELAPSED_TIME(timer));
    START_TIMER(timer);
    for (i = 0; i < NUM_ITERS; i++) {
        fputs("hello", f);
    }
    printf("%.3f\n", ELAPSED_TIME(timer)); …
Run Code Online (Sandbox Code Playgroud)

c printf putchar

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

没有goto跳转进出循环的更好方法?

do {
    int j, k;
    if (num_copy_array[0][0] == num_copy_array[0][1]) {
        goto next_num;
    } else {
        goto first_time;
    }
    for (j = 0; j < ARRAY_LEN; j++) {
        for (k = 0; k < NUM_LEN; k++) {
            goto skip;
        first_time:
            j = 0, k = 2;
        skip:
            for (int j_2 = 0; j_2 <= j; j_2++) {
                for (int k_2 = 0; k_2 <= NUM_LEN; k_2++) {
                    if (j_2 == j && k_2 == k) {
                        goto next_digit;
                    }
                    if (num_copy_array[j][k] …
Run Code Online (Sandbox Code Playgroud)

c c++ goto

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

C中功能编程的意外结果

在C编程中尝试函数风格时,我尝试将以下Haskell代码转换为C.

f (0, 0, 0, 1) = 0
f (0, 0, 1, 0) = f (0, 0, 0, 1) + 1
f (0, 1, 0, 0) = f (0, 0, 1, 1) + 1
f (1, 0, 0, 0) = f (0, 1, 1, 1) + 1
f (a, b, c, d) = (p + q + r + s) / (a + b + c + d)
    where
    p
        | a > 0 = a * f (a - …
Run Code Online (Sandbox Code Playgroud)

c haskell functional-programming purely-functional

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

使用编译器优化的联合中的分段错误

我的代码使用union来表示RGB像素.它在调试模式下运行良好,但启用了编译器优化后,会出现段错误.

您可以在下面看到简化的测试代码来重现错误.我的系统是Ubuntu和GCC4.8.2.

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

union Pixel {
    uint32_t color;
    uint8_t at[4];
};

typedef struct Screen {
    union Pixel *pixels;
    int width;
    int height;
    int rPosition;
    int gPosition;
    int bPosition;
} *Screen;

Screen ScreenCreate(int width, int height, uint32_t rPosition, uint32_t gPosition, uint32_t bPosition) {
    Screen this = malloc(sizeof *this);
    this->pixels = malloc(width * height * sizeof this->pixels[0]);
    this->width = width;
    this->height = height;
    this->rPosition = rPosition;
    this->gPosition = gPosition;
    this->bPosition = bPosition;
}

void ScreenDelete(Screen …
Run Code Online (Sandbox Code Playgroud)

c return segmentation-fault

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

使用`std :: move`自动返回类型推导

从下面的程序的输出都将1AA经历时c++filt -t.我可以看到返回类型在返回时被推导为值类型而不是rvalue引用类型std::move.对于大多数返回的用例来说,这是有意义的std::move,但这是什么原因?std::move返回一个右值引用,但为什么返回类型会自动推导为值类型?

#include <iostream>
#include <memory>
#include <utility>
#include <typeinfo>

struct A
: std::unique_ptr<int>
{
    auto f()
    {
        return A();
    }

    auto g()
    {
        return std::move(A());
    }
};

int main()
{
    std::cout << typeid(decltype(A().f())).name() << ' ';
    std::cout << typeid(decltype(A().g())).name() << ' ';
    std::cout << typeid(A).name() << '\n';
}
Run Code Online (Sandbox Code Playgroud)

c++ c++14

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

(C/C++)windows7中的EOF

#include <cstdio>

void fcopy(FILE* from, FILE* to);

int main()
{
    fcopy(stdin, stdout);
    return 0;
}

void fcopy(FILE* from, FILE* to)
{
    int c;
    while ((c = getc(from)) != EOF) {
        putc(c, to);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时,^ Z(Ctrl + z)会发生一些意外的行为,我会用它来表示EOF.

我输入的"hello \n"执行'fcopy'中的while循环来打印相同的内容.

"^ Z \n"结束程序.

但是如果我输入"blahblah ^ Zasdfasdf \n",而我期望程序打印"blahblah"并终止,它会用一个小箭头打印"blahblah→"并等待我的输入.无论我在这里写下什么都将被完全复制下来; 它似乎重新执行循环,同时切掉'^ Z'之后写的任何东西.

in: hello
out: hello

in: hello^Z
out/in: hello??? // "??" is my input
out: ??

in: ^Z
termination
Run Code Online (Sandbox Code Playgroud)

任何人都可以澄清为什么程序以这种方式工作?提前感谢您的帮助.

c c++ windows

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

有符号整数类型的最大值

有符号整数类型的最大整数值是否保证
(uintmax_t)pow(2, sizeof(TYPE) * CHAR_BIT - 1) - 1
在C和C++中?

c c++ signed integer

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

std :: threads构造函数参数错误

我不是一个优秀的C++程序员,但目前使用C++的一些功能来清理我的C代码的脏部分.g ++编译器抱怨threads[i] = thread(split, i, sums[i], from, to, f, nThreads);.请帮我找到问题所在.

// mjArray只是一个瘦的类而不是std :: vector,在我的情况下太重了.

#include <cstdio>
#include <cmath>
#include <ctime>
#include <thread>
using namespace std;

template<typename T>
class mjArray {
private:
    T* _array;
    int _length;

public:
    mjArray(int length) {
        _array = new T[length];
        _length = length;
    }

    mjArray(int length, T val) {
        _array = new T[length];
        _length = length;
        for (int i = 0; i < length; ++i) {
            _array[i] = val;
        }
    }

    ~mjArray() {
        delete[] …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading templates move c++11

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

在int main(void)冗余中不是void吗?

我已经明确指出C99标准文件

6.7.5.3.14标识符列表仅声明函数参数的标识符.函数声明符中的空列表是该函数定义的一部分,指定该函数没有参数.函数声明符中的空列表不是该函数定义的一部分,它指定不提供有关参数数量或类型的信息.

我从那句话中解释的是,void在函数定义中写入是多余的.我弄错了吗?

c program-entry-point

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

仅允许特定的`typedef`作为函数参数

是否可以仅允许特定typedef的函数参数?

typedef int foo;
typedef int goo;

void f(goo g); //I don't want to accept anything `foo` as an argument
Run Code Online (Sandbox Code Playgroud)

如果不能简单地使用typedef,我可以使用什么语言结构来实现这种效果?

c++ typedef

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

是`N.0 == N &&(int)N.0 == N` true其中`N`是int范围内的整数字面值?

N.0 == N && (int)N.0 == N真的,N在整数字面的范围内int

这是真的还是假的?

c

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

这是const_cast的有效用法吗?

在示例代码中,调用message()永远不会影响类的内容,所以我希望方法是const.但是我也不希望返回值const,所以const_cast下面使用它是否安全?还是有更好的解决方案?

编辑:抱歉..这n_是一个指针!

编辑2:我终于设法正确地重新产生了问题.如果n_是一个数组int n[100]怎么办?然后我看到没有问题const_cast.

class A
{
private:
    int n_[10];

public:
    /* ... */

    int* n() const
    {
        return const_cast<int*>(n_);
    }
};
Run Code Online (Sandbox Code Playgroud)

c++ const const-cast

0
推荐指数
2
解决办法
225
查看次数

Java比C快2倍(作为C++的子集)

下面的代码是一种非常低效的乘法算法.它被写成测试目的.我相信我已经用不同的语言编写了相同的代码.

正下方是运行代码的结果.

OS: Windows 7
language: C (as a subset of C++)

compiler: Visual C++
optimization option: /Ox /Oi /Ot /Oy /GL
running time (seconds): 40 +/- 1

compiler: MinGW/gcc
optimization option: -O3 march=native
running time (seconds): 81 +/- 1

compiler: MinGW/g++
optimization option: -O3 march=native
running time (seconds): 82 +/- 1

language: Java

compiler: Oracle JDK
VM: Oracle JVM
running time (seconds): 18 +/- 1
Run Code Online (Sandbox Code Playgroud)

我相信我在我的C代码中做了一些糟糕的事情,完全优化的编译器无法以任何方式优化.如果有任何大问题请告诉我.我正在计划一个项目,其中有一部分涉及大量的计算.我决定在C中编写这个核心计算部分,但是有了这种结果,我宁可用Java编写所有东西; 它更容易,甚至更快?我仍然相信C,所以如果我的代码中有任何问题,请告诉我.我的期望是Java版本应该慢1.5倍或更慢,但它在某种程度上优于C.

TEST.C

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

typedef signed char …
Run Code Online (Sandbox Code Playgroud)

c c++ java optimization performance

-2
推荐指数
1
解决办法
454
查看次数