C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 4th, 2012, 11:17 PM
jack_ly jack_ly is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 jack_ly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 12 sec
Reputation 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.

Reply With Quote
  #2  
Old September 5th, 2012, 12:37 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 50 m 19 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old September 5th, 2012, 03:26 AM
jack_ly jack_ly is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 jack_ly User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 12 sec
Reputation 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 : '}'

Reply With Quote
  #4  
Old September 5th, 2012, 04:12 AM
salem's Avatar
salem salem is online now
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 50 m 19 sec
Reputation Power: 1774
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.

Reply With Quote
  #5  
Old September 8th, 2012, 03:22 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Compiling Function macros on CC Compilers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap