2011-04-06 08:11 AM
Keil and structure pointers
2011-05-17 05:30 AM
2011-05-17 05:30 AM
''it fact its working''
Well, it might just happen not to fail in this case - but you really mustn't make a habit of casting pointers like this! If you have a pointer to some unaligned item, and cast it to something that needs to be aligned, then your program will crash.
2011-05-17 05:30 AM
Just to note that this has nothing specifically to do with Keil, nor even ARM - it is a general issue related to any architecture with a multiple-byte word size.
2011-05-17 05:30 AM
2011-05-17 05:30 AM
''since my structure is built of elements of the same type''
It has nothing to do with them being the same type. What matters is that they are all types with no alignment restrictions. ''so I dont think that would generate big problem''
Not in this case, true; but it is purely by luck - not by design!''right now everthing is ok''
Not really!
Again, it's only working by luck - not by judgement! You have a dangerous trap waiting there to trip you up...2011-05-17 05:30 AM
Thank you neil for you advice , I used to use this kind of casting before , but till now I did not get anyproblem , I get problems when using different kinds of types in the structure and that due to the paddings,
let me explain you my case: I have a structure of 200 bytes , I want to piont the structure to a buffer of 200 byte , that is , is there any better solution ? Casting look for me the best solution right now. Regards2011-05-17 05:30 AM
''I used to use this kind of casting before , but till now I did not get anyproblem''
Exactly! Up until now, you have just happened to be lucky - but, one day, your luck will run out.''is there any better solution ?''You haven't said what the problem is yet - so it's impossible to say anything about solutions.
What, exactly, is it that you are actually trying to achieve here?
2011-05-17 05:30 AM
''is there any better solution ?''
Read the elements of your structure one by one from your buffer and place them into your structure. If you read the elements ''byte by byte'' you can also negate the effect of endianness and therefore end up with somthing that is portable accross differient processors with differient endianness and differient padding formats. Other approaches include sending the structure as ASCII which again does not suffer from endianness or padding problems. Many other approaches exist. This sort of thing is often refered to as serialization. See here