RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 876604
Accepted
Adokenai
Adokenai
Asked:2020-09-03 12:58:10 +0000 UTC2020-09-03 12:58:10 +0000 UTC 2020-09-03 12:58:10 +0000 UTC

如何告诉编译器用相同的非空值填充静态数组?

  • 772

一种基本的方法:在初始化期间填充数组。但是该数组是静态的,并且已经占用了图像中的空间。您需要以某种方式告诉编译器用特定值填充数组的区域。代码static int arr[5]={1}填写为{1, 0, 0, 0, 0},但需要{1, 1, 1, 1, 1}

__attribute__((fillupper(1)))被 gcc 忽略。

该数组是非常量的。一个有 5 个元素的数组就是一个例子。您需要填充 4096 个元素的数组。STL 功能的使用受到严格限制,包括衍生产品。它们都有第一句话中指出的基本解决方案。

编辑答案:

template <typename type, type v>
struct IArray {
  type val = v;
  IArray& operator=(type n){
    val=n;
    return *this;
  }
  IArray& operator|=(type n){
    val|=n;
    return *this;
  }
};

您需要为所需的转换添加/更改运算符。

c++
  • 5 5 个回答
  • 10 Views

5 个回答

  • Voted
  1. AlexGlebe
    2020-09-03T15:40:20Z2020-09-03T15:40:20Z

    不要打我。只找到了asm一种方法。

    //> g++ -std=c++11 memset.cpp
    #include<iostream>
    
    asm(  
    "   .globl  s2\n"
    "   .data\n"
    "   .align 32\n"
    "   .type   s2, @object\n"
    "   .size   s2, 16384\n"
    "s2: .fill 4096,4,777\n"
    );
    
    extern "C" { extern int s2[4096]; };
    
    int main(){
      for(size_t j = 4096+1;j>0;){--j; std::cout<<"s2["<<j<<"]="<<s2[j]<<" ";}
      std::cout<<"s2["<<-1<<"]="<<s2[-1]<<std::endl;  }
    

    错误答案: 使用memset很容易。例子:

    //> g++ -std=c++11 -S memset.cpp
    #include <string.h>
    int main(){
      int m[4096]{};
      int n[4096];
      memset(n,1,4096);
      }
    

    结果将是代码:

    subq    $32768, %rsp
    leaq    -32768(%rbp), %rax
    movl    $16384, %edx
    movl    $0, %esi
    movq    %rax, %rdi
    call    memset
    leaq    -16384(%rbp), %rax
    movl    $4096, %edx
    movl    $1, %esi
    movq    %rax, %rdi
    call    memset
    

    如您所见,没有区别,只有 0 和 1 。

    • 4
  2. AnT stands with Russia
    2020-09-03T15:32:35Z2020-09-03T15:32:35Z

    GCC 支持非标准语法

    static int arr[4096] = { [0 ... 4095] = 1 };
    

    但这是指定初始化器功能的扩展,即 这是 C,不是 C++。

    也就是说,如果此选项适合您,则可以在 C++ 项目中使用

    int arr[4096] = { [0 ... 4095] = 1 };
    

    并且在 C++ 代码中已经添加

    extern "C" int arr[4096];
    

    如果你愿意捐赠static。

    • 3
  3. Best Answer
    Vlad Nimof
    2020-09-04T14:11:14Z2020-09-04T14:11:14Z
    #include <iostream>
    
    template <int v>
    struct Int {
        int val = v;
    };
    
    int main() {
        Int<10> arr[1024];
        std::cout << arr[10].val << std::endl;
    }
    

    https://godbolt.org/z/6-BEPi

    • 3
  4. isnullxbh
    2020-09-03T17:05:45Z2020-09-03T17:05:45Z
    dd if=/dev/zero of=/tmp/sarr conv=swab count=4096
    xxd -i /tmp/sarr | sed -e 's/0x00/0x01/g' > /tmp/parr.h
    

    PS :)

    升级版:

    #include <iostream>
    
    template <typename T, size_t N, T def_val>
    class ct_array {
    public:
        constexpr explicit ct_array();
    
    public:
        const T& operator[](size_t idx) const;
    
    protected:
        T m_data[N];
    };
    
    template <typename T, size_t N, T def_val>
    constexpr ct_array<T, N, def_val>::ct_array()
        : m_data()
    {
        for (T *el = m_data; el != m_data + N; ++el)
            *el = def_val;
    }
    
    template <typename T, size_t N, T def_val>
    const T& ct_array<T, N, def_val>::operator[](size_t idx) const {
        return m_data[idx];
    }
    
    template <typename T, size_t N, T def_val>
    constexpr decltype(auto) make_ct_array() {
        return ct_array<T, N, def_val>();
    }
    
    int main() {
        constexpr auto arr = make_ct_array<int, 1024, 0x01>();
        std::cout << arr[10] << std::endl;
        return 0;
    }
    

    UPD2:

    #include <iostream>
    
    template <typename T, size_t N>
    class ct_array {
    public:
        constexpr explicit ct_array(T def);
    
    public:
        const T& operator[](size_t idx) const;
    
    protected:
        T m_data[N];
    };
    
    template <typename T, size_t N>
    constexpr ct_array<T, N>::ct_array(T def)
        : m_data()
    {
        for (T *el = m_data; el != m_data + N; ++el)
            *el = def;
    }
    
    template <typename T, size_t N>
    const T& ct_array<T, N>::operator[](size_t idx) const {
        return m_data[idx];
    }
    
    template <typename T, size_t N>
    constexpr decltype(auto) make_ct_array(T def) {
        return ct_array<T, N>(def);
    }
    
    int main() {
        constexpr auto arr = make_ct_array<double, 1024>(3.14);
        std::cout << arr[10] << std::endl;
        return 0;
    }
    
    • 1
  5. AR Hovsepyan
    2020-09-03T16:51:40Z2020-09-03T16:51:40Z

    你可以写一个函数

    template <class T>
    void f(T a[], size_t s, const T& n)
    {
         for (size_t i = 0; i < s; ++i)
             a[i] = n;
    }
    

    然后

    const int n = 4096;
    int arr[n];
    f(arr, n, 1);
    

    调用它后你会得到想要的结果

    或者使用标准算法

    const int n = 4096;
    int arr[n];
    std::fill(arr, arr + n, 1);
    
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5