By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,649 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

memory used while declaring function

P: n/a
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....
Nov 13 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Nov 13, 7:18*am, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:
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
Nov 13 '08 #2

P: n/a
c.***********@gmail.com wrote:
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?)
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

Nov 13 '08 #3

P: n/a
c.***********@gmail.com wrote:
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.
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.

--
Er*********@sun.com
Nov 13 '08 #4

P: n/a
In article <a6**********************************@w24g2000prd. googlegroups.com>,
c.***********@gmail.com <c.***********@gmail.comwrote:
>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.
Nov 13 '08 #5

P: n/a
In article <a6**********************************@w24g2000prd. googlegroups.com>,
"c.***********@gmail.com" <c.***********@gmail.comwrote:
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!
Nov 14 '08 #6

P: n/a
So what actually all things compiler do when it encounters declaration
of function first time....
Nov 14 '08 #7

P: n/a
c.***********@gmail.com wrote:

[please keep enough of the context for your reply to make sense]
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
Nov 14 '08 #8

P: n/a
c.***********@gmail.com wrote:
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

Nov 14 '08 #9

P: n/a
On Nov 13, 4:19*pm, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <a6384f5b-b419-4e87-adfb-34c9daf55...@w24g2000prd.googlegroups..com>,

c.lang.mys...@gmail.com <c.lang.mys...@gmail.comwrote:
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.

--
Nick Keighley
Nov 14 '08 #10

P: n/a
In article <c6**********************************@c22g2000prc. googlegroups.com>,
Nick Keighley <ni******************@hotmail.comwrote:
>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
Yes of course. That's why they're on the stack!

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 14 '08 #11

P: n/a
Nick Keighley wrote:
rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
>c.lang.mys...@gmail.com <c.lang.mys...@gmail.comwrote:
>>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.
Nov 14 '08 #12

P: n/a
In article <49***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>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.
Nov 14 '08 #13

P: n/a
On 14 Nov 2008 at 22:12, Richard Tobin wrote:
CBFalconer <cb********@maineline.netwrote:
>>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.

Nov 14 '08 #14

P: n/a
CBFalconer wrote:
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

Nov 17 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.