I have a problem with allocating memory...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • toktam
    New Member
    • May 2007
    • 10

    I have a problem with allocating memory...

    Hi guys!
    I defined a type like this:

    typedef struct {
    char *key;
    int freq;
    float pr;
    } TableEntry;

    and then I have an array of pointers to this struct:
    TableEntry* hash_table[hash_table_size];

    But it gives an error when I want to allocate memory for this array:

    assert((hash_ta ble = malloc((hash_ta ble_size)*sizeo f(TableEntry))) !=NULL);


    Do I need to allocate some memory for key before allocating memory for struct???and how I can allocate memory for a string without knowing its length???

    Cheers
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by toktam
    Hi guys!
    I defined a type like this:

    typedef struct {
    char *key;
    int freq;
    float pr;
    } TableEntry;

    and then I have an array of pointers to this struct:
    TableEntry* hash_table[hash_table_size];

    But it gives an error when I want to allocate memory for this array:

    assert((hash_ta ble = malloc((hash_ta ble_size)*sizeo f(TableEntry))) !=NULL);


    Do I need to allocate some memory for key before allocating memory for struct???and how I can allocate memory for a string without knowing its length???

    Cheers
    Variable 'hash_table' is an array, i.e. you cannot assign to it. Make it a pointer
    to a pointer of your struct instead:
    Code:
    TableEntry** hash_table;
    kind regards,

    Jos

    Comment

    • toktam
      New Member
      • May 2007
      • 10

      #3
      Originally posted by JosAH
      Variable 'hash_table' is an array, i.e. you cannot assign to it. Make it a pointer
      to a pointer of your struct instead:
      Code:
      TableEntry** hash_table;
      kind regards,

      Jos
      Thanks for that Jos,
      Can I use this type as an array,I mean an array of TableEntry* elements?Can I assign some value to for example hash_table[0] ???

      Comment

      • toktam
        New Member
        • May 2007
        • 10

        #4
        Originally posted by toktam
        Thanks for that Jos,
        Can I use this type as an array,I mean an array of TableEntry* elements?Can I assign some value to for example hash_table[0] ???
        Actually I tried to assign a string to it :

        assert((hash_ta ble[h]->key=malloc(siz eof(strlen(inpu t)+1)))!=NULL);
        strcpy(hash_tab le[h]->key,input);

        it gave me a segmentation fault...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by toktam
          Actually I tried to assign a string to it :

          assert((hash_ta ble[h]->key=malloc(siz eof(strlen(inpu t)+1)))!=NULL);
          strcpy(hash_tab le[h]->key,input);

          it gave me a segmentation fault...
          Sure, you have just allocated room for a bunch of pointers to those structures,
          but you don't have the structures yet. Maybe you want to allocate room for
          a bunch (read: array) of structs (but that is entirely different from what you
          wrote in your original posting).

          kind regards,

          Jos

          Comment

          • toktam
            New Member
            • May 2007
            • 10

            #6
            Originally posted by JosAH
            Sure, you have just allocated room for a bunch of pointers to those structures,
            but you don't have the structures yet. Maybe you want to allocate room for
            a bunch (read: array) of structs (but that is entirely different from what you
            wrote in your original posting).

            kind regards,

            Jos
            Actually I did this:

            typedef struct {
            char *key;
            int freq;
            float pr;
            } TableEntry;

            TableEntry** hash_table;

            assert((hash_ta ble = malloc((hash_ta ble_size)*sizeo f(TableEntry))) !=NULL);
            assert((hash_ta ble[h]->key=malloc(siz eof(strlen(inpu t)+1)))!=NULL);
            strcpy(hash_tab le[h]->key,input);

            But I gave segmentation fault for this lines...
            I have another guestion:
            when we declare hash_table's type as TableEntry** ,can we pass hash_table as an array to a function?

            Thanks alot,

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by toktam
              Actually I did this:

              typedef struct {
              char *key;
              int freq;
              float pr;
              } TableEntry;

              TableEntry** hash_table;

              assert((hash_ta ble = malloc((hash_ta ble_size)*sizeo f(TableEntry))) !=NULL);
              assert((hash_ta ble[h]->key=malloc(siz eof(strlen(inpu t)+1)))!=NULL);
              strcpy(hash_tab le[h]->key,input);

              But I gave segmentation fault for this lines...
              I have another guestion:
              when we declare hash_table's type as TableEntry** ,can we pass hash_table as an array to a function?

              Thanks alot,
              That's not correct for two reasons:

              1) you're allocating room for 'hash_table_siz e' structures

              2) you're treating that memory as an array of pointers. Of course those pointers
              don't point to sensible values. I guess you want this:
              Code:
              TableEntry* hash_table;
              
              assert((hash_table = malloc((hash_table_size)*sizeof(TableEntry)))!=NULL);
              assert((hash_table[h].key=malloc(sizeof(strlen(input)+1)))!=NULL);
              strcpy(hash_table[h].key,input);
              Now you can consider 'hash_table' and array of structures (not pointers).

              kind regards,

              Jos

              Comment

              Working...