473,396 Members | 1,743 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,396 software developers and data experts.

Memory allocation for function paramaters

Hi

Can anybody tell me, where memory will be allocated for Function
parameters and return value of a function.
Are these stored on stack area, or data area, or any where else.

Reg
Praveen
Nov 14 '05 #1
8 1561
pr**********@softhome.net (Praveen Kumar Madis) wrote:
Hi

Can anybody tell me, where memory will be allocated for Function
parameters
From the C Standard POV function parameters behave just like ordinary
automatic variables with function scope.
and return value of a function.
Are these stored on stack area, or data area, or any where else.


Yes. No. Neither. Both. Depends.

<OT>
On some (most?) typical implementations return values will be passed
by stack or register. However, this is beyond the scope of the C
language definition and hence OT in comp.lang.c.
</OT>

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #2
Praveen Kumar Madis wrote:
Hi

Can anybody tell me, where memory will be allocated for Function
parameters and return value of a function.
Are these stored on stack area, or data area, or any where else.


C implementations have a lot of freedom in this area. On many
implementations/platforms, function parameters are passed in a
defined set of registers and if the registers are for some reason
not enough, the stack is used (as well). The same for return
value: often a register but the stack is used for this as well.

I said 'implementations/platforms' because in many cases the C
implementation is not (fully) free to choose which registers to
use; this because the platform (i.e., the operating system or even
the CPU design) has certain rules as well. Using the same rules
on a platform also makes linking modules written in different
programming languages easier/possible; of course when all compiler
adhere to the same or compatible rules.

As a portable C programmer you don't need to know how and where,
and that's a great thing about this language!

Case

Nov 14 '05 #3
pr**********@softhome.net (Praveen Kumar Madis) writes:
Can anybody tell me, where memory will be allocated for Function
parameters and return value of a function.
Are these stored on stack area, or data area, or any where else.


The C language standard doesn't say how the values of the arguments are
transferred to a function, or how the return value is transferred back,
so it's up to the implementation to decide. There is no requirement
that a stack even exists.

That said, most implementations do indeed use a stack for this, often
in combination with CPU registers. If you're interested how your
particular implementation handles it, please ask in a newsgroup
dedicated to that implemenation.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #4
In <a3**************************@posting.google.com > pr**********@softhome.net (Praveen Kumar Madis) writes:
Can anybody tell me, where memory will be allocated for Function
parameters and return value of a function.
Are these stored on stack area, or data area, or any where else.


Function parameters are allocated the same way as function automatic
variables, regardless of the mechanism used for passing their values
from the caller.

The return value of a function need not be stored anywhere, except in a
machine register. If it doesn't fit in one or more registers, it is up to
the compiler to decide where to store it, so that the caller can retrieve
it.

But the qood question is: why do you care about these issues? Portable C
code (the only kind of code we normally discuss here) is not affected in
any way by them.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Martin Dickopp <ex****************@zero-based.org> wrote in message news:<cu*************@zero-based.org>...
pr**********@softhome.net (Praveen Kumar Madis) writes:
[Q about where parameters live redacted]

That said, most implementations do indeed use a stack for this, often
in combination with CPU registers. If you're interested how your
particular implementation handles it, please ask in a newsgroup
dedicated to that implemenation.


The Zilog ZEUS Z8000 compiler passed the first 6 16 bit arguments in
registers (R7-R1, descending), and thereafter on the stack.

Made the implementation of <vararg.h> (this was pre-C89) kind of a
pain in the butt!
Nov 14 '05 #6
In <96************************@posting.google.com> re********@yahoo.com (red floyd) writes:
Martin Dickopp <ex****************@zero-based.org> wrote in message news:<cu*************@zero-based.org>...
pr**********@softhome.net (Praveen Kumar Madis) writes:
[Q about where parameters live redacted]

That said, most implementations do indeed use a stack for this, often
in combination with CPU registers. If you're interested how your
particular implementation handles it, please ask in a newsgroup
dedicated to that implemenation.


The Zilog ZEUS Z8000 compiler passed the first 6 16 bit arguments in
registers (R7-R1, descending), and thereafter on the stack.

Made the implementation of <vararg.h> (this was pre-C89) kind of a
pain in the butt!


This kind of convention (first N arguments in registers, the rest on the
stack) is commonplace on modern architectures. Never bothered to see
how <stdarg.h> is actually implemented (probably using compiler magic).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
In message <c9**********@sunnews.cern.ch>
Da*****@cern.ch (Dan Pop) wrote:
In <96************************@posting.google.com> re********@yahoo.com (red floyd) writes:
The Zilog ZEUS Z8000 compiler passed the first 6 16 bit arguments in
registers (R7-R1, descending), and thereafter on the stack.

Made the implementation of <vararg.h> (this was pre-C89) kind of a
pain in the butt!


This kind of convention (first N arguments in registers, the rest on the
stack) is commonplace on modern architectures. Never bothered to see
how <stdarg.h> is actually implemented (probably using compiler magic).


Simplest thing to do is often for the compiled function entry sequence to
push the argument registers onto the stack if any of their addresses are
taken by the C code in the body. Then a bog-standard <stdarg.h> will just
work.

Alternatively, arguments might not get passed in registers for variadic
functions (possible because variadic functions must be prototyped).

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #8
In <60****************@tematic.com> Kevin Bracey <ke**********@tematic.com> writes:
In message <c9**********@sunnews.cern.ch>
Da*****@cern.ch (Dan Pop) wrote:
In <96************************@posting.google.com> re********@yahoo.com (red floyd) writes:
>The Zilog ZEUS Z8000 compiler passed the first 6 16 bit arguments in
>registers (R7-R1, descending), and thereafter on the stack.
>
>Made the implementation of <vararg.h> (this was pre-C89) kind of a
>pain in the butt!


This kind of convention (first N arguments in registers, the rest on the
stack) is commonplace on modern architectures. Never bothered to see
how <stdarg.h> is actually implemented (probably using compiler magic).


Simplest thing to do is often for the compiled function entry sequence to
push the argument registers onto the stack if any of their addresses are
taken by the C code in the body. Then a bog-standard <stdarg.h> will just
work.

Alternatively, arguments might not get passed in registers for variadic
functions (possible because variadic functions must be prototyped).


This alternative approach is avoided in practice by most implementors,
although perfectly allowed by the standard: far too many (clueless)
people call printf/scanf without including <stdio.h> and the implementors
don't want to get complaints from them ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

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

Similar topics

16
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid,...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
2
by: mghale | last post by:
Greetings, I have moved a database from the DB2 Windows Platform (Dev) to our production AIX Server and am experiencing an issue: The AIX platform is 5.3 and DB2 version 8.1 pixpack 9a...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
9
by: Sundar | last post by:
Hi, i am trying to make an application that will require registering of quite a few dlls and execute. Now one of the first bottlenecks that my mentor refused is allocation of memory or the usage...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.