Re: Polymorphism in C (very basic)

From: Jason Curl (j.curl_at_motorola.com)
Date: 07/09/04


Date: Fri, 9 Jul 2004 12:27:50 +0200

Thanks everybody.

The form used here [*(Float32*)&u32 = 3.14159;] is what I use now.

But for all your curiosity, why am I doing this? I've used C for quite some
time and mostly on embedded systems. This is a kind of flight recorder,
where the data being recorded may change, but the number of data elements to
record is fixed.

The array of Uint32's allows me to define a block of memory that is
allocated at compile time. The index to the array provides a very fast way
to access that index. The datatype isn't important, except for how much
space it takes up, and all of my data is 32-bits or less. Where I need a
string, it's a pointer to another general memory space that contains the
string. However, the most common datatype being used is a Uint32.

So it means I don't use "malloc" on initialisation. It also means in 80% of
the cases I don't need to do type conversions of this sort because the
datatypes are already in some integer form. It also means that components
can access the number of elements, perform a run-time function call to know
how to interpret the data. I can add to the number of elements in one small
area that collects it. Other components can use a small portion that they
need, or they can use everything. This is useful for realtime analysis of
incoming data, as well as storage (where the storage mechanism doesn't need
to know what the datatypes are). Hence, the point of the title, some basic
form of polymorphism in C.

I have a separate array that defines the datatype and it is used to
interpret the datatype at runtime for third party components.

I didn't want to have a solution using a Union construct, as when
initialising this array it means I first need to write to a temporary
variable and then copy that either using memcpy, union structs, or
typecasts. I simply wanted the compiler to take a float, without type
promotion or demotion and store it. Why not have one ASM instruction, than
have three?

The other solution I did think about was to have three arrays of a union
construct, one array of 100 Uint32's, another of 100 Float32's and the third
of 100 char ptrs. From there I could simply access the one I wanted.

Based on the comments it might be the better way to go. But type conversions
would still need to be made for assigning variables of different sizes (e.g.
Int16), especially when extracting the information from the array.

And the comment about the last part? Well..... Now you might know how I
intend to use the data and what it's for.

Cheers,

Jason.

"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message
news:Pine.LNX.4.58-035.0407081251520.5043@unix44.andrew.cmu.edu...
>
> On Thu, 8 Jul 2004, James Hu wrote:
> >
> > On 2004-07-08, Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> wrote:
> > >
> > > *(Float32*)u32 = 3.14159;
> > >
> >
> > Nit:
> > *(Float32*)&u32 = 3.14159;
>
> Whoops... Good catch! Especially considering the context... if the
> OP thought it was a good idea to coerce floats to ints, he might not
> have thought anything unusual about converting an int to a pointer!
>
> Thanks,
> -Arthur
>



Relevant Pages

  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... has to do so to make pointer equality work consistently). ... objects which are not members of some aggregate, ... were a single-element array. ...
    (comp.lang.c)
  • Re: Can I use the SQL "Table" Datatype to pass parameter info from VB?
    ... this datatype. ... could do to pass array safely is to pass it as an XML structure ... > string values from VB (using ADO) to the SQL Server stored procedure. ...
    (microsoft.public.vb.database.ado)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: Arrays of Unions
    ... a Union is a variable that may hold objects ... how can an array hold a union that has ... The size of a union object is the size of its largest member. ... Yes, this means that if you store a char value in "tiny", ...
    (comp.lang.c)