Connecting Tech Pros Worldwide Forums | Help | Site Map

memory used while declaring function

c.lang.myself@gmail.com
Guest
 
Posts: n/a
#1: Nov 13 '08
Is there any memory allocated in stack when declaring a function

e.g void fun(int a,int b,int c);

Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....

Fred
Guest
 
Posts: n/a
#2: Nov 13 '08

re: memory used while declaring function


On Nov 13, 7:18*am, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
Quote:
Is there any memory allocated in stack when declaring a function
>
e.g void fun(int a,int b,int c);
>
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....
No. There isn't even a requirement that such a thing as a stack even
exists.
--
Fred
Chris Dollin
Guest
 
Posts: n/a
#3: Nov 13 '08

re: memory used while declaring function


c.lang.myself@gmail.com wrote:
Quote:
Is there any memory allocated in stack when declaring a function
>
e.g void fun(int a,int b,int c);
What stack?

The code isn't running, so even if there's a stack at runtime,
there isn't one when the declaration gets processed by the
compiler.

(Unless you're asking about the /compiler's/ stack, if any; if
so, why?)
Quote:
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....
No.

When the compiler processes that /declaration/, it needn't reserve
stack space for anything. The declaration just says "there's this
function `fun` with arguments `(int, int, int)` and return-type void."

Whether arguments are passed on a stack or not doesn't matter, since
we're not compiling a call of `fun` or a definition of it either.

When we compile `void fun(int a, int b, int c) { ...the body ... }`,
/then/ we might think about stack allocation, if that's how the
implementation operates. What's /required/ is that the code behaves
as if there's three auto variables a, b, c, initialised to the values
passed in a call, and which can be forgotten when the function exits.

Consider

int fun( int a, int b, int c ) { return a + b + c; }

where I've returned `int` to have something happening. When the
compiler compiles this code it need not use any stack /at all/,
not even a bit:

add r0, r1
add r0, r2
mov pc, r14

The arguments are passed in registers r0, r1, and r2, and the
result returned in r0. The addition can be done in-place into
the result/first-argument register. Since `fun` doesn't call
anything else, we don't need to stack a return address or copies
of the arguments.

So, while an implementation /might/ use a stack for function
business, specific cases can be stack-free.

The moral of the stoty is that one shouldn't confuse an
implementation technique (stacks) with a specification (variables
which vanish when their scope is left), nor declarations (let
me tell you about X) with definitions (compile me this X) and
executions (call X and pass these three values).

--
"It's just the beginning we've seen." - Colosseum, /Tomorrow's Blues/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Eric Sosman
Guest
 
Posts: n/a
#4: Nov 13 '08

re: memory used while declaring function


c.lang.myself@gmail.com wrote:
Quote:
Is there any memory allocated in stack when declaring a function
>
e.g void fun(int a,int b,int c);
The C language does not describe how an implementation does
what is required of it, so it's impossible to answer your question
in full generality. For what it's worth, I have never come across
an implementation that used any stack space at all for a function
declaration.
Quote:
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....
As above: Possibly, but almost certainly not. The language
standard does not forbid reserving stack space for declarations,
but I've never seen an implementation that did so.

--
Eric.Sosman@sun.com
Richard Tobin
Guest
 
Posts: n/a
#5: Nov 13 '08

re: memory used while declaring function


In article <a6384f5b-b419-4e87-adfb-34c9daf55360@w24g2000prd.googlegroups.com>,
c.lang.myself@gmail.com <c.lang.myself@gmail.comwrote:
Quote:
>Is there any memory allocated in stack when declaring a function
>
>e.g void fun(int a,int b,int c);
>
>Is it true that compiler reserves 3 stack spaces for parameters a ,b
>and c....
It depends what you mean by "allocated" and "reserved". The compiler
might well choose three positions on the stack that will be used for
the arguments at run time. But the actual allocation won't happen
until run time, and it will happen at each invocation of the function,
rather than once for the declaration.

-- Richard


--
Please remember to mention me / in tapes you leave behind.
S M Ryan
Guest
 
Posts: n/a
#6: Nov 14 '08

re: memory used while declaring function


In article <a6384f5b-b419-4e87-adfb-34c9daf55360@w24g2000prd.googlegroups.com>,
"c.lang.myself@gmail.com" <c.lang.myself@gmail.comwrote:
Quote:
Is there any memory allocated in stack when declaring a function
>
e.g void fun(int a,int b,int c);
>
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....
Perhaps. But the memory associated with functions is not something you control,
allocate, or deallocate. Unless you're working on a machine with tight memory,
don't worry about it.

--
I'm not even supposed to be here today.

I ASSURE YOU WE'RE OPEN!
c.lang.myself@gmail.com
Guest
 
