0
int array[] = {2, 3, 4, 6};
int array[6] = {2, 3, 4, 6};
int array[MAX_SIZE] = {2, 3, 4, 6};

Here in the first statement I don't mention the no of elements an array can hold. The second array can hold 6 elements and third can also hold 6 elements. When I used sizeof(array) to determine the size. Size of first array is 16. Second and third have 24 respectively.

Why should not i prefer the first one? As it used memory, as the no of elements it has. While in case of second and third, I first tell the no. of elements it can hold and the size become 24. Further i can't use that memory for other work.

Another way to initialize with use of malloc(6 * sizeof(int)) and call free(array). This will allow me to use the unused memory. But why should i do this as array[] use memory as the no of elements it has. malloc also doing the same thing but it is easy to write and remember array[].

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Hemant Parihar
  • 732
  • 1
  • 6
  • 19

3 Answers3

2

Q: Is it better to initialize the array without the MAX_ELEMENTS?

A: It depends on what you want to do!


Use this:

int array[] = {2, 3, 4, 6};

when you want the array to have exactly as many cells as the elements in the initialization process. Note that int array[4] = {2, 3, 4, 6}; is equivalent.

Use this:

int array[6] = {2, 3, 4, 6};

when you know that you are going to need to more cells, that are going to get filled after the initialization process.

This:

int array[MAX_SIZE] = {2, 3, 4, 6};

is equivalent to the above case, when MAX_SIZE = 6.


You should check this:

Difference between static memory allocation and dynamic memory allocation

and this one for the sizeof() operator:

How do I determine the size of my array in C?


So, if you know exactly how many elements your array will have, use static memory allocation, where if you do not know, use dynamic memory allocation.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • can i add elements to array array[] ? After i initialize it. – Hemant Parihar Sep 23 '15 at 22:03
  • @HemantParihar if you don't do much work (which will be costly too), then no. If you want to add more elements, you may need to consider the dynamic memory allocation. Check my edit please! Btw, I understand your confusion, thus my upvote to your question. – gsamaras Sep 23 '15 at 22:06
  • I made a program to insert element(5) to array[] = {3, 4, 6} and i insert it successfully. so when ever i need to insert i can and its size is increased as according to no. of elements. As you say I should prefer dynamic memory allocation to insert the element . so why I prefer dynamic memory allocation to insert as i can do the same thing with static memory allocation.? – Hemant Parihar Sep 23 '15 at 22:43
  • 2
    @HemantParihar: C arrays don't dynamically resize themselves. If you thought you added an element to that array you probably just wrote past the end of the array. Your code was badly broken and unfortunately didn't crash in an obvious way. – Blastfurnace Sep 24 '15 at 02:02
  • I agree with @Blastfurnace. It's not like PHP for example. – gsamaras Sep 24 '15 at 02:15
0

I think you are confused by the sizeof operator and assume that the number 16 you get is the number of elements of the array. This is not true. The sizeof operator gives you the size of the object (in a low level sense, not in a object oriented sense) you pass, in bytes. So, you get 16 bytes, which means 4 elements, since an int has 4 bytes.

Paul92
  • 8,827
  • 1
  • 23
  • 37
0

The answer depends on how you intend on using the array.

If the array will contain a known set of elements at compile time and only those elements, don't specify the size. Then if you need to loop through the array you can use this syntax:

for (i = 0; i < sizeof(array)/sizeof(array[0]); i++)

If you ever want to add or remove elements from the list, the above statement doesn't need to change.

If the size of the array isn't related to what its initial elements are, or if the array is uninitialized or partially initialized, then you would specify the size.

dbush
  • 205,898
  • 23
  • 218
  • 273