Re: [cc65] Another wish: Unsized Arrays in Structures

Date view Thread view Subject view

From: Christian Krüger (christian.krueger_at_pace.de)
Date: 2003-02-01 16:05:45


Hi,

>legalize a common practice that is also used frequently in the cc65
>compiler
>sources:
>
>        struct foo {
>            int x, y;
>            char name[1];
>        };
>
>        ...
>        struct foo* f = malloc (sizeof (foo) + strlen (name));
>        ...

I my case, this is a kind of overhead. Since I'm working in a
single task/program environment (game console) I have no use
for dynamic memory mangement and like to save the memory/cycles
which is/are needed for 'malloc/free'. I think that many embedded
applications also dislike this overhead when all resources are
'defined' at compile time...
And remember: I like to asign *static* data! When I have to allocate
this buffer dynamicly I also have to copy my static data into this
buffer 'dynamicly' (costs cycles & code...).

>The difference is that the standard does not allow initialization for
>flexible
>array members, while Microsoft allows this.

Yepp, that's my point!

>I'm still not decided how useful this feature actually is. I've been
>using
>structures with an "open" array myself, but always in conjunction with
>dynamic memory.

See above. I would be very unhappy if I have to include 'stdlib' only
for this usecase. In fact, currently I helped myself with some crude
casts... 

>But how useful is this additional Microsoft feature of allowing
>initialization? Especially since it introduces some problems. One is
>that the
>sizeof operator does no longer return the actual size of the variable:

In your case (with dynamic memory) 'sizeof' delivers also an 'untrue'
value, taking your array allways with one byte into account...

>Another problem is, that one can initialize single struct instances, but
>not
>arrays of structs:

Won't work with dynamic allocation either...

>Can anyone post an example where the additional initialization is not
>only
>convenient, but helps to to something not possible before, or were the
>alternative, standard compliant code is a lot more complex and/or ugly?

It's not only convenience. As I wrote above, I won't use dynamic
memory, so I helped myself with ugly casts and initialize the data
in ca65 and let cc65 point on it, telling that this is the structure.

The way of defining an unsized array seems much more 'portable' than
relying upon the allocation principle of static data by a
specific compilier:

--8<----

struct foo = {10,20,'T'};
char addtionalData[] = "his is the name";

--8<----

...hoping that the compiler will put the data exactly in this
sequence into memory.

Maybe you know a solution of defining static data of various length
which is accessible by a structure, but I don't know how to handle
this... (any solution would be better than none..)

Regards
chrisker
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-02-01 16:06:27 CET