I have strange program behavior while trying to read a value from a structure after assigning a value to it. I’m showing the related structure and function below:
/*Data struct for cor_entry */
struct cor_entry {
struct cor_entry * pre_entry;
struct cor_entry * next_entry;
long long unsigned int entry_data;
};
I’ve commented out most of my function to highlight the problem:
/* update correlation table */
void cor_table_update(long long unsigned int cor_table_data,
struct cor_entry **cor_table_head_ptr,
struct cor_entry **cor_table_tail_ptr,
int *entry_num,
const int MAX_NUM)
{
struct cor_entry *cor_table_entry;
int cor_hit=0;
//test code
//cor_table_head=cor_table_tail=(struct cor_entry*)calloc(1, sizeof(struct cor_entry));
//printf("original cor_entry_num=%d\n",*entry_num);
////////////////////////code for test///////////////////////////////
cor_table_entry=(struct cor_entry*)calloc(1, sizeof(struct cor_entry));
printf("The cor_table_entry=%x\n",cor_table_entry);
cor_table_entry->entry_data=cor_table_data;
if (cor_table_entry->entry_data==cor_table_data)
{
printf("The assignment is correct!\n");
printf("the cor_enrty_data=%x, stored data=%x,\n",
cor_table_data,
cor_table_entry->entry_data);
}
// ... rest of function
}
And I get this output while running the program:
The cor_table_entry=8c09a58 The assignment is correct! the cor_enrty_data=8ffc8, stored data=0, The cor_table_entry=8c09a70 The assignment is correct! the cor_enrty_data=8ffc8, stored data=0, The cor_table_entry=8c09a88 The assignment is correct! the cor_enrty_data=8ffc8, stored data=0, The cor_table_entry=8c09ae8
Could someone shed some light on this problem? I’m using the GCC-3.4.6 compiler.
Try to compile with
-Wall. GCC should then tell you that the sizes of the%format specifiers and theprintf()arguments don’t match. Try%llxinstead of%x. That should fix the issue.