Posts: n/a
#7: Nov 14 '08

re: memory used while declaring function


So what actually all things compiler do when it encounters declaration
of function first time....
Ian Collins
Guest
 
Posts: n/a
#8: Nov 14 '08

re: memory used while declaring function


c.lang.myself@gmail.com wrote:

[please keep enough of the context for your reply to make sense]
Quote:
So what actually all things compiler do when it encounters declaration
of function first time....
If the declaration isn't a definition, it simply parses and remembers it.

--
Ian Collins
Chris Dollin
Guest
 
Posts: n/a
#9: Nov 14 '08

re: memory used while declaring function


c.lang.myself@gmail.com wrote:
Quote:
So what actually all things compiler do when it encounters declaration
of function first time....
The compiler remembers the declaration, so that it can check uses of the
function against the declaration and compile the appropriate code for the
call.

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nick Keighley
Guest
 
Posts: n/a
#10: Nov 14 '08

re: memory used while declaring function


On Nov 13, 4:19*pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
Quote:
In article <a6384f5b-b419-4e87-adfb-34c9daf55...@w24g2000prd.googlegroups..com>,
>
c.lang.mys...@gmail.com <c.lang.mys...@gmail.comwrote:
Quote:
Is there any memory allocated in stack when declaring a function
>
Quote:
e.g void fun(int a,int b,int c);
>
Quote:
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....
>
It depends what you mean by "allocated" and "reserved". *The compiler
might well choose three positions on the stack that will be used for
the arguments at run time. *But the actual allocation won't happen
until run time, and it will happen at each invocation of the function,
rather than once for the declaration.
but those stack positions must be relative rather than absolute
otherwise I can't see how recursive functions are to be implemented.

--
Nick Keighley
Richard Tobin
Guest
 
Posts: n/a
#11: Nov 14 '08

re: memory used while declaring function


In article <c6675922-ee9d-49c8-be54-33f1a96f0b82@c22g2000prc.googlegroups.com>,
Nick Keighley <nick_keighley_nospam@hotmail.comwrote:
Quote:
Quote:
>It depends what you mean by "allocated" and "reserved". *The compiler
>might well choose three positions on the stack that will be used for
>the arguments at run time. *But the actual allocation won't happen
>until run time, and it will happen at each invocation of the function,
>rather than once for the declaration.
Quote:
>but those stack positions must be relative rather than absolute
Yes of course. That's why they're on the stack!

-- Richard
--
Please remember to mention me / in tapes you leave behind.
CBFalconer
Guest
 
Posts: n/a
#12: Nov 14 '08

re: memory used while declaring function


Nick Keighley wrote:
Quote:
rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
Quote:
>c.lang.mys...@gmail.com <c.lang.mys...@gmail.comwrote:
>>
Quote:
>>Is there any memory allocated in stack when declaring a function
>>e.g void fun(int a,int b,int c);
>>>
>>Is it true that compiler reserves 3 stack spaces for parameters
>>a ,b and c....
>>
>It depends what you mean by "allocated" and "reserved". The
>compiler might well choose three positions on the stack that will
>be used for the arguments at run time. But the actual allocation
>won't happen until run time, and it will happen at each invocation
>of the function, rather than once for the declaration.
>
but those stack positions must be relative rather than absolute
otherwise I can't see how recursive functions are to be implemented.
This has been stated before, but this thread seems to be ignoring
it. THERE IS NO STACK. At least none is specified. If one
exists, it was put there for the convenience of that particular
implementor. The things that do exist are static, allocated, and
automatic storage.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Richard Tobin
Guest
 
Posts: n/a
#13: Nov 14 '08

re: memory used while declaring function


In article <491DF224.83D1FC94@yahoo.com>,
CBFalconer <cbfalconer@maineline.netwrote:
Quote:
>This has been stated before, but this thread seems to be ignoring
>it.
That's because we're not all mindless cretins.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Antoninus Twink
Guest
 
Posts: n/a
#14: Nov 14 '08

re: memory used while declaring function


On 14 Nov 2008 at 22:12, Richard Tobin wrote:
Quote:
CBFalconer <cbfalconer@maineline.netwrote:
Quote:
>>This has been stated before, but this thread seems to be ignoring
>>it.
>
That's because we're not all mindless cretins.
Exactly.

It's been stated before. It was bullshit then, and it's bullshit now.

Chris Dollin
Guest
 
Posts: n/a
#15: Nov 17 '08

re: memory used while declaring function


CBFalconer wrote:
Quote:
This has been stated before, but this thread seems to be ignoring
it. THERE IS NO STACK. At least none is specified.
I said as much in my response. Did you read it?

--
"It took a very long time, much longer than the most /Sector General/
generous estimates." - James White

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Closed Thread