Reputation: 34
I am doing hash implementation. I have sample program below.
#define HASHSIZE 25
struct HashTable {
char key[50];
char val[50];
};
int main() {
struct HashTable *hashtab[HASHSIZE]; // declaration - 1
struct HashTable hashtab2[HASHSIZE]; // declaration - 2
}
declaration-1:
struct HashTable *hashtab[HASHSIZE];
declaration-2:
struct HashTable hashtab2[HASHSIZE];
As per my understanding, both declarations are seems to be doing same in memory layout of allocation. Like, allocating HASHSIZE
of array of struct HashTable
and variable pointing to the [0]
th element of array of structs.
But, Still I feel that there is some difference that I might not be able to get it.
I want to know if there is a difference, how does one differ from another in memory layout (OR) allocation/accessing.
Upvotes: 0
Views: 71
Reputation: 6437
struct HashTable *hashtab[HASHSIZE];
Declares an array of pointers to struct HashTable. This array will be be on the stack. You will have to afterwards dynamically allocate the memory for the struct itself, which will be on the heap and may not be contiguous with the other struct allocations. If used to the full, the memory will be equal to the size of the array of pointers plus the size of all the allocated structures (in your example: HASHSIZE [times] sizeof(struct HashTable *) + HASHSIZE times [times] sizeof(struct HashTable)), but will be shared between heap and stack (majority on the heap).
struct HashTable hashtab2[HASHSIZE];
Declares an array of struct HashTable. Array elements are contiguous and on the stack. Memory used will be equal to the size of the array (in your example: HASHSIZE*sizeof(struct HashTable)), regardless of how many elements in the array are used.
Upvotes: 1
Reputation: 4174
The first is an array of pointers to HashTable
structs - each element of the array is the address of a HashTable
. To use it, you'll need to allocate memory for a HashTable
, then place the address of that memory into the array. You'll need to do that for each element of the array.
The second is an array of HashTable
structs - each element of the array is an actual HashTable
struct, and the memory for it is allocated for you by the compiler.
Upvotes: 0
Reputation: 726569
As per my understanding, both declarations are seems to be doing same in memory layout of allocation.
This is demonstrably incorrect: they allocate structures of very different size (200 bytes vs. 2500 bytes on a 32-bit system).
Still I feel that there is some difference that I might not be able to get it.
The first one is an array of pointers. The second one is an array of struct
s. If you want to use the first array, and set anything into key
s and val
s, you need to allocate the struct
first, for example, like this:
hashtab[pos] = malloc(sizeof(struct HashTable));
You also need to dereference the pointer:
strcpy(hashtab[pos]->key, "hello"); // Note the -> operator
strcpy(hashtab[pos]->val, "world");
On the other hand, hashtab2
has all its positions preallocated, and does not need dereferencing:
strcpy(hashtab2[pos].key, "hello"); // Using . instead of ->
strcpy(hashtab2[pos].val, "world");
Upvotes: 2
Reputation: 93274
cdecl is an useful online tool to help beginners get used to complicated declarations.
struct HashTable *hashtab[25];
declare
hashtab
as array 25 of pointer to structHashTable
struct HashTable hashtab[25];
declare
hashtab
as array 25 of structHashTable
Upvotes: 1