Wednesday, December 29, 2004

PODS and variables or standards vs. comfort

I recently ported a small, but working OnboardC project over to PODS 1.0 to benefit from the comfort of the large screen standing on my desktop. According to friendly advice gathered from the developer newsgroup, getting sources and XRD files to the PC was a painless process. After having faced a few minor difficulties at the Windows 2000 command prompt, everything was set. I now expected a painless build cycle - but the app didn't even survive the parser. Parse errors at almost every variable definition. An Update to the latest and greatest version didnt help. Treat this as an example:

switch(var)
{
//Useless code
case foo:
int bar;
//More useless code
}

A parse error occurs immediately when the code above is compiled-and the variable is not accepted. A short post at the user group brought enlightment to the coder-ANSI C requires that variable definitions must be the first code in a block-and this requirement isn't met above.
While some compilers(at least OnBoardC) accept variable definitions anywhere, the PODS(Ben Combee says that this is GCC's fault, as we all known PODS is based on GCC) is picky.
However, ANSI C has a comfortable way of combatting this problem-the creating of instruction blocks. Many C programmers treat the {} as a fixed part of the syntax-but actually they only group instructions together. Each block gets its own stack space-and can thus have lots of local variables. So, just put your code into a block-and alas, the PODS accepts it. The corrected example would look like this:

switch(var)
{
//Useless code
case foo:
{
int bar;
}
//More useless code
}

A possible second way is to change the C file uinto a C++ file by changing its filename to .cpp and then reinserting the file into the project-C++ dors not impose this limitation.

Altough it is difficult to call this 'pedantery' shortcoming or even bug, it can still pull nerves and cause headaches.
Feel free to comment!

P.S.This article isnt published to bash Palmsource. It should just save developer's time. PODS is a cool product in general, but knowing possible traps is always good.
Full disclosure: This article was sent to Ben Combee for correcting before it was published here!

8 Comments:

Blogger Steven Fisher said...

Two comments:

"While most compilers accept variable definitions anywhere..."

Are you sure about that? I've worked with a variety of C compilers, and while C++ compilers will accept variables anywhere, I have never seen this in a C compiler.

"Each block gets its own stack space-and can thus have lots of local variables."

I'm not sure about this, either. Although that's the way it seems to work, I think that at least in Codewarrior only the main function actually adjusts the stack. But it puts space on the stack for subvariables.

Still, you've got my curious about this, and I'm going to see if I can confirm one way or the other!

12:16 AM  
Blogger Steven Fisher said...

I should add:

The primary use of variables within { } is (as far as I know) scope limitation. The variables are not available outside of those brackets. This allows a few clever things.

For instance:

void Foo( int a, int b )
{
  if ( a < b ) {
    int i;
    for ( i = a; i<b; i++ )
    { /* do something here */ }
  } else {
    int j;
    for ( j = b; j<a; j++ )
    { /* do something here */ }
  }
}

In this case, i is not accessible outside of its block. And j is not accessible outside of its block. However, since the scope of i and j are so limited, the compiler is able to actually use the same space on the stack. Instead of this function taking 4 bytes for local variable storage, it will only take 2.

In fact, a compiler with a good optimizer could very likely reuse stack space/registers for i and j regardless of whether they were put in a limited scope like that, so long as the life of the variables is short. Variables in sub-blocks are more useful in C++ than in C, where constructors/destructors get called.

It's really mostly just a tool for making your code easier to understand, like naming your function something other than foo and your parameters something other than a and b.

3:14 AM  
Blogger Tam Hanna said...

Hi Steven,
thanks yet again for the good and informative comments! A C beginner like me can always learn something new here!
However, lets face it:
I didn't want to discuss scope, etc here. I just wanted to get the program working and compiling under PODS. And inserting blocks into each switch statement and moving the variables to the top of the block solved my problem!
BTW, if you want a compiler that CAN run such code, go for OnBoardC! It can definitely do it.
Merry 2005 and thanks for the comments!
Tam Hanna

11:20 AM  
Blogger Junda Xu said...

20151028 junda®
coach factory outlet
nfl jerseys
Gucci Outlet Online Sale
Abercrombie T-Shirts
hollister clothing store
ugg boots
michael kors outlet
Michael Kors Outlet Handbags Factory Price
ugg boots
toms outlet
abercrombie
mont blanc
michael kors outlet
michael kors outlet
Coach Outlet Store Online Shop
canada goose outlet
michael kors outlet
Abercrombie and Fitch Outlet Sale
Louis Vuitton Bags Outlet Store
michael kors bags
Wholesale Authentic Designer Handbags
ugg outlet store
air max 95,nike golf,nike janoski,air max 1,nike canada,nike plus,nike shox,nike factory store
michael kors handbag
Louis Vuitton Handbags For Cheap
cheap ugg boots
Christian Louis Vuitton Red Bottoms
coach outlet store online
true religion outlet
canada goose jackets

4:57 AM  
Blogger oakleyses said...

cheap oakley sunglasses, louis vuitton outlet, uggs on sale, michael kors outlet online, prada outlet, michael kors outlet online, ugg boots, oakley sunglasses, longchamp outlet, oakley sunglasses, oakley sunglasses wholesale, jordan shoes, michael kors outlet online, christian louboutin shoes, michael kors outlet, replica watches, gucci handbags, louis vuitton outlet, uggs outlet, polo outlet, nike free, chanel handbags, michael kors outlet online, christian louboutin outlet, oakley sunglasses, ugg boots, uggs outlet, kate spade outlet, nike air max, nike outlet, longchamp outlet, ray ban sunglasses, louis vuitton outlet, ray ban sunglasses, christian louboutin, longchamp outlet, michael kors outlet, louis vuitton, burberry handbags, nike air max, tiffany jewelry, burberry outlet, polo ralph lauren outlet online, tiffany and co, ray ban sunglasses

2:37 AM  
Blogger oakleyses said...

polo lacoste, burberry pas cher, true religion outlet, hollister uk, ray ban pas cher, replica handbags, ralph lauren uk, nike free uk, michael kors, true religion outlet, north face uk, nike roshe, sac longchamp pas cher, timberland pas cher, longchamp pas cher, coach outlet, mulberry uk, nike blazer pas cher, kate spade, nike air max, true religion jeans, true religion outlet, lululemon canada, nike free run, ray ban uk, nike roshe run uk, converse pas cher, guess pas cher, hollister pas cher, nike tn, michael kors pas cher, oakley pas cher, nike air max uk, nike air force, sac vanessa bruno, jordan pas cher, new balance, north face, coach purses, vans pas cher, nike air max uk, sac hermes, hogan outlet, louboutin pas cher, air max, michael kors, michael kors outlet, abercrombie and fitch uk, coach outlet store online, polo ralph lauren

2:39 AM  
Blogger oakleyses said...

iphone cases, ipad cases, iphone 6 plus cases, bottega veneta, chi flat iron, louboutin, iphone 6s cases, insanity workout, nike roshe run, babyliss, hollister, p90x workout, vans outlet, s6 case, lululemon, mac cosmetics, jimmy choo outlet, hollister clothing, nfl jerseys, north face outlet, nike huaraches, oakley, soccer jerseys, nike trainers uk, instyler, baseball bats, mcm handbags, mont blanc pens, herve leger, wedding dresses, ferragamo shoes, north face outlet, giuseppe zanotti outlet, timberland boots, ghd hair, ralph lauren, soccer shoes, iphone 5s cases, abercrombie and fitch, hermes belt, iphone 6s plus cases, valentino shoes, celine handbags, beats by dre, longchamp uk, new balance shoes, nike air max, reebok outlet, iphone 6 cases

2:40 AM  
Blogger oakleyses said...

wedding dresses, moncler outlet, pandora jewelry, toms shoes, converse, louis vuitton, ugg uk, ray ban, moncler, links of london, nike air max, marc jacobs, karen millen uk, moncler outlet, canada goose uk, canada goose outlet, ugg pas cher, juicy couture outlet, pandora jewelry, lancel, barbour, canada goose, hollister, swarovski, louis vuitton, hollister, doudoune moncler, canada goose outlet, louis vuitton, swarovski crystal, converse outlet, montre pas cher, moncler uk, coach outlet, thomas sabo, canada goose, canada goose, ugg,ugg australia,ugg italia, louis vuitton, vans, moncler, ugg, louis vuitton, barbour uk, moncler, pandora uk, replica watches, canada goose outlet, gucci, doke gabbana, pandora charms, supra shoes, moncler

2:42 AM  

Post a Comment

<< Home