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: Promotions ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 8 Topic 1076 of 1133
Post > Topic >>

Re: Promotions while delivering parameters

by =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@[EMAIL PROTECTED] > Apr 16, 2008 at 04:56 PM

Magi Su <magisu@[EMAIL PROTECTED]
> writes:
> I am a student learning Expert C Programming.
> One interesting topic I have found is that, which the book told me,
> when we use K&R style declaration and definition of functions:
>
> int kandr();
> ....
> int kandr(p1,p2)
>  int p1; char p2;
> {}
>
> the 'char' parameter p2 is promoted to 'int' while pu****ng into stack;
> however, when we use ANSI style:
>
> int ansi(int p1,char p2)
> {}
>
> the parameter p2 is pushed as 'char'.

The C standard says absolutely nothing about how function parameters are
stored on the stack.  In fact, it says nothing about a stack, period.
The exact mechanism by which parameters are passed to functions is left
to the implementation, and *will* vary from one processor architecture
to another, one operating system to another, and even (in extreme cases)
one compiler to another.

I would expect a lecturer or textbook in "Expert C Programming" to know
and teach this.

I suspect you're not getting your money's worth.

Get a copy of the C standard and read up about the difference between
the language and the implementation, and about alignment and padding,
which is the closest you'll get to the issue you're asking about.

Here's one place you can get a cheap copy of the standard:

http://www.amazon.co.uk/C-Standard-British-Standards-Institute/dp/0470845732

> From the selected part of assemble file, I found that both function
call,
> fun1 and fun2, have promoted the 'char' to 'int'. Could anyone explain
why?

This is an implementation detail, and therefore completely outside the
scope of this newsgroup.  However, I can tell you this much: most modern
processors can't perform unaligned accesses efficiently, and some can't
perform them at all, so most compilers will align everything on word
boundaries (usually 32 or 64 bits), although they may clump multiple
less-than-word-sized variables together.  Try the above again, but with
*two* char parameters in addition to the int parameter, and you may very
well find that the two chars are stored consecutively on the stack,
within a single word.

DES
-- 
Dag-Erling Smørgrav - des@[EMAIL PROTECTED]
 
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.
 




 8 Posts in Topic:
Promotions while delivering parameters
Magi Su <magisu@[EMAIL  2008-04-13 02:41:49 
Re: Promotions while delivering parameters
Francis Glassborow <fr  2008-04-16 16:56:02 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-18 16:11:36 
Re: Promotions while delivering parameters
Francis Glassborow <fr  2008-04-21 13:20:21 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-16 16:56:33 
Re: Promotions while delivering parameters
Brian Inglis <Brian.In  2008-04-18 16:11:55 
Re: Promotions while delivering parameters
Kenneth Brody <kenbrod  2008-04-16 16:56:45 
Re: Promotions while delivering parameters
=?utf-8?Q?Dag-Erling_Sm=C  2008-04-18 16:11:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Thu Jul 24 2:33:23 CDT 2008.