0

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.

1
  • 2
    First is an array of pointers to the struct (you will have to allocate the memory after for each struct used, which may not be contiguous, which will be in the heap), the second is an array of the struct itself (contiguous, on the stack). Commented Mar 31, 2017 at 14:21

4 Answers 4

2

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 structs. If you want to use the first array, and set anything into keys and vals, 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");
Sign up to request clarification or add additional context in comments.

Comments

1

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 struct HashTable


struct HashTable hashtab[25];

declare hashtab as array 25 of struct HashTable

3 Comments

...obvious from cdecl's output that I quoted in my answer.
Is it? I mean the difference is obvious from the original declaration to me, but I am not the one asking the question, an obvious beginner is.
cdecl looks like a useful resource though.
1
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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.