Issue with function declarations when using register based parameter passing

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Issue with function declarations when using register based parameter passing

ソリューションへジャンプ
1,998件の閲覧回数
vier_kuifjes
Senior Contributor I

I managed to convert my Coldfire Lite based applications to work correctly when compiled with the new CodeWarrior 7.2. All it actually took was to modify the assembler routines to use register based parameter passing.

 

I first had an issue with the UDP server in my app, but this was caused by an incorrect declaration of a function. The actual error there was that I forgot to assign a type to the parameters. Instead of writing...

 

 

void process_udp_packet(ip_addr host_ip, char *data, unsigned int data_len)
{
...
}

 

 

...I wrote...

 

 

void process_udp_packet( host_ip, data, data_len )
{
...
}

 

I did not use a function prototype for this function.

 

The odd thing is, this worked fine with stack-based parameter passing. The second odd thing is that the compiler did not throw an error or a warning when compiling this.

 

Is this normal behaviour for a C compiler? Is it possible to enable flagging of this kind of omissions?

ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
912件の閲覧回数
bkatt
Contributor IV

"data" without the "char *" is assumed to have type int. 32-bit pointers and ints are handled the same with stack based parameter passing, but differently with register based parameters (pointers use the address registers, non-pointers use the data registers).

 

All modern code should require function prototypes and prohibit defaulting to int. Compilers may have specific options for these settings, or you may have specify a "c99" mode. The "normal" behavior that does not enforce these rules is specified by the c90 standard for compatibility with really old K&R style code. 

 

If you are not careful with declarations and header files, you can still get into trouble with different declarations for the same function.

元の投稿で解決策を見る

0 件の賞賛
返信
4 返答(返信)
913件の閲覧回数
bkatt
Contributor IV

"data" without the "char *" is assumed to have type int. 32-bit pointers and ints are handled the same with stack based parameter passing, but differently with register based parameters (pointers use the address registers, non-pointers use the data registers).

 

All modern code should require function prototypes and prohibit defaulting to int. Compilers may have specific options for these settings, or you may have specify a "c99" mode. The "normal" behavior that does not enforce these rules is specified by the c90 standard for compatibility with really old K&R style code. 

 

If you are not careful with declarations and header files, you can still get into trouble with different declarations for the same function.

0 件の賞賛
返信
912件の閲覧回数
vier_kuifjes
Senior Contributor I

Thanks, I learned something new again! I found a checkbox "enable C99 extensions"  in the project settings and now the compiler throws an error when the parameter type is not declared.

 

Apparently the "bug" was already there in the Coldfire Lite sample code (file freescale_UDP_server.c), on which my code is based. So it appears that I'm not the one to be blamed! :smileywink:

 

I guess I really need a good resource (book) about C programming. There's a lot I don't know yet. All I know about C programming I learned by myself, which means a lot of info is missing...

 

Any suggestions on a good book?

0 件の賞賛
返信
912件の閲覧回数
jbezem
Contributor III

Since you seem to know already quite a lot, get the latest edition of H&S (5th ed.):

" C A Reference Manual", Samuel P. Harbison, Guy L. Steele Jr, ISBN 0-13-089592X, on Amazon: http://www.amzn.com/013089592X

Written by "frustrated" compiler-writers fed-up with the uncharted areas of C in the 80-ies, it covers most if not all dark corners of the language, the fifth edition covering the Standard up to C99, as well as many deviations commonly adopted by various compilers.

I haven't found any real but uncovered issue with it yet. Strongly recommended, but not for a beginner.

 

HTH,

 

Johan

0 件の賞賛
返信
912件の閲覧回数
vier_kuifjes
Senior Contributor I
Thanks!
0 件の賞賛
返信