A
Andrew Fabbro
I have a need to create an array that is larger than size_t - 1, which
I believe is the largest guaranteed array. I though I'd found
salvation in FAQ 6.14, but it appears I am not understanding
something.
The array is an array of pointers to structs. Here is an example
using a small array:
/* checks that malloc succeeded, main's return, etc. removed for
brevity */
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} example_struct;
int main (void) {
example_struct * small_array[1000];
example_struct * a_single_struct;
a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);
small_array[900] = a_single_struct;
printf ("small_array 900 foo: %i\n",small_array[900]->foo);
}
This works fine:
a_single_struct foo: 4000
small_array 900 foo: 4000
So far, so good. Now I want to do an array of, say, 3,000,000
members, too large for a simple big_array[3000000] declaration. So I
use the code from faq 6.14:
/* checks that malloc succeeded, main's return, etc. removed for
brevity */
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} example_struct;
int main (void) {
example_struct * big_array;
example_struct * a_single_struct;
a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);
big_array = malloc ( 3000000 * sizeof ( example_struct * ) );
big_array[1000000] = a_single_struct; /* line 20, error below */
printf ("big_array foo: %i\n",big_array[1000000]->foo);
}
No go. The compiler (gcc in this case) returns:
test.c: In function 'main':
test.c:20: error: incompatible types in assignment
test.c:21: error: invalid type argument of '->'
So the example in FAQ 6.14 is:
"int *dynarray;
dynarray = malloc(10 * sizeof(int));
....you can reference dynarray (for i from 0 to 9) almost as if
dynarray were a conventional, statically-allocated array (int a[10])"
Given that, I'm confused why
small_array[900] = a_simple_struct;
works, while
big_array[1000000] = a_simple_struct;
doesn't.
Is there some obvious error in my code? Have I just embarrassed
myself by displaying my obtusity before the comp.lang.c gods? Or is
there some better method of creating arrays > (size_t - 1) ?
Thanks.
I believe is the largest guaranteed array. I though I'd found
salvation in FAQ 6.14, but it appears I am not understanding
something.
The array is an array of pointers to structs. Here is an example
using a small array:
/* checks that malloc succeeded, main's return, etc. removed for
brevity */
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} example_struct;
int main (void) {
example_struct * small_array[1000];
example_struct * a_single_struct;
a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);
small_array[900] = a_single_struct;
printf ("small_array 900 foo: %i\n",small_array[900]->foo);
}
This works fine:
a_single_struct foo: 4000
small_array 900 foo: 4000
So far, so good. Now I want to do an array of, say, 3,000,000
members, too large for a simple big_array[3000000] declaration. So I
use the code from faq 6.14:
/* checks that malloc succeeded, main's return, etc. removed for
brevity */
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int foo;
int bar;
} example_struct;
int main (void) {
example_struct * big_array;
example_struct * a_single_struct;
a_single_struct = malloc ( sizeof(example_struct *) );
a_single_struct->foo = 4000;
printf ("a_single_struct foo: %i \n", a_single_struct->foo);
big_array = malloc ( 3000000 * sizeof ( example_struct * ) );
big_array[1000000] = a_single_struct; /* line 20, error below */
printf ("big_array foo: %i\n",big_array[1000000]->foo);
}
No go. The compiler (gcc in this case) returns:
test.c: In function 'main':
test.c:20: error: incompatible types in assignment
test.c:21: error: invalid type argument of '->'
So the example in FAQ 6.14 is:
"int *dynarray;
dynarray = malloc(10 * sizeof(int));
....you can reference dynarray (for i from 0 to 9) almost as if
dynarray were a conventional, statically-allocated array (int a[10])"
Given that, I'm confused why
small_array[900] = a_simple_struct;
works, while
big_array[1000000] = a_simple_struct;
doesn't.
Is there some obvious error in my code? Have I just embarrassed
myself by displaying my obtusity before the comp.lang.c gods? Or is
there some better method of creating arrays > (size_t - 1) ?
Thanks.