10

I created a dynamic array ,and i need to initialize all the members to 0. How can this be done in C?

   int* array;
    array = (int*) malloc(n*sizeof(int));
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69

5 Answers5

21

In this case you would use calloc():

array = (int*) calloc(n, sizeof(int));

It's safe to assume that all systems now have all zero bits as the representation for zero.

§6.2.6.2 guarantees this to work:

For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.

It's also possible to do a combination of malloc() + memset(), but for reasons discussed in the comments of this answer, it is likely to be more efficient to use calloc().

Community
  • 1
  • 1
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • It's more than "safe to assume", I'd say that it's actually guaranteed by §6.2.6.2. – Matteo Italia Jan 07 '12 at 21:50
  • @Mysticial it is guaranteed by the standard (C99, 6.2.6.2p5) "For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type." – ouah Jan 07 '12 at 21:52
  • Yes, you are right, I found the line that says this. Adding to my answer. – Mysticial Jan 07 '12 at 21:52
  • Value bits => "Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type"; Sign bits => "If the sign bit is zero, it shall not affect the resulting value." Padding bits aren't relevant. – Matteo Italia Jan 07 '12 at 21:53
  • The cast is unnecessary too in C. – Mat Jan 07 '12 at 21:54
  • @MatteoItalia sorry, I didn't have your answer displayed when I responded – ouah Jan 07 '12 at 21:54
  • @Mat Correct, it's not necessary. I was mainly trying to preserve the OP's sample. Though the cast can still be useful if the code is ever ported to C++. – Mysticial Jan 07 '12 at 22:08
  • @Mysticial: I'd hope conversion to C++ would get rid of that `malloc` altogether and replace it with proper containers :) – Mat Jan 07 '12 at 22:11
7
memset(array, 0, n*sizeof(int));

Or alternatively, you could allocate your block of memory using calloc, which does this for you:

array = calloc(n, sizeof(int));

calloc documentation:

void *calloc(size_t nmemb, size_t size);

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. ...

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
4

Use calloc function (usage example):

int *array = calloc(n, sizeof(int));

From calloc reference page:

void *calloc(size_t nelem, size_t elsize);

The calloc() function shall allocate unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.

Community
  • 1
  • 1
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
0
memset(array, 0, n * sizeof *array);
ouah
  • 142,963
  • 15
  • 272
  • 331
0
                    for (i=0; i<x; ++i){
                        array[i]=0;
                    }

That should do the trick. I guess you have to make a loop that makes 0 every element in the array. Memset could also work for you.

BugShotGG
  • 5,008
  • 8
  • 47
  • 63
  • Hey, you do not have to write a program that is interactively proving the answer correct. Finding the one line that does the trick in this source is way harder... – Doncho Gunchev Jan 07 '12 at 22:00
  • @dgunchev Yes you are right but i was just writing something similar in c arm when i saw the question by chance and thought about a fast answer. :) – BugShotGG Jan 07 '12 at 22:04