cancel
Showing results for 
Search instead for 
Did you mean: 

Keil and structure pointers

samiassaad
Associate II
Posted on April 06, 2011 at 17:11

Keil and structure pointers

17 REPLIES 17
samiassaad
Associate II
Posted on May 17, 2011 at 14:30

Thank you all again ; it fact its working but , it was debuger fualt, the debuger shows wrong values ! (ULINK2 uv4 ) . I pint the values in the LCD and everything is ok as expected .

Thank you all and sorry for disturb , should not I trust the debuger ? should I?

Andrew Neil
Evangelist III
Posted on May 17, 2011 at 14:30

''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.
Andrew Neil
Evangelist III
Posted on May 17, 2011 at 14:30

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.

samiassaad
Associate II
Posted on May 17, 2011 at 14:30

Thank you Neil , I will takecare when casting, I read about padding , now I understand it , since my structure is built of elements of the same type , so I dont think that would generate big problem , right now everthing is ok .

Regards

Andrew Neil
Evangelist III
Posted on May 17, 2011 at 14:30

''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...
samiassaad
Associate II
Posted on May 17, 2011 at 14:30

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.

Regards

Andrew Neil
Evangelist III
Posted on May 17, 2011 at 14:30

''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?

trevor23
Associate III
Posted on May 17, 2011 at 14:30

''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 

http://en.wikipedia.org/wiki/Serialization