Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C Moderated > Re: Need a GURU...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 8 of 9 Topic 1044 of 1134
Post > Topic >>

Re: Need a GURU!!! I'm stuck.

by Joel Yliluoma <bisqwit@[EMAIL PROTECTED] > Mar 8, 2008 at 12:20 PM

On Sun, 17 Feb 2008 12:04:40 -0600 (CST), Chris LaVelle wrote:
> I don't know the easiest way to explain this so, I'm going to give an 
> example of how it is today....and what I'm trying to accomplish.
>
> in the header...
>
> typedef struct XXX_ELEMENT_tag
> {
>    UINT8   num;
>    UINT8   param;
>    UINT16  fre1;
>    INT16   a1;
> } ;
> typedef struct XXX_ELEMENT_tag ELEMENT_T;
>
> #define MAX_ELEMENTS 8
> typedef struct XYZ_S_tag {
>     UINT8     num_elements;
>     UINT8     flag;
>     ELEMENT_T element[MAX_E];    /*** This is the spot ***/
> } XYZ_S_tag;
>
> And of course the S_tag structure is buried one more structure down. 
> Say something like:
>
> typedef struct T_TABLE_tag
> {
>    XYZ_S_tag run;
>    XYZ_S_tag walk;
>    ....  /* alot of these */
>    XYZ_S_tag crawl;
> } TABLE_T;
> typedef struct T_TABLE_tag T_table;
>
> And in a C file a global is initialized like this...
> T_table t_table =
> {
>         { /* run      */
>                 2, /* Num Elements */
>                 APPLY_WINDOW, /* Flag */
>                 {
>                         /*        N  p   f     a */
>                         /* 0 */ { 2, 0, 480, -240 },
>                         /* 1 */ { 0, 0,   0, 500 },
>                         /* 2 */ { 0, 0,   0, 0 },
>                         /* 3 */ { 0, 0,   0, 0 },	
>                         /* 4 */ { 0, 0,   0, 0 },
>                         /* 5 */ { 0, 0,   0, 0 },
>                         /* 6 */ { 0, 0,   0, 0 },
>                         /* 7 */ { 0, 0,   0, 0 },
>                         /* 8 */ { 0, 0,   0, 0 },
>                 }
>         },
>         { /* walk  */
>   			/* repeat many many times */
>                 }
>         }
> }
>
> Ok, let's just say element is bigger than my example above and there are

> about a 1000 of these, only half of which have data.  I've inherited 
> this code and this table is occupying 300k of memory and memory's tight.
> I want to delete all the filler rows and declare element in a way that 
> the compiler will auto size what it needs for each instance. I have the 
> number of elements.  I would have thought something along the lines of 
> *element instead of element[MAX_E].  But I get warnings about the 
> unexpected brace for element.  Is there a way to make the compiler auto 
> size the space for me?  In the above example I would be deleting rows
2-8.

You can avoid your problem by creating separate tables and using pointers.

Like this:

typedef struct XXX_ELEMENT_tag
{
   UINT8   num, param;
   UINT16  fre1;
   INT16   a1;
} ELEMENT_t;

typedef struct XYZ_S_tag
{
    UINT8 num_elements, flag;
    const ELEMENT_T* element;
} XYZ_S_tag;

typedef struct T_TABLE_tag
{
    XYZ_S_tag run, walk, crawl;
} TABLE_T;
typedef struct T_TABLE_tag T_table;

extern const T_table t_table;


/* And in the C file: */

static const ELEMENT_t run_elements[] =
{
    /*        N  p   f     a */
    /* 0 */ { 2, 0, 480, -240 },
    /* 1 */ { 0, 0,   0, 500 }
};
static const ELEMENT_t walk_elements[] =
{
};
static const ELEMENT_t crawl_elements[] =
{
};

const T_table t_table = 
{
    { /* run */
      sizeof(run_elements)/sizeof(*run_elements), /* autom. count */
      APPLY_WINDOW,
      run_elements
    },
    { /* walk */
      sizeof(walk_elements)/sizeof(*walk_elements), /* autom. count */
      APPLY_WINDOW,
      walk_elements
    },
    { /* crawl */
      sizeof(crawl_elements)/sizeof(*crawl_elements), /* autom. count */
      APPLY_WINDOW,
      crawl_elements
    }
};

Note that as a side effect, this avoids the issue of
"maximum lengths" entirely. Of course, you will need
to be careful to not overflow the 8-bit num_elements
variable... But your compiler should warn you if such
becomes an issue anyway.

Note that I'm using "const" here to indicate data that will
not changed at runtime. On many platforms, it will produce
potentially more efficient code.

Also note the word "static", which is used to indicate data
that is local to that particular module (the name "run_elements"
does not need to be accessible to external modules); doing
so will speed up linking, among other benefits.

-- 
Joel Yliluoma - http://iki.fi/bisqwit/
-- 
comp.lang.c.moderated - moderation address: clcm@[EMAIL PROTECTED]
 -- you must
have an appropriate newsgroups line in your header for your mail to be
seen,
or the newsgroup name in square brackets in the subject line.  Sorry.
 




 9 Posts in Topic:
Need a GURU!!! I'm stuck.
Chris LaVelle <cjlavel  2008-02-17 12:04:40 
Re: Need a GURU!!! I'm stuck.
=?ISO-8859-1?Q?Hans-Bernh  2008-03-08 12:19:59 
Re: Need a GURU!!! I'm stuck.
Keith Thompson <kst-u@  2008-03-17 15:18:58 
Re: Need a GURU!!! I'm stuck.
=?UTF-8?B?SGFucy1CZXJuaGF  2008-03-20 01:09:51 
Re: Need a GURU!!! I'm stuck.
Barry Schwarz <schwarz  2008-03-08 12:20:10 
Re: Need a GURU!!! I'm stuck.
David Thompson <dave.t  2008-03-17 15:23:28 
Re: Need a GURU!!! I'm stuck.
Carl Barron <cbarron41  2008-03-08 12:20:22 
Re: Need a GURU!!! I'm stuck.
Joel Yliluoma <bisqwit  2008-03-08 12:20:33 
Re: Need a GURU!!! I'm stuck.
hnarkaytis@[EMAIL PROTECT  2008-03-08 12:21:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 15:51:11 CDT 2008.