By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

How can we reinitialize a array of structure?

P: 11
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.
Jun 8 '07 #1
Share this Question
Share on Google+
4 Replies


100+
P: 256
I would use memset() or bzero() - both will allow you to clear all data from your structure.
Jun 8 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This si going to cause you trouble:

Expand|Select|Wrap|Line Numbers
  1. struct out_rectype out_record[50000];
  2.  
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.
Jun 8 '07 #3

P: 11
This si going to cause you trouble:

Expand|Select|Wrap|Line Numbers
  1. struct out_rectype out_record[50000];
  2.  
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
Jun 12 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jun 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.