Initialization of all elements of an array to one default value in C++?

前端 未结 13 1048
礼貌的吻别
礼貌的吻别 2020-11-22 07:59

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a

int array[100] = {-1};

expecting it to be full with

相关标签:
13条回答
  • 2020-11-22 08:17

    C++11 has another (imperfect) option:

    std::array<int, 100> a;
    a.fill(-1);
    
    0 讨论(0)
  • 2020-11-22 08:17

    For the case of an array of single-byte elements, you can use memset to set all elements to the same value.

    There's an example here.

    0 讨论(0)
  • 2020-11-22 08:21

    With {} you assign the elements as they are declared; the rest is initialized with 0.

    If there is no = {} to initalize, the content is undefined.

    0 讨论(0)
  • 2020-11-22 08:23

    Should be a standard feature but for some reason it's not included in standard C nor C++...

    #include <stdio.h>
    
     __asm__
     (
    "    .global _arr;      "
    "    .section .data;    "
    "_arr: .fill 100, 1, 2; "
     );
    
    extern char arr[];
    
    int main() 
    {
        int i;
    
        for(i = 0; i < 100; ++i) {
            printf("arr[%u] = %u.\n", i, arr[i]);
        }
    }
    

    In Fortran you could do:

    program main
        implicit none
    
        byte a(100)
        data a /100*2/
        integer i
    
        do i = 0, 100
            print *, a(i)
        end do
    end
    

    but it does not have unsigned numbers...

    Why can't C/C++ just implement it. Is it really so hard? It's so silly to have to write this manually to achieve the same result...

    #include <stdio.h>
    #include <stdint.h>
    
    /* did I count it correctly? I'm not quite sure. */
    uint8_t arr = {
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
        2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    };    
    
    int main() 
    {
        int i;
    
        for(i = 0; i < 100; ++i) {
            printf("arr[%u] = %u.\n", i, arr[i]);
        }
    }
    

    What if it was an array of 1,000,00 bytes? I'd need to write a script to write it for me, or resort to hacks with assembly/etc. This is nonsense.

    It's perfectly portable, there's no reason for it not to be in the language.

    Just hack it in like:

    #include <stdio.h>
    #include <stdint.h>
    
    /* a byte array of 100 twos declared at compile time. */
    uint8_t twos[] = {100:2};
    
    int main()
    {
        uint_fast32_t i;
        for (i = 0; i < 100; ++i) {
            printf("twos[%u] = %u.\n", i, twos[i]);
        }
    
        return 0;
    }
    

    One way to hack it in is via preprocessing... (Code below does not cover edge cases, but is written to quickly demonstrate what could be done.)

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    open my $inf, "<main.c";
    open my $ouf, ">out.c";
    
    my @lines = <$inf>;
    
    foreach my $line (@lines) {
        if ($line =~ m/({(\d+):(\d+)})/) {
            printf ("$1, $2, $3");        
            my $lnew = "{" . "$3, "x($2 - 1) . $3 . "}";
            $line =~ s/{(\d+:\d+)}/$lnew/;
            printf $ouf $line;
        } else {
            printf $ouf $line;
        }
    }
    
    close($ouf);
    close($inf);
    
    0 讨论(0)
  • 2020-11-22 08:26

    In the C++ programming language V4, Stroustrup recommends using vectors or valarrays over builtin arrays. With valarrary's, when you create them, you can init them to a specific value like:

    valarray <int>seven7s=(7777777,7);
    

    To initialize an array 7 members long with "7777777".

    This is a C++ way of implementing the answer using a C++ data structure instead of a "plain old C" array.

    I switched to using the valarray as an attempt in my code to try to use C++'isms v. C'isms....

    0 讨论(0)
  • 2020-11-22 08:29

    1) When you use an initializer, for a struct or an array like that, the unspecified values are essentially default constructed. In the case of a primitive type like ints, that means they will be zeroed. Note that this applies recursively: you could have an array of structs containing arrays and if you specify just the first field of the first struct, then all the rest will be initialized with zeros and default constructors.

    2) The compiler will probably generate initializer code that is at least as good as you could do by hand. I tend to prefer to let the compiler do the initialization for me, when possible.

    0 讨论(0)
提交回复
热议问题