Dwayne Dietrich

c syntax: pointer type cast

Discussion created by Dwayne Dietrich Employee on Mar 31, 2006
This message contains an entire topic ported from the WildRice - Coldfire forum.  Freescale has received the approval from the WildRice administrator on seeding the Freescale forum with messages.  The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value as you search for answers to your questions.  Freescale assumes no responsibility whatsoever with respect to Posted Material.  For additional information, please see the Terms of Use - Message Boards and Community Forums.  Thank You and Enjoy the Forum!


Jan 21, 2006, 6:19 AM
Post #1 of 4 (82 views)
Copy Shortcut
 [ColdFire] c syntax: pointer type cast  Can't Post 
--------------------------------------------------------------------------------
 
let us assume i utilize "fake" long variables, indeed pointing to data.
(i do not have access to c compiler at every system/it crashes on some windows versions by random)
long src_data;
long tags;
long dst_buffer;
now i would like to compare two longwords:
if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}
then i store a result:
(*(ULONG *)dst_buffer)=data;
and then i go to the next longword data item:
(ULONG *)dst_buffer++;
or the datasize gets added:
dst_buffer+=sizeof(struct Image); /* size of data structure */
dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary */
it's the easiest way, because i utilize arrays pointing to memory area's.
in example while they are initialized, different data gets copied in, and each time the data buffer gets padded to 16byte boundary. most effectively i do it by just accessing the pointer as "long" variable, or sometimes casting it to "long variable pointer" (ULONG *).
now my question: are the above c expressions correct, or can i write them in a better/more simple form?
 

---------------------------------
Jan 22, 2006, 3:49 PM
Post #2 of 4 (78 views)
Copy Shortcut
 Re: [ColdFire] c syntax: pointer type cast [In reply to]  Can't Post 
--------------------------------------------------------------------------------
 
alex wrote:
> let us assume i utilize "fake" long variables, indeed pointing to
> data. (i do not have access to c compiler at every system/it crashes
> on some windows versions by random)
>
> long src_data; long tags; long dst_buffer;
>
> now i would like to compare two longwords: if( (*(UBYTE
> *)src_data)==(*(UBYTE *)tags) ) {do_something();}
That statement is comparing two bytes.
>
> then i store a result: (*(ULONG *)dst_buffer)=data;
>
> and then i go to the next longword data item: (ULONG *)dst_buffer++;
So far so good... but the casting is pretty ugly!
If you have access to it on your system I would
recommend the new C99 types uint8_t and uint32_t
> or the datasize gets added: dst_buffer+=sizeof(struct Image); /*
> size of data structure */
WRONG!!!...
this will increment dst_buffer by
sizeof(long) * sizeof(struct Image )

> dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary
> */
As above.
> it's the easiest way, because i utilize arrays pointing to memory
> area's. in example while they are initialized, different data gets
> copied in, and each time the data buffer gets padded to 16byte
> boundary. most effectively i do it by just accessing the pointer as
> "long" variable, or sometimes casting it to "long variable pointer"
> (ULONG *).
I would do it like this (but tastes do vary).
uint32_t *dst;
uint32_t *src;
uint8_t * u8dst = (uint8_t * ) dst;
uint8_t * u8src = (uint8_t * ) src;
if ( *u8dst == *u8src ) do_something();
if you want to increment the long pointer by the sizeof a struct
you need to be careful.
dst += sizeof(struct Image ) / sizeof(uint32_t ) ;
Regards
Paul
=========================================================================
Jan 23, 2006, 1:53 AM
Post #3 of 4 (78 views)
Copy Shortcut
 Re: [ColdFire] c syntax: pointer type cast [In reply to]  Can't Post 
--------------------------------------------------------------------------------
 
Paul Whitfield wrote: alex wrote:
> let us assume i utilize "fake" long variables, indeed pointing to
> data. (i do not have access to c compiler at every system/it crashes
> on some windows versions by random)
>
> long src_data; long tags; long dst_buffer;
>
> now i would like to compare two longwords: if( (*(UBYTE
> *)src_data)==(*(UBYTE *)tags) ) {do_something();}
That statement is comparing two bytes.
>
> then i store a result: (*(ULONG *)dst_buffer)=data;
>
> and then i go to the next longword data item: (ULONG *)dst_buffer++;
So far so good... but the casting is pretty ugly!
If you have access to it on your system I would
recommend the new C99 types uint8_t and uint32_t
> or the datasize gets added: dst_buffer+=sizeof(struct Image); /*
> size of data structure */
WRONG!!!...
this will increment dst_buffer by
sizeof(long) * sizeof(struct Image )

