0

I am initializing my array, with 0, and I have the buffer clean, what happens to the bits? For example, when I initialize with 'a', not the same, if it were with memset the whole buffer would be filled with 'a'?

#include <stdio.h>
#include <string.h>

int main(void) {

    char buffer[256] = {0}, array[256] = {'a'};
    char array1[256];
    memset(array1, 'a', sizeof(array1));

    printf("%c\n%c\n%c\n", buffer[1], array[1], array1[1]);

    return 0;

}
alk
  • 69,737
  • 10
  • 105
  • 255
Yuri Albuquerque
  • 474
  • 3
  • 14
  • It may have seemed like buffer was being filled entirely by the initaliser, but that's just because everything past the input you give is set to 0 for chars. If you try `buffer[256]={65}` instead you will notice that only position 0 is 65 `('A')` – Patrick Apr 08 '19 at 13:34
  • I know, it was just an example, because when it initializes with 0, the rest of the array is filled with 0 tb, I do not remember why this happens – Yuri Albuquerque Apr 08 '19 at 13:36
  • Possible duplicate of [How to initialize all members of an array to the same value?](https://stackoverflow.com/questions/201101/how-to-initialize-all-members-of-an-array-to-the-same-value) – vgru Apr 08 '19 at 13:51

3 Answers3

2

If the initialiser does not provide enough elements to initialise the complete variable the rest is initialised as if the variable were declare globally, that is:

  • integers to 0
  • floats to 0.
  • pointers to NULL.

In your particular example the remaining elements of the char-array array will be following the above rule for integers.

alk
  • 69,737
  • 10
  • 105
  • 255
2

The initialization in the case of array[256] = {'a'}; happens as per this rule:

6.7.9 Initialization
...
21 If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

So only the first element of array will have the value 'a'.

But in the case of memset function,

void *memset(void *s, int c, size_t n);

the function copies the value of c (converted to an unsigned char) into each of the first n characters of the object pointed to by s.

So in this case all the elements of array1 will have the value 'a'.

P.W
  • 26,289
  • 6
  • 39
  • 76
0

When you enter the function, in this case main() the stack is increased by the amount needed by the stack frame, in the stack frame there is space for all of the autos (variables declared inside the function) as well other information not relevant here. So in this case when you write

char array[256]

as the program enters the function the stack will be increased by enough to make room for 256 characters in the array, the value of the characters in the array are undefined, it is possible that this area in memory was previously written to by another function or program who no longer needs it, so we don't know what the value of the rest of the array is.

When you write

char array[256] = {'a'}

it is equivalent to:

char array[256];
array[0] = 'a';

In this case we have not defined what is in the rest of the array

When you do

memset(array, 'a', sizeof(array))

the CPU will need to go through the entire array and initialize each char in the array to 'a', creating a known value for everything in the array at the cost of using a little more CPU.

liamcomp
  • 376
  • 1
  • 8