Strange code behaviour?

Posted by goldenmean on Stack Overflow See other posts from Stack Overflow or by goldenmean
Published on 2010-03-31T08:10:36Z Indexed on 2010/03/31 8:13 UTC
Read the original article Hit count: 161

Filed under:
|
|
|

Hi,

I have a C code in which i have a structure declaration which has an array of int[576] declared in it.

For some reason, i had to remove this array from the structure, So i replaced this array with a pointer as int *ptr; declared some global array of same type, somewhere else in the code, and initialized this pointer by assigning the global array to this pointer. So i did not have to change the way i was accessing this array, from other parts of my code.

But it works fine/gives desired output when i have the array declared in the structure, but it gives junk output when i declare it as a pointer in the structure and assign a global array to this pointer, as a part of the pointer initialization.

All this code is being run on MS-VC 6.0/Windows setup/Intel-x86.

I tried below things:

1)Suspected structure padding/alignment but could not get any leads? If at all structure alignment could be a culprit how can i proceed to narrow it down and confirm it?

2) I have made sure that in both cases the array is initialized to some default values, say 0 before its first use, and its not being used before initialization.

3)I tried using global array as well as malloc based memory for this newly declared array. Same result, junk output.

Am i missing something?

How can i zero down the problem. Any pointers would be helpful.

Thanks,

-AD.

© Stack Overflow or respective owner

Related posts about c

    Related posts about data-structures