How can we reinitialize a array of structure?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • souravmallik
    New Member
    • May 2007
    • 11

    How can we reinitialize a array of structure?

    I have a structure declared in a header file, and its initialized in the header file too.

    struct rectype{
    char out_storenum[100];
    unsigned int out_usrcode;
    unsigned int out_strngtyp;
    char out_bit0_ind[5];
    char out_bit1_ind[5];
    char out_bit2_ind[5];
    char out_bit3_ind[5];
    char out_bit4_ind[5];
    char out_bit5_ind[5];
    char filename[200];
    };
    struct out_rectype out_record[50000];


    I am using the structure in a loop to insert data. Now when I'm coming back in the loop and inserting data in the structure member, its getting over written. the code is running file, but when the array index is reaching around 40,000 - 45,000 some junk values is setting in the character fields.

    I need to reinitialize the array structure. Can anyone help / advice me..

    Thanks
    Sourav M.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I would use memset() or bzero() - both will allow you to clear all data from your structure.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This si going to cause you trouble:

      [code=cpp]
      struct out_rectype out_record[50000];
      [/code]

      Each element appears to be 338 bytes and you have 50000 of them for 16.9MB of memory. This is stack memory which is often limited to 4096 bytes. You may be exceeding a limit here and that's producing the indeterminate results.

      I recommend a) you allocate the array on the heap and b) you allocate each member of the struct on the heap.

      Comment

      • souravmallik
        New Member
        • May 2007
        • 11

        #4
        Originally posted by weaknessforcats
        This si going to cause you trouble:

        [code=cpp]
        struct out_rectype out_record[50000];
        [/code]

        Each element appears to be 338 bytes and you have 50000 of them for 16.9MB of memory. This is stack memory which is often limited to 4096 bytes. You may be exceeding a limit here and that's producing the indeterminate results.

        I recommend a) you allocate the array on the heap and b) you allocate each member of the struct on the heap.
        Thanks.. for the advice.

        According you guys... I should declare a pointer to the structure and allocate memory with malloc(). This will allocate memory in a heap.. Right.

        Is my understanding right..please advice...

        Thanks in advance..
        Sourav

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by souravmallik
          According you guys... I should declare a pointer to the structure and allocate memory with malloc(). This will allocate memory in a heap.. Right.

          Is my understanding right..please advice...
          That's correct. Avoid stack variables.

          Comment

          Working...