小编Gos*_*low的帖子

为什么没有初始化而不是越界?

在下面的代码中为什么b[9]未初始化而不是越界?

#include <stdio.h>

int main(void)
{
    char b[] = {'N', 'i', 'c', 'e', ' ', 'y', 'o', 'u', '!'};
    printf("b[9] = %d\n", b[9]);

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

编译器调用:

% gcc -O2 -W -Wall -pedantic -c foo.c
foo.c: In function ‘main’:
foo.c:6:5: warning: ‘b[9]’ is used uninitialized in this function [-Wuninitialized]
     printf("b[9] = %d\n", b[9]);
% gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. …
Run Code Online (Sandbox Code Playgroud)

c gcc gcc-warning

45
推荐指数
2
解决办法
2241
查看次数

如何修复"在丢弃的部分中定义"链接器错误?

我的程序编译好没有-flto但是使用-flto我得到这个错误:

% arm-none-eabi-g++ --version
arm-none-eabi-g++ (4.8.3-9+11) 4.8.3 20140820 (release)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

% arm-none-eabi-g++ -O2 -W -Wall -fPIE -flto -march=armv7-a -mfloat-abi=hard -mfpu=vfpv3-d16 -ffreestanding -nostdlib -std=gnu++11 -fno-exceptions -fno-rtti -c -o main.o main.cc

% arm-none-eabi-g++ -fPIE -nostdlib -O2 -flto boot.o memcpy.o font.o main.o -lgcc -Tlink-arm-eabi.ld -o kernel.elf
`memcpy' referenced in section `.text' of /tmp/ccYO5wE8.ltrans0.ltrans.o: …
Run Code Online (Sandbox Code Playgroud)

c++ g++ linker-errors

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

如何在arm asm上写PC相对地址?

我正在使用GNU AS作为汇编程序,我正在尝试编写与位置无关的二进制文件,并且在编写外部符号的PC相对地址时遇到了一些问题.Normaly是一个相对地址

adr r0, symbol
Run Code Online (Sandbox Code Playgroud)

但这仅适用于汇编文件中同一部分中定义的符号.另一种加载符号的方法是

ldr r0, =symbol
Run Code Online (Sandbox Code Playgroud)

它将符号的绝对地址存储在一个常量中,并从那里加载它的pc.所以你得到这样的代码:

  48:   e59f0008        ldr     r0, [pc, #8]    ; 58 <text+0xc>
  ...
  58:   00000008        .word   0x00200018
                        58: R_ARM_ABS32 symbol
Run Code Online (Sandbox Code Playgroud)

但我需要的是一个R_ARM_REL32链接器引用.

我想出的是这段代码:

tmp1:    ldr    r0, [pc, #tmp2 - tmp1 - 8]
         ldr    r0, [pc, r0]
tmp2:    .word  symbol - tmp1
Run Code Online (Sandbox Code Playgroud)

这会产生:

0000003c <tmp1>:
  3c:   e59f0000        ldr     r0, [pc]        ; 44 <tmp2>
  40:   e79f0000        ldr     r0, [pc, r0]

00000044 <tmp2>:
  44:   00000008        .word   0x00000008
                        44: R_ARM_REL32 symbol
Run Code Online (Sandbox Code Playgroud)

我几乎可以将它放入一个宏中,因此它可以重用于我需要的任何符号.除了我不知道如何告诉汇编程序tmp2需要转到常量块而不是在代码中间结束.

但是在GNU AS中是不是已经存在一些现有的语法或宏?

assembly arm

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

如何使用少于8 GB的ram编译带有模板的余弦表?

我正在尝试使用2.14签名格式(2位有符号整数,14位分数)生成固定点算术的余弦/正弦表.余弦/正弦的参数被归一化并折叠在180度,90度和45度轴附近,因此我只需要从0到45度(或12867作为定点)的余弦值和正弦值.代码计算一个稍大的表,从0到1弧度(或16384作为固定点).

我已经为8.8,7.9,6.10,5.11,4.12和3.13位固定点测试了这段代码但是无法为2.14位定点编译它.当g ++使用大约7 GiB的ram并且仍在增长时,我停止了它.

那么如何让模板使用更少的ram?

#include <stdint.h>
#include <math.h>

template <uint16_t...> struct IndexList {};

template <uint16_t left_base, typename LeftList,
          uint16_t right_base, typename RightList> struct Merge;

template <uint16_t left_base, uint16_t... left,
          uint16_t right_base, uint16_t... right>
struct Merge<left_base, IndexList<left...>,
             right_base, IndexList<right...> > {
    typedef IndexList<left..., right...> Result;
};

template <uint16_t base, uint16_t n> struct Indexes {
    static constexpr uint16_t left_base = base;
    static constexpr uint16_t left = n / 2;
    static constexpr uint16_t right_base = base + n / 2;
    static constexpr …
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates

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

如何专门化模板子类?

我正在尝试在另一个类中专门化一个模板类,但编译器不会让我.代码在类Foo之外工作但不在内部,我希望struct Bla对Foo类是私有的.

class Foo {
   template<typename ... Ts> struct Bla; 
   template<> struct Bla<> { static constexpr int x = 1; };
};

error: explicit specialization in non-namespace scope 'class Foo'
Run Code Online (Sandbox Code Playgroud)

c++ templates template-specialization

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

为什么const vector <const pair <... >>给出'不能重载'的错误?

我有这个简单的代码:

#include <vector>
#include <string>

void foo(const std::vector<std::pair<std::string, int> > & networks) {
  for (auto p : networks) {
  }
}

void bla(const std::vector<const std::pair<std::string, int> > & networks) {
  for (auto p : networks) {
  }
}
Run Code Online (Sandbox Code Playgroud)

这会产生错误bla():

mrvn@frosties:~% g++ -O2 -W -Wall -g -std=gnu++17 -c bla.cc
In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/c++allocator.h:33:0,
                 from /usr/include/c++/5/bits/allocator.h:46,
                 from /usr/include/c++/5/vector:61,
                 from bla.cc:1:
/usr/include/c++/5/ext/new_allocator.h: In instantiation of ‘struct __gnu_cxx::new_allocator<const std::pair<std::__cxx11::basic_string<char>, int> >’:
/usr/include/c++/5/bits/allocator.h:92:11:   required from ‘class std::allocator<const std::pair<std::__cxx11::basic_string<char>, int> >’
/usr/include/c++/5/bits/stl_vector.h:79:14:   required …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++17

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

uint32_t * uint32_t = uint64_t与gcc的向量乘法

我正在尝试将uint32_t产生完整64位结果的uint64_t向量乘以gcc 中的向量。我期望的结果是gcc发出一条VPMULUDQ指令。但是,gcc作为代码输出的结果是可怕地乱堆了各个uint32_t源向量,然后进行了完整的64 * 64 = 64乘法运算。这是我尝试过的:

#include <stdint.h>

typedef uint32_t v8lu __attribute__ ((vector_size (32)));
typedef uint64_t v4llu __attribute__ ((vector_size (32)));

v4llu mul(v8lu x, v8lu y) {
    x[1] = 0; x[3] = 0; x[5] = 0; x[7] = 0;
    y[1] = 0; y[3] = 0; y[5] = 0; y[7] = 0;
    return (v4llu)x * (v4llu)y;
}
Run Code Online (Sandbox Code Playgroud)

第一个掩盖了uint32_t向量的不需要部分,希望gcc可以优化掉64 * 64 = 64乘法的不需要部分,然后看到掩盖也是没有意义的。没有这种运气。

v4llu mul2(v8lu x, v8lu y) {
    v4llu tx = {x[0], …
Run Code Online (Sandbox Code Playgroud)

c gcc vectorization avx2 gcc9

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

ocaml类,方法接受派生类

请考虑以下代码:

module Proxy = struct
  type 'a t
end

class qObject proxy = object(self : 'self)
  method proxy : 'self Proxy.t = proxy
end

class qWidget proxy = object(self : 'self)
  inherit qObject proxy
  method add : qWidget -> unit = fun w -> ()
  method as_qWidget = (self :> qWidget)
end

class qButton proxy = object(self : 'self)
  inherit qWidget proxy
  method text = "button"
end

let qObject_proxy : qObject Proxy.t = Obj.magic 0
let qWidget_proxy : qWidget Proxy.t …
Run Code Online (Sandbox Code Playgroud)

inheritance ocaml class

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

如何在裸机上的Raspberry Pi 2上启动额外的内核?

问题相当广泛但我甚至无法在ARMv7 ARM,MPCore TRM,GIC架构手册中找到起点......所以请原谅模糊性.

我有一个简单的裸机内核用于Raspberry Pi 2初始化活动LED,UART0,MMU和缓存,一切正常.我可以眨眼,我可以输出文本,我可以将物理页面映射到虚拟地址并访问它们.到现在为止还挺好.

现在我想启动额外的核心,在那里我遇到了真空.目前还没有关于如何做到这一点的Linux内核的例子,因为它支持如此多的主板,因此相当复杂.看看规格我似乎找不到任何好的起点.因此,我没有在黑暗中徘徊,而是来到这里.:)

那么有没有其他人研究过这个并找出核心在启动和重置时的状态?什么启动协议/机制用于启动aditional核心?我发现的一个信息是这是特定于SOC的,所以请不要在Cortex-A9或其他不是RPi 2的情况下做一些示例.

bare-metal smp raspberry-pi2

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

如何创建+就地填充容器?

我正在尝试创建一个无法复制或移动的类数组.所以我需要就地创建对象,我无法弄清楚如何做到这一点:

#include <vector>

struct Foo {
  Foo(int) { }
  Foo(const Foo &) = delete;
  Foo & operator =(const Foo &) = delete;
  Foo(Foo &&) = delete;
  Foo & operator =(Foo &&) = delete;
};

struct Bla {
  Bla(const std::vector<int> & args) {
    for (auto i : args) {
      foo.emplace_back(i);
    }
  }
  std::vector<Foo> foo;
};
Run Code Online (Sandbox Code Playgroud)

编译器抱怨删除的移动构造函数,因为它不能保证所有对象都就地构造并且从不移动.我不必使用std::vector容器,所以请随意提出其他建议.

c++ in-place

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

C/C++ 标准中定义的具有灵活数组成员的结构体的用法在哪里?

如果我有这样的代码

struct s { int x; char b[]; };

int main() {
    struct s s[10];
}
Run Code Online (Sandbox Code Playgroud)

我用“gcc -O2 -W -Wall -pedantic”进行编译,然后我得到:

<source>:4:14: warning: invalid use of structure with flexible array member [-Wpedantic]
    4 |     struct s s[10];
      |              ^
Run Code Online (Sandbox Code Playgroud)

gcc 是完全正确的。具有灵活数组成员的结构不能这样工作。

C/C++ 标准中的什么地方定义了这个?

c c++ language-lawyer

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