#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Compiling Function macros on CC Compilers


    Hi,
    The following piece of code was successfully compiled on gcc compiler,but failed on cc compiler

    #include <stdio.h>
    typedef unsigned char uch;
    typedef unsigned short ush;
    typedef unsigned long ulg;

    static uch *inbuf;
    static unsigned insize;
    static unsigned inptr;

    char input_data[];
    char input_data_end[];

    #define NEXTBYTE() ({ int v = get_byte(); (uch)v; })

    #define NEEDBITS(n) {while(k<(n)){b|=((ulg)NEXTBYTE())<<k;k+=8;}}


    #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())

    static int fill_inbuf(void);

    int main()
    {
    unsigned int k;
    unsigned long b;
    unsigned int e;
    NEEDBITS(e)
    return 0;
    }




    int fill_inbuf(void)
    {

    if (insize != 0){
    }

    inbuf = input_data;
    insize = &input_data_end[0] - &input_data[0];

    inptr = 1;
    return inbuf[0];
    }

    This may due to the function macro syntax difference between the two.How can i tuned this function macros in order to compile in cc compilers.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    1. You didn't use [code][/code] tags when you posted code (did you read the READ ME FIRST posts?)

    2. Saying "cc" compiler doesn't say much - many compilers are invoked with "cc".
    State your OS and compiler versions - including your gcc version.

    > successfully compiled on gcc compiler,but failed on cc compiler
    And the error messages are....

    This is broken.
    Code:
    char input_data[];
    char input_data_end[];
    ...
    insize = &input_data_end[0] - &input_data[0];
    - Your input arrays have no size.
    - You're assuming an ordering of arrays in memory that makes your calculation deliver a useful result.

    If you had a #define for the array size, you wouldn't need the dubious size calculation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Ok,Actually the stated code was a part of one application,the input_data are other are extern.When i tried to compile the entire application on VC++6.0 ,it show some error.So that i cut a part of code from there,and made some minimum modification and compile on vc++6.0 ,again compilation failed.

    This modified code was posted earlier.I hope this is due to the some mistakes in the function macros.But the same piece of code compiled on ubuntu12.04.

    The compilation error was
    D:\workspace\C++\General\Study\Sample\main.c(27) : error C2059: syntax error : '{'
    D:\workspace\C++\General\Study\Sample\main.c(27) : error C2065: 'v' : undeclared identifier
    D:\workspace\C++\General\Study\Sample\main.c(27) : error C2143: syntax error : missing ';' before ')'
    D:\workspace\C++\General\Study\Sample\main.c(27) : error C2059: syntax error : ')'
    D:\workspace\C++\General\Study\Sample\main.c(28) : error C2059: syntax error : 'return'
    D:\workspace\C++\General\Study\Sample\main.c(29) : error C2059: syntax error : '}'
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    I would suggest you rewrite those macros as proper functions.

    Your macro expands to this.
    {while(k<(e)){b|=((ulg)({ int v = (inptr < insize ? inbuf[inptr++] : fill_inbuf()); (uch)v; }))<<k;k+=8;}}

    Whilst gcc may compile this, it isn't standard C.
    $ gcc -ansi -pedantic foo.c
    foo.c: In function ‘main’:
    foo.c:27:1: warning: ISO C forbids braced-groups within expressions [-pedantic]
    foo.c: In function ‘fill_inbuf’:
    foo.c:37:7: warning: pointer targets in assignment differ in signedness [-Wpointer-sign]

    So other compilers are going to barf over it.

    Unconstrained gcc allows an awful lot of things which are not strictly ANSI/ISO C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    You have to consider how it expands:
    Here for example the cast is invalid:
    Code:
    (ulg)NEXTBYTE()
    since you can only cast an expression and NEXTBYTE() does not expand to an expression but rather a statement block.

    So called "function-like macros" and real functions are not semantically equivalent and should probably be avoided in most cases.

    The simplest and safest solution (and the way it should have been done in the first place) is to re-write the macros as functions.

IMN logo majestic logo threadwatch logo seochat tools logo