473,809 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1411
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.***********@g mail.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.***********@g mail.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************ *************** *******@w24g200 0prd.googlegrou ps.com>,
c.***********@g mail.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************ *************** *******@w24g200 0prd.googlegrou ps.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.***********@g mail.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.***********@g mail.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...@w2 4g2000prd.googl egroups..com>,

c.lang.mys...@g mail.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1893
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 from the original lenght in order to get it to work as there seems to be 2 extra 0 bytes at the end. Functions included Stu
5
1779
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 of the text, the program is supposed to demonstrate the use of a copy constructor function for the object created on the heap (because there is a char* pointer variable in the instantiated class) and how you would use the delete keyword with the...
5
1696
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 a problem which corrected my way of thinking. Now, people probably won't be able to answer much to that question, so I will give an example. This is example uses the qt library qstring.h, but I assume this would be similar to the C++ string.h...
25
2394
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 memory profile shows that those instances survive 3 rounds of GC collections - it's not what I expected. In my real code, CMyClass occupies big amount of memory and they all share one stance of another class that I don't have enough memory hold...
13
6169
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 per convenience and access them. I was told this would save some memory. I dont understand the logic behind this, as i`ve declared variables as global (assuming i`ve declared the block in main() ) this would always b a residual data for access at...
15
4795
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 10000 (actually, what I need is 80000), the program has segmentation error. The intersting thing is that the positions reporting segmentation error are different if I set N to be different values. What problem is this usually? I guess must be...
57
3064
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 a long int, the windows refuses to execute the program. More precicely the code terminates as soon as it starts to execute. How can i overcome this issue. Note, if array length is set smaller the code runs with no problems. Here is the code:...
18
2637
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 tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
4
1304
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, I want the table automatically show relevant rows and hide others. Say I type 10023,
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.