> dst_buffer=alignchunky(dst_buffer); /* align to next 16byte boundary
> */
As above.
> it's the easiest way, because i utilize arrays pointing to memory
> area's. in example while they are initialized, different data gets
> copied in, and each time the data buffer gets padded to 16byte
> boundary. most effectively i do it by just accessing the pointer as
> "long" variable, or sometimes casting it to "long variable pointer"
> (ULONG *).
I would do it like this (but tastes do vary).
uint32_t *dst;
uint32_t *src;
uint8_t * u8dst = (uint8_t * ) dst;
uint8_t * u8src = (uint8_t * ) src;
if ( *u8dst == *u8src ) do_something();
if you want to increment the long pointer by the sizeof a struct
you need to be careful.
dst += sizeof(struct Image ) / sizeof(uint32_t ) ;
Regards
Paul
=========================================================================
The information in this e-mail is intended for the addressee only.
Unauthorised use, copying, disclosure or distribution by anyone else
is prohibited. Please let us know immediately if you receive this
e-mail in error. Thank you.
=========================================================================
--------------------------------------------------------------------

(UBYTE *)chipmembuffer2+=dsize; /* long chipmembuffer2 */
this works (it compiles, links and executes)
thought i can leave it out (the c compiler does not know its a pointer)
by the way its a pretty old one. what happens if i compile for ms-dos/windows?
here i store linear 32bit address into long, and treat it as pointer (by doing typecast). anyway its not executeable under ms-dos (addressed hardware not existent), just asking if a c compiler generates a (water resistent) segment:smileysurprised:ffset addressing layer. (like borland pascal always normalizes to "segment only" pointers by own device, but accepts changes i.e. add to the offset). i assume each time i cast to a pointer type the compiler generates an addressing layer (which may vary from system to system)
now the reason for NOT using a derived datatype: the values are freely passed around to various functions, stored into data structures for later processing. then i would need to cast to long, and would need to take care to pass the right datatype to functions. probably its oldfashioned to treat pointers just as "long" values.
(alignchunky() makes sense for any memory address, it does not depend on the datatype. the system requires "even" alignment, and even "longword" alignment for some I/O data structures. in this case i need to declare the datatype, and to de-cast it each time i pass it to functions like alignchunky().)
one answer i found myself: not to mix various addressing methods/cast systems. even if the code size increases and execution slows down.
alex finn, dublin, ireland. electronic games and effective design.
information about superstition: http://uk.geocities.com/nikemoto2511
 
 
Jan 24, 2006, 1:42 AM
Post #4 of 4 (76 views)
Copy Shortcut
 Re: [ColdFire] c syntax: pointer type cast [In reply to]  Can't Post 
--------------------------------------------------------------------------------
 
regards, however i have put in "long" for illustration purpose.
lthe way you state it: that's how it WAS.
but i can see now i labelled/commented it wrong:
>now i would like to compare two UBYTE values:
> if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}
if it does not work correctly (inc/dec numeric variable), i need to add additional
casts (they do work, i have tested it). some documents explain that "pointer variables get increased by the datasize". however its difficult/interruptive for me to compile/test/debug it right now, hence i ask...
the background are arrays of pointers to structures which: contain pointers to others structures, memory areas and so on. it just does not make sense to make up highly specialized datatypes/pointer types, and then downcast them just to add a numerical value.
if an address (pointer=address representation) gets aligned to EVEN, 4byte,... it has nothing to do with the type of data, its just a numerical thing.
nikemoto2511
--------------------------------------------------------------------------------------------------------------

Scott Hauck <haucks@nextnetwireless.com> wrote:
I see someone already replied to you, but I have just a couple more points.
> now i would like to compare two longwords:
> if( (*(UBYTE *)src_data)==(*(UBYTE *)tags) ) {do_something();}
src_data and tags are already of type long so if you wish
to compare longwords, then "(src_data == tags)" would do it
for you. The statement you have isn't just a byte compare,
but you are casting src_data and tags as pointers to bytes.
Therefore you statement is comparing the byte information
that is being pointed at by src_data and tags. Probably not
what you want...
> then i store a result:
> (*(ULONG *)dst_buffer)=data;
Another pointer problem here. dst_buffer is a long and you
have cast it to a pointer to an 'unsigned long'. So when you
store the data, you are actually storing it in a location
pointed to by dst_buffer.
> and then i go to the next longword data item:
> (ULONG *)dst_buffer++;
You don't need the cast. Just "dst_buffer++" is sufficient.
> or the datasize gets added:
> dst_buffer+=sizeof(struct Image); /* size of data structure */
The other responder was correct. When you increment a variable
it gets incremented by the size of its type. Likewise, when
adding to a variable, it gets whatever is being added *times*
the size of the receiving variable.
Pointers are a somewhat confusing element of C. In fact,
it is hardly even taught in school anymore. If you truly
want these things to be pointers, then declare them that way
instead of constantly having to cast them.
long *src_data;
long *tags;
long *dst_buffer;
# compare 2 longwords
if (*src_data == *dst_buffer) {do_something();}
# then store a result:
*dst_buffer = data;
# and then i go to the next longword data item:
dst_buffer++;

-Scott
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 
 
 

Message Edited by Dietrich on 04-01-2006 11:34 AM

Message Edited by Dietrich on 04-04-2006 09:33 PM

Outcomes