Solved

What is the default storage class type for function arguments?  See my claims in the question body

Posted on 2004-10-07
8
346 Views
Last Modified: 2013-11-15
In KR and many autors of C programming, it is saying all about storage class type for various variables depending upon their storage specifier or by the default storage type if no storage specifier is mentioned.  In a local variable declaration, ie; variables declared inside a function are given an auto stoarge class type by default if no storage class specifer is prefixed at the variable declaration.  It is saying that function arguments are of local scope and so it is also by default auto. In, KR it is saying that the only allowable storage specifier for function argument is register only and by default it is auto.  But if we give the storage type specifier auto prefixed to a function argument it will throw a compile error saying that auto is not allowed here.  If by default it can take auto, then why can't I put auto explictly here only?  We can give only register storage class specifier only there.  That means, function arguments are register type by default.  Then also a question comes?  It is saying that address of a register variable is not allowed in C. Indeed, if the register type variable cannot be stored in a register it will be reduced to auto type storage class.  Then also we cannot get the address of that variable using an & operator.  But here in function argument, eventhough if we specify those as register types, we can get the addess of that variables without any compile error or runtime error.  It seems contradictory.  

For Eg:-

void main()
{
   int a=10,b=20,c,fun(int,int);
    c=fun(a,b);
    printf("%d\n",c);
}

int fun(register int x,/*auto */ int y)
{
  int z,*ptrtox,*ptrtoy,*ptrtor,*ptrtoarray;
  register int r=50,array[3]={9,8,7};
   ptrtox = &x;/*Why this is allowed? */
   ptrtoy = &y;
   /*ptrtor = &r;*//*Enabling this line will throw compile error*/
   ptrtoarray = array;/*Why this is allowed, arrays are of contigious allocation and they cannot be stored in a register, then why register type is allowed here? */
   /* Then why it is restricted to put auto before the variable y in the function argument */
   z=*ptrtox + *ptrtoy;
   printf("First array element is %d\n",*ptrtoarray);
   printf("First function argument of register type is %d\n",*ptrtox);
   /*printf("Value in the register variable r is %d",*ptrtor);*/
   return(z);
}

Function arguments and local variables inside to the function are stored on the stack only according to the C memory model and that is of the auto type by default if no storage type specifier defined.  Try to print the address of each variables(including variables in function arguments) for the above program for clarification.  

It may really a confusion and challege to the experts I think.  Seeking for a favourable answer that is logical in the sense.

Regards,
Thomas Mathew.
( C is the backbone to Software Technology that makes it a Sea ).
0
Comment
Question by:thomasmathewk
8 Comments
 
LVL 22

Accepted Solution

by:
grg99 earned 100 total points
ID: 12247706
C is a mighty fuzzy language.  Here's on explanation, from a code-generator's point of view:

The code generator cant in general assign registers to parameters, for many reasons:

The function may be separately compiled and there's no easy way for the compiler to pass the register name between the compile sessions.

The hardware may have it's own parameter passing protocol, for instance in a RISC machine there are often special register frames, on a VAX there are special parameter blocks.  

Putting a parameter in a register makes it difficult for a function to call itself, either firectly or indirectly.

it can't or shouldnt make parameters "static", as that conflicts with being able to pass new values in, or have different values for the same parameter in recusrsive calls.


----
Also I think somewhere some standard says the compiler is free to ignore "register".  If it's a two-pass compiler it may be seeing the "&Register" being used and then in the next pass it can ignore the "register" attribute.   Alternatively it's possible for a really smart compiler to tremporarily store the register somewhere, take the address of THAT, then after its use reload the register from memory (which won't work if that address gets stashed away somewhere).

I'm not too surprised you can't say "auto" in a parameter declaration, that 's the only reasonable attribute fro a parameter.  Neither "register" or "static" make any sense here.

0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12253659
Well, what I have read so far, function parameters are generally stored on stack. When a function is called, first the parameters are put on the stack and then the assmbly CALL is made for that function. Inside the function stack, the parameters are read and then processed. I read the return values are kept in the stack if possible but I have not read anywhere that parameters are passed through stacks.
0
 
LVL 11

Expert Comment

by:avizit
ID: 12254412
>>When a function is called, first the parameters are put on the stack and then the assmbly CALL is made for that function.

>>... but I have not read anywhere that parameters are passed through stacks.


doesnt the above two contradict ?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 12

Expert Comment

by:stefan73
ID: 12260422
Hi pankajtiwary,
> Well, what I have read so far, function parameters are generally stored on stack
Not necessarily. Up to a certain number of arguments, SPARC passes them via registers.

Almost all architectures return via a register. C allows only a single return value, so that fits nicely.

IMHO, the "register" storage class is obsolete. A good optimizer knows which variables offer most gain in a register.

BTW: There are architectures which mirror registers in memory, so they have an "address" - the 6510 is an example (6502 in general?)

Cheers!

Stefan
0
 

Expert Comment

by:Mamata_gd
ID: 12269358
Ok, Here is my first question to origional poster.

What kind of compiler are you using?  Is it C99 standard?

Declaring register variable and decaring function inline are just request to compiler and not command. This is difference between inline function and macros.

Now, when you say c=fun(a,b);
comiler simply pushes these two parameters on stack and call the fun. I also suggest you try putting function declaration and/or changing the position of function fun before the calee.

Now when the control gets into the function it is upto the loader( I am not sure at what phase it decides to use register variable) to decide depending upon free CPU registers, it will  really allocate register variable for your variable. I believe, if you are running on x86 platform there is little chance that it will get free CPU register and your request getting  satisfied.

And about passing a variable through register is only happens by default in some RISC processors''s RTOS compilers and not in x86 desktop machines.

So in order to compete, new x86 compiler came up with inline function. If you make above function inline and see whether it works. I positively feel, it will not. DO use force inline and not simple inline.

Passing value through CPU register has some pros and cons. Advantange is, it will be fast and disadvantage is, you can not have recusive function. And some code does not work and needs to be ported by implementing its own stack.

Hope this helps....








0
 

Author Comment

by:thomasmathewk
ID: 12368259
Sorry for the delay in responding to the comments.  As per your question, the compiler that I am using is the GNU Compiler Collections (gcc ver 3.xx) bundled with RedHat 8.0 or 9.0 and I assume this to be compliant with C99 standards, since C99 specifications are an open source effort.  
Then still also my sceptics are not yet fully cleared as I haven't got a vivid and reasonable answer to the question I posted.  Why my core quest to this post is about the default storage type of function parameters?  Is it register or auto?  The main thing that induce me to start a post on this topic is because when I put a storage specifier as auto infrom a function parameter declaration, I am getting a compiler error saying that auto is not allowed here.  And from books I read that the only allowed stoarge specifier here is register.  So by default is it register?  I too also agree that static and global are absurd and illogical to here.  I do believe and assume that the default stoarge type here may be auto only.  Then why cann't I put a specifier here as auto infront of it?  So please tell me the default storage type of the function parameters and I am expecting a logical answer to this question solely.  Thanks and regards to all who made a glance and comment to my post yet and in the future too.

Regards,
Thomas Mathew.
(C gave the jump-start to Computer Software Technology that advances to today's state, which was yesterday's dream).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now