Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Constexpr静态数组初始化和使用(使用avr-g+;+;)_C++_Arrays_Gcc_Constexpr - Fatal编程技术网

C++ Constexpr静态数组初始化和使用(使用avr-g+;+;)

C++ Constexpr静态数组初始化和使用(使用avr-g+;+;),c++,arrays,gcc,constexpr,C++,Arrays,Gcc,Constexpr,在下面的示例中,初始值设定项比静态数组小一个元素。在这种情况下,avr-g++-6.3和avr-g++-7.0生成的代码在数据段(初始值设定项)中没有任何元素。这就是我所期望的 #include <stdint.h> volatile uint8_t x = 0; volatile uint8_t y = 0; int main() { constexpr uint8_t l[4] = {0, 1, 2}; y = l[x]; while(true) {}

在下面的示例中,初始值设定项比静态数组小一个元素。在这种情况下,avr-g++-6.3和avr-g++-7.0生成的代码在数据段(初始值设定项)中没有任何元素。这就是我所期望的

#include <stdint.h>

volatile uint8_t x = 0;
volatile uint8_t y = 0;

int main() {
    constexpr uint8_t l[4] = {0, 1, 2};
    y = l[x];
    while(true) {}
}
第二种情况是:

    .file   "bm10a.cc"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
    .section    .rodata
.LC0:
    .byte   0
    .byte   1
    .byte   2
    .section    .text.startup,"ax",@progbits
.global main
    .type   main, @function
main:
    push r28
    push r29
    rcall .
    push __zero_reg__
    in r28,__SP_L__
    in r29,__SP_H__
/* prologue: function */
/* frame size = 3 */
/* stack size = 5 */
.L__stack_usage = 5
    lds r24,.LC0
    lds r25,.LC0+1
    lds r26,.LC0+2
    std Y+1,r24
    std Y+2,r25
    std Y+3,r26
    lds r30,x
    add r30,r28
    mov r31,r29
    adc r31,__zero_reg__
    ldd r24,Z+1
    sts y,r24
.L2:
    rjmp .L2
    .size   main, .-main
.global y
    .section .bss
    .type   y, @object
    .size   y, 1
y:
    .zero   1
.global x
    .type   x, @object
    .size   x, 1
x:
    .zero   1
    .ident  "GCC: (GNU) 7.0.1 20170218 (experimental)"
.global __do_copy_data
.global __do_clear_bss

[OT]:
而(true){}
是UB。真的吗?你能详细说明一下吗?如果是这样,大多数µC代码都是UB…请看,初始化不完整的数组在编译器中的处理方式似乎有所不同。第三个测试可能是如何
constexpr uint8_t l[]={0,1,2}已编译。您可以在gcc源代码中找到确切的答案。(我曾经研究过gcc源代码,以获得一些编译器构造问题的提示,但这很难理解…(对我来说))是的,不同类型的初始化处理方式不同。但是:因为它是constexpr,所以根本不应该使用任何RAM!
    .file   "bm10a.cc"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
    .section    .text.startup,"ax",@progbits
.global main
    .type   main, @function
main:
    push r28
    push r29
    rcall .
    rcall .
    in r28,__SP_L__
    in r29,__SP_H__
/* prologue: function */
/* frame size = 4 */
/* stack size = 6 */
.L__stack_usage = 6
    std Y+1,__zero_reg__
    std Y+2,__zero_reg__
    std Y+3,__zero_reg__
    std Y+4,__zero_reg__
    ldi r24,lo8(1)
    std Y+2,r24
    ldi r24,lo8(2)
    std Y+3,r24
    lds r30,x
    add r30,r28
    mov r31,r29
    adc r31,__zero_reg__
    ldd r24,Z+1
    sts y,r24
.L2:
    rjmp .L2
    .size   main, .-main
.global y
    .section .bss
    .type   y, @object
    .size   y, 1
y:
    .zero   1
.global x
    .type   x, @object
    .size   x, 1
x:
    .zero   1
    .ident  "GCC: (GNU) 7.0.1 20170218 (experimental)"
.global __do_clear_bss
    .file   "bm10a.cc"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
    .section    .rodata
.LC0:
    .byte   0
    .byte   1
    .byte   2
    .section    .text.startup,"ax",@progbits
.global main
    .type   main, @function
main:
    push r28
    push r29
    rcall .
    push __zero_reg__
    in r28,__SP_L__
    in r29,__SP_H__
/* prologue: function */
/* frame size = 3 */
/* stack size = 5 */
.L__stack_usage = 5
    lds r24,.LC0
    lds r25,.LC0+1
    lds r26,.LC0+2
    std Y+1,r24
    std Y+2,r25
    std Y+3,r26
    lds r30,x
    add r30,r28
    mov r31,r29
    adc r31,__zero_reg__
    ldd r24,Z+1
    sts y,r24
.L2:
    rjmp .L2
    .size   main, .-main
.global y
    .section .bss
    .type   y, @object
    .size   y, 1
y:
    .zero   1
.global x
    .type   x, @object
    .size   x, 1
x:
    .zero   1
    .ident  "GCC: (GNU) 7.0.1 20170218 (experimental)"
.global __do_copy_data
.global __do_clear_bss