For example, I have this structure and code:
typedef struct
{
long long *number;
} test;
test * test_structure;
test_structure = malloc(sizeof(test));
test_structure->number = malloc(sizeof(long long) * 100);
free(test_structure->number);
free(test_structure);
versus this structure and code:
typedef struct
{
long long number[100];
} test;
test * test_structure;
test_structure = malloc(sizeof(test));
free(test_structure);
Since i know that the number array will ALWAYS be 100, are either of these methods perfectly acceptable, or is one way better than the other, and why?
The second way is better for several reasons:
With all that said, the two methods are substantially the same. You use (I was tempted to say ‘waste’ but that’s not quite fair) more space with the first, but it is unlikely to be a major problem (and if it was, you’d be likely to want to vary the size from 100, as that will save you more space than tinkering with pointer vs array).