Asked  7 Months ago    Answers:  5   Viewed   67 times

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 -1's but its not, only first value is and the rest are 0's mixed with random values.

The code

int array[100] = {0};

works just fine and sets each element to 0.

What am I missing here.. Can't one initialize it if the value isn't zero ?

And 2: Is the default initialization (as above) faster than the usual loop through the whole array and assign a value or does it do the same thing?

 Answers

85

Using the syntax that you used,

int array[100] = {-1};

says "set the first element to -1 and the rest to 0" since all omitted elements are set to 0.

In C++, to set them all to -1, you can use something like std::fill_n (from <algorithm>):

std::fill_n(array, 100, -1);

In portable C, you have to roll your own loop. There are compiler-extensions or you can depend on implementation-defined behavior as a shortcut if that's acceptable.

Tuesday, June 1, 2021
 
Sethunath
answered 7 Months ago
60

For understanding the difference, we need to understand two different contexts.

  • In value contexts, the name of an array of type T is equivalent to a pointer to type T, and is equal to a pointer to the array's first element.
  • In object contexts, the name of an array of type T does not reduce to a pointer.

What is object context?

In a = b;, a is in object context. When you taken the address of a variable, it's used in object context. Finally, when you use sizeof operator on a variable, it's used in object context. In all other cases, a variable is used in value context.

Now that we have this knowledge, when we do:

void f(int arr[4]);

It is exactly equivalent to

void f(int *arr);

As you found out, we can omit the size (4 above) from the function declaration. This means that you can't know the size of the "array" passed to f(). Later, when you do:

int a[]={1,2,3,4};
f(a);

In the function call, the name a is in value context, so it reduces to a pointer to int. This is good, because f expects a pointer to an int, so the function definition and use match. What is passed to f() is the pointer to the first element of a (&a[0]).

In the case of

int a[]={1,2,3,4};
int b[4] = a;

The name b is used in a object context, and does not reduce to a pointer. (Incidentally, a here is in a value context, and reduces to a pointer.)

Now, int b[4]; assigns storage worth of 4 ints and gives the name b to it. a was also assigned similar storage. So, in effect, the above assignment means, "I want to make the storage location the same as the previous location". This doesn't make sense.

If you want to copy the contents of a into b, then you could do:

#include <string.h>
int b[4];
memcpy(b, a, sizeof b);

Or, if you wanted a pointer b that pointed to a:

int *b = a;

Here, a is in value context, and reduces to a pointer to int, so we can assign a to an int *.

Finally, when initializing an array, you can assign to it explicit values:

int a[] = {1, 2, 3, 4};

Here, a has 4 elements, initialized to 1, 2, 3, and 4. You could also do:

int a[4] = {1, 2, 3, 4};

If there are fewer elements in the list than the number of elements in the array, then the rest of the values are taken to be 0:

int a[4] = {1, 2};

sets a[2] and a[3] to 0.

Tuesday, June 1, 2021
 
bumperbox
answered 6 Months ago
55

If it's a primitive type, you can use Arrays.fill():

Arrays.fill(array, -1);

[Incidentally, memset in C or C++ is only of any real use for arrays of char.]

Saturday, July 31, 2021
 
Anele
answered 4 Months ago
96

The behavior must be identical, but any differences in the generated code really depend on the compiler.

For example, the compiler could generate this for the initialized variable:

somefunction:
pushl    %ebp
movl     %esp, %ebp
pushl    $2 ; allocate space for x and store 2 in it
...

and this for the uninitialized, but later assigned variable:

somefunction:
pushl   %ebp
movl    %esp, %ebp
subl    $4, %esp ; allocate space for x
...
movl    $2, -4(%ebp) ; assign 2 to x
...

The C standard does not mandate the generated code to be identical or non-identical in these cases. It only mandates identical behavior of the program in these two cases. And that identical behavior does not necessarily imply identical machine code.

Friday, August 6, 2021
 
Dobz
answered 4 Months ago
25

That's the default implementation of toString() in Object you're seeing. You can use Arrays.toString for a readable result (make sure to import java.util.Arrays):

System.out.println(Arrays.toString(a));
Wednesday, August 25, 2021
 
Denis
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share