473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

memory used while declaring function

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
14 1377
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
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
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
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
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
So what actually all things compiler do when it encounters declaration
of function first time....
Nov 14 '08 #7
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2...
5
by: August1 | last post by:
This is a short program that I have written from a text that demonstrates a class object variable created on the stack memory and another class object variable created on the heap memory. By way...
5
by: Lionel | last post by:
Hi all, Just wondering exactly how memory is allocated using the new operator? More specifically I am interested in how the memory is calculated for globable variables? I recently stumbled into...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
15
by: syang8 | last post by:
hi, folks, I use Kdevelop to build some scientific simulation on Linux. If I set the size of an array N = 8000, the program works fine. However, if I set the array N some number greater than...
57
by: fermineutron | last post by:
I wrote a program to calculate factorials of large numbers. The long precission integers are stores in arrays. When i specify the length of these arrays larger than 16000 elements, each element is...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
4
by: Rob | last post by:
I want to implement a in-memory row selection function. Specifically, given a table Name Zip Phone Tony 98034 127xxxxx Mike 10023 271xxxx Jame 10023 253xxxx and a text box,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.