initializing array to zero

closed account (1vf9z8AR)
why doesn't the code work with an & before arr in for loop.(line 8)Shouldn't it start from initial address?

1
2
3
4
5
6
7
8
9
10
  #include<iostream>
using namespace std;
int main()
{
    int *p;
    const int len=20;
        int arr[len];
    for(p=arr;p<&arr[len];p++)
        *p=0;
}
Last edited on
I'm not sure what you're asking. What exactly isn't working? How does it differ from what you expect your code to do?

Despite &arr[len] looking like it's trying to access something out of bounds, it is actually technically legal* as long as you just don't dereference arr[len]. arr[len] is the same thing as saying *((arr) + (len)).
https://stackoverflow.com/questions/988158/take-the-address-of-a-one-past-the-end-array-element-via-subscript-legal-by-the

*Edit: Actually the language used in the standard in the above link is quite confusing to me in this regard, and there seems to be a lot of back-and-forth discussion between this, especially regarding ANSI C vs C++. I now don't think it's technically legal. It might be de facto legal, but I would avoid the &arr[len] construct and just replace it with arr + len... (or better yet, avoid using pointers in this manner and just use a regular int for loop!!)

Legal, but unnecessary pointer arithmetic:
1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
int main()
{
    int *p;
    const int len=20;
        int arr[len];
    for(p=arr;p<arr+len;p++)
        *p=0;
}


Much more clear:
1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int main()
{
    const int len=20;
    int arr[len];
    for(int i = 0; i < len; i++)
        arr[i] = 0;
}
Last edited on
Why do you think it doesn't work ? It looks like you want to initialize all elements in your array to 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>

using namespace std;

int main()
{
    int *p;
    const int len=20;
    int arr[len];
    
    for(p = arr; p < &arr[len]; p++)
      *p=0;
        
    int *ip = arr;
    int size = len;
    
    while(size-- > 0)
    {
        cout << "ip = " << *ip << " ";
        ip++;
    }
}


Output

ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0
ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0 ip = 0  
Even simpler way to initialize all elements of an array to zero is to use an empty initializer ={} or (as of C++11) {}:
1
2
    const int len=20;
    int arr[len] = {};
+1 Cubbi, the pointer arithmetic confusion made me not even think of that...
You may also see:
int arr[len] = {0};

and if re-clearing it to re-use it:

memset(arr, 0, len*sizeof(int)); //does not work for everything, but ints & doubles etc are ok.



> and if re-clearing it to re-use it:
> memset(arr, 0, len*sizeof(int)); //does not work for everything, but ints & doubles etc are ok.

This would work for everything, and it would call std::memset() if appropriate (for trivial types).
1
2
3
4
5
6
7
#include <algorithm>

template < typename T > 
void fill( T* arr, std::size_t sz, const T& v ) { std::fill_n( arr, sz, v ) ; }

template < typename T > 
void clear( T* arr, std::size_t sz ) { fill( arr, sz, T() ) ; }

With C++, we can get (and should use) abstraction with zero or minimal run-time cost.

(For some reason, godbolt was failing at creating a short url)
https://gcc.godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(j:1,source:'%23include+%3Calgorithm%3E%0A%0Anamespace+my%0A%7B%0A++++template+%3C+typename+T+%3E+%0A++++void+fill(+T*+arr,+std::size_t+sz,+const+T%26+v+)+%0A++++%7B+std::fill_n(+arr,+sz,+v+)+%3B+%7D%0A%0A++++template+%3C+typename+T+%3E+%0A++++void+clear(+T*+arr,+std::size_t+sz+)+%7B+fill(+arr,+sz,+T()+)+%3B+%7D%0A%0A++++struct+A+%7B+int+i+%3B+%7D%3B%0A++++struct+B+%7B+int+i+%3B+char+c+%3B+%7D%3B%0A%7D%0A%0Avoid+foo(+my::A*+arr,+std::size_t+sz+)+%7B+clear(+arr,+sz+)+%3B+%7D%0A%0Avoid+bar(+my::B*+arr,+std::size_t+sz+)+%7B+clear(+arr,+sz+)+%3B+%7D%0A'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:55.71955719557196,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:clang500,filters:(___0:(),b:'0',commentOnly:'0',directives:'0',intel:'0',jquery:'3.2.1',length:1,prevObject:(___0:(sizzle1505271175664:(undefined:(legend:!(27509,0,'1')))),length:1,prevObject:(___0:(jQuery3210292847579061787271:(display:''),sizzle1505271175664:(undefined:(legend:!(27509,0,'1')))),length:1)),trim:'0'),options:'-std%3Dc%2B%2B1z+-stdlib%3Dlibc%2B%2B+-Wall+-Wextra+-pedantic-errors+-O3',source:1),l:'5',n:'0',o:'x86-64+clang+5.0.0+(Editor+%231,+Compiler+%232)',t:'0')),k:44.28044280442804,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4
Topic archived. No new replies allowed.