tags:

views:

71

answers:

3

Hi guys!

I want to use designated initializers, but what about the others index that aren't initialized?

Are they spend my memory too?

for example:

EDIT PART: {

int array[590] = {[2] = 1};

note: I don't use the other array index, like this example i want to use only one, but i will allocate memory for another index?

(okey I know that this example is vague, i don't want only one int or any other type, I don't know how to explain what I want. But I think that explain it is not necessary because my question is not 'how to accomplish this' my question is 'whats happen when I do it?' or 'how it is implemented?', thanks a lot.)

what about the memory in this? I waste 590 piece of memory, or only one? If the first is correct, how Can I spend only one?

and if I do this:?

int array [] = { [2] = 1, [590] = 2};

I will allocate 590 piece of memory, or only two?

}

Thanks so much!

+1  A: 

Yes, the declaration you use will create an array of 5 elements, and only set one of them to the value '1'. The rest will be uninitialised (I believe).

What you want, I think, is a hash table or some other kind of associative container, which C does not have in it's standard library. You'll either have to write one yourself, or find one that someone else has written.

Dean Harding
How to avoid it?
drigoSkalWalker
You need to implement an associated container. There is no such thing in the standard C library, but it's not *that* hard to implement yourself. Of course, for such a small array as the one in your example, it really doesn't matter but I assume you're planning on much bigger arrays. Kevin Little gave you some good keywords to plug into google...
Dean Harding
The rest will *not* be uninitialised - they will be implicitly initialised to 0. Objects in C are *never* partially initialised, so if you have an initialiser for one member of an array, the entire array is initialised.
caf
@caf: I stand corrected. I did a quick google to check before I posted my answer, but I got conflicting answers so I just went with the safest assumption :)
Dean Harding
A: 

Yes memory is allocated when you declare an array, even though you dont initialize every element. If you want only one value stored you need a single variable, you can also Allocate memory dinamically but it requires a good knolewdge of pointers and memory allocation/management functions (malloc,realloc,free,etc.)

dallen
So, how I can allocate dinamically to avoid spend my memory?
drigoSkalWalker
MALLOC:Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space.CALLOC:Allocates space for an array of elements initializes them to zero and returns a pointer to the memoryFREE:Frees previously allocated spaceREALLOC:Modifies the size of previously allocated space.
dallen
The link is for a dynamic memory allocation tutorial.http://www.exforsys.com/tutorials/c-language/dynamic-memory-allocation-in-c.html
dallen
+2  A: 

Let's ask Mr. Compiler!

#include <stdio.h>
int main( int argc, char ** argv )
{
    int array[] = {[2] = 1, [590] = 2};
    printf("sizeof(array) is %d bytes\n", sizeof(array));
}

Survey says:

$ gcc initsize.c
$ ./a.out
sizeof(array) is 2364 bytes

Yep! 591 * 4 = 2364.

Here, you've allocated a 591 element integer array on the stack. It has to be allocated; the compiler does not know what you may do with it (pass it to a library function it knows nothing about, for example). You told it the size is 591 elements, and it obeys...

P.S. There are many "sparse matrix" C libraries; just google for "C library sparse matrix". But, for a vector of 591 elements, they are total over-kill. Now, if you have 10,000 of such vectors, that's another story.

Kevin Little
haha, great answer! thanks guy!
drigoSkalWalker
So, how to accomplish what I want? allocate only parts of my index?
drigoSkalWalker