Allocating memory to array of strings in c

Paldan

I'm in the process of creating a hash table. I'm using a struct for the capacity, number of keys, frequencies and the keys themselves. Here is my code for initializing the struct:

htable htable_new(int capacity) {

  htable result = emalloc(sizeof *result);
  result->capacity = capacity;
  result->frequencies = emalloc(capacity * sizeof result->frequencies[0]);
  result->keys = emalloc(capacity * sizeof result->keys[0]);
  result->frequencies = 0;
  result->keys = NULL;

  return result;
}

Now, from my understanding, a char** array is a pointer to an array of pointers (of type char)? So when I allocate memory, would it be correct to use keys[0]? I assume that this just represents the size of the char pointer? Which leads to my next question of when I actually set the keys in the array (which is obviously in another function) would I just allocate memory to each index by the size of the string I input before storing it?

i.e. h->keys[index] = emalloc(sizeof(str)

Thanks for your answers!

R Sahu

Assuming emalloc is a valid macro or function,

The calls

result->frequencies = emalloc(capacity * sizeof result->frequencies[0]);
result->keys = emalloc(capacity * sizeof result->keys[0]);

are OK. However, the next two lines:

result->frequencies = 0;
result->keys = NULL;

immediately cause memory leaks. I don't know why you have them. They should be removed.

Assuming str is of type char* or char const*, the line

h->keys[index] = emalloc(sizeof(str));

won't allocate the necessary amount of memory for h->key[index]. That will allocate enough memory to hold only a char*. You need:

h->keys[index] = emalloc(strlen(str)+1);
strcpy(h->keys[index], str);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Allocating memory for one dimensional array in C

From Dev

Allocating memory to hold an array of structs in C

From Dev

C: Array not allocating more memory correctly

From Dev

C not allocating memory for array of structs correctly?

From Dev

Memory allocating array of structure pointers in c

From Dev

allocating memory to an array of string

From Dev

Allocating Memory for string in c?

From Dev

Allocating memory (C)

From Dev

C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

From Dev

C++ allocating large array on heap gives "out of memory exception"

From Dev

Why is C++ allocating such a large space in memory for my dynamic array?

From Dev

Allocating memory for a array of linked lists

From Dev

Allocating memory for global multidimensional array

From Dev

Allocating memory for array of struct in fgets

From Dev

Passing a pointer to an array and allocating memory

From Dev

When dynamically allocating memory for a list of strings that will be put in an array, is it possible to add on to that list later in the program?

From Dev

Problems with allocating memory for a matrix in c

From Dev

Allocating memory in C for a Fortran allocatable

From Dev

In C, memory allocating fails, why?

From Dev

Allocating memory for triple pointer: C

From Dev

Allocating and freeing memory in a loop in C

From Dev

Trouble allocating memory to pointer in c

From Dev

Allocating memory for structure types in C

From Dev

Allocating memory to a char* in C++

From Dev

Check if there is enough memory before allocating byte array

From Dev

Correctly allocating/freeing memory for records in static array

From Dev

Memory management in allocating 2-D array

From Dev

Allocating memory for a 2-dimensional array that is in a structure

From Dev

Allocating memory for a structure array inside another structure

Related Related

  1. 1

    Allocating memory for one dimensional array in C

  2. 2

    Allocating memory to hold an array of structs in C

  3. 3

    C: Array not allocating more memory correctly

  4. 4

    C not allocating memory for array of structs correctly?

  5. 5

    Memory allocating array of structure pointers in c

  6. 6

    allocating memory to an array of string

  7. 7

    Allocating Memory for string in c?

  8. 8

    Allocating memory (C)

  9. 9

    C++ - safety of allocating memory for an array, then returning a pointer to be deleted externally

  10. 10

    C++ allocating large array on heap gives "out of memory exception"

  11. 11

    Why is C++ allocating such a large space in memory for my dynamic array?

  12. 12

    Allocating memory for a array of linked lists

  13. 13

    Allocating memory for global multidimensional array

  14. 14

    Allocating memory for array of struct in fgets

  15. 15

    Passing a pointer to an array and allocating memory

  16. 16

    When dynamically allocating memory for a list of strings that will be put in an array, is it possible to add on to that list later in the program?

  17. 17

    Problems with allocating memory for a matrix in c

  18. 18

    Allocating memory in C for a Fortran allocatable

  19. 19

    In C, memory allocating fails, why?

  20. 20

    Allocating memory for triple pointer: C

  21. 21

    Allocating and freeing memory in a loop in C

  22. 22

    Trouble allocating memory to pointer in c

  23. 23

    Allocating memory for structure types in C

  24. 24

    Allocating memory to a char* in C++

  25. 25

    Check if there is enough memory before allocating byte array

  26. 26

    Correctly allocating/freeing memory for records in static array

  27. 27

    Memory management in allocating 2-D array

  28. 28

    Allocating memory for a 2-dimensional array that is in a structure

  29. 29

    Allocating memory for a structure array inside another structure

HotTag

Archive