Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Memory alloc for a Function (http://www.velocityreviews.com/forums/t440146-memory-alloc-for-a-function.html)

Ryan Wang 11-14-2005 02:17 AM

Memory alloc for a Function
 
Hi,
here is the function:

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

How many spaces will system alloc for
it when calling the function?(4 Bytes
for an int)

I still have a lot of questions that makes
me confuse.such as the return value of
sizeof() func, where to alloc the memory for
the variable of the funcs etc.

And which book will discuss such questions
in detail?

Thanks,
Best Regard!

--
Ryan Wang
email:wangrm1979#at#gmail#dot#com
msn:wangrm1979#at#hotmail#dot#com




Walter Roberson 11-14-2005 02:57 AM

Re: Memory alloc for a Function
 
In article <dl8sm7$ed$1@news.cn99.com>,
Ryan Wang <wangruiming@windics1.com> wrote:
>here is the function:


>int sum(int a,int b)
>{
>int c;
>c = a + b;
>return c;
>}


>How many spaces will system alloc for
>it when calling the function?(4 Bytes
>for an int)


The amount of memory would depend entirely on the system, compiler,
and compiler options.

On some of the systems I know of, *no* memory would be allocated.
The two integer parameters would be passed in via registers,
the compiler would emit a single "add" instruction on those
registers depositing the result in a third register, and that
third register would happen to be the one in which the return value
would normally be passed back.

On some systems, there would not even be memory allocated for the
return adress.

Other systems... I have seen other systems that would have allocated
a minimum of 86 bytes stack bytes for a function such as that.

Some of the posters are using embedded systems on which there *is*
no stack.
--
Programming is what happens while you're busy making other plans.

Keith Thompson 11-14-2005 05:03 AM

Re: Memory alloc for a Function
 
"Ryan Wang" <wangruiming@windics1.com> writes:
> here is the function:
>
> int sum(int a,int b)
> {
> int c;
> c = a + b;
> return c;
> }
>
> How many spaces will system alloc for
> it when calling the function?(4 Bytes
> for an int)


The allocated space will vary from one implementation to another.
There's seldom any reason you should care, as long as the function
works properly. Why do you want to know?

> I still have a lot of questions that makes
> me confuse.such as the return value of
> sizeof() func, where to alloc the memory for
> the variable of the funcs etc.


sizeof is an operator, not a function. It yields the size of its
argument in bytes; the argument can be either an expression or a type
name in parentheses.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Ryan Wang 11-14-2005 06:33 AM

Re: Memory alloc for a Function
 
thanks for all your answers.

Indeed I'm not willing to know all these answers.
I am a Chinese,Software Engineer.Whenever I am
looking for a new job.Such kind of questions always
be asked. And always I give a not correct answer.

I can write programs with no difficulties,but I
don't know such ****ing questions's answer.So lost
opptunity severel time.

I am so sorry to waste you to answer such a stupid
question and Really thank you for your help.

--
Ryan Wang
email:wangrm1979#at#gmail#dot#com
msn:wangrm1979#at#hotmail#dot#com



Eric Sosman 11-14-2005 01:11 PM

Re: Memory alloc for a Function
 
Keith Thompson wrote:
> [...]
> sizeof is an operator, not a function. It yields the size of its
> argument in bytes; the argument can be either an expression or a type
> name in parentheses.


Since sizeof is an operator, not a function, it might
be best to avoid mentioning its "argument." Operators
have "operands," or in this case "operand."

<Musing> Remember when there used to be different
names for different operands? Augend and addend, minuend
and subtrahend, and the rest? Just about the only such
names one hears nowadays are dividend and divisor. I used
some of the now-unfashionable words in the comments for a
suite of multiple-precision arithmetic functions and got
blank stares at the code review.

However, it's probably a good thing the style has largely
shifted to the generic "operand" as a catch-all -- otherwise,
we'd need to come up with a special name for the operand of
sizeof. Any votes for "measurand?"

--
Eric Sosman
esosman@acm-dot-org.invalid

pete 11-14-2005 01:31 PM

Re: Memory alloc for a Function
 
Eric Sosman wrote:

> <Musing> Remember when there used to be different
> names for different operands? Augend and addend, minuend
> and subtrahend, and the rest?


multiplicand

> Just about the only such
> names one hears nowadays are dividend and divisor. I used
> some of the now-unfashionable words in the comments for a
> suite of multiple-precision arithmetic functions and got
> blank stares at the code review.
>
> However, it's probably a good thing the style has largely
> shifted to the generic "operand" as a catch-all -- otherwise,
> we'd need to come up with a special name for the operand of
> sizeof. Any votes for "measurand?"


"sizand"
The operators all seem to have an operand
that begins with at least a partial spelling of the operator.

--
pete

Thad Smith 11-14-2005 04:02 PM

Re: Memory alloc for a Function
 
Ryan Wang wrote:

> here is the function:
>
> int sum(int a,int b)
> {
> int c;
> c = a + b;
> return c;
> }
>
> How many spaces will system alloc for
> it when calling the function?(4 Bytes
> for an int)


The amount of memory required for the code and for the data
varies from one implementation to another and is outside the scope of
Standard C as discussed here. You could look at the output of a
specific compiler to determine its allocation.

> I still have a lot of questions that makes
> me confuse.such as the return value of
> sizeof() func,


sizeof returns the size of an object or object type in bytes.

> where to alloc the memory for the variable of the funcs etc.


For fixed size variables, they are usually defined at the beginning of
the function or beginning of a block where they are used (C99 allows
definition to be placed within a block) . Variables that have the size
decided are runtime as allocated with malloc() and friends.

> And which book will discuss such questions
> in detail?


K&R2 (Kernigan & Richie: The C Programming Language) is a good place to
start. The C FAQ <http://www.eskimo.com/~scs/C-faq/top.html> is another
good resource.

--
Thad



Keith Thompson 11-14-2005 07:23 PM

Re: Memory alloc for a Function
 
Eric Sosman <esosman@acm-dot-org.invalid> writes:
> Keith Thompson wrote:
>> [...]
>> sizeof is an operator, not a function. It yields the size of its
>> argument in bytes; the argument can be either an expression or a type
>> name in parentheses.

>
> Since sizeof is an operator, not a function, it might
> be best to avoid mentioning its "argument." Operators
> have "operands," or in this case "operand."


Yes, good point.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Eric Sosman 11-15-2005 02:21 AM

[OT] Re: Memory alloc for a Function
 
pete wrote:
> Eric Sosman wrote:
>
>
>> <Musing> Remember when there used to be different
>>names for different operands? Augend and addend, minuend
>>and subtrahend, and the rest?

>
>
> multiplicand


Yes, of course. Not sure why you didn't mention its
co-operand (co-conspirator?), the multiplier.

Lots of specialized arithmetic or mathematical terms
seem to have fallen out of favor. I've encountered
"abscissa" only a few times, and have never seen "surd"
outside of fiction (the surname of a grouchy professor of
mathematics, whose lovely daughter was Abscissa Surd) or
of comic riddling verse:

But what are all such gaities to me
Whose thoughts are full of indices and surds?
x^2 + 7x + 53 = 11/3.

-- Lewis Carroll

--
Eric Sosman
esosman@acm-dot-org.invalid

pete 11-15-2005 04:49 AM

Re: [OT] Re: Memory alloc for a Function
 
Eric Sosman wrote:
>
> pete wrote:
> > Eric Sosman wrote:
> >
> >
> >> <Musing> Remember when there used to be different
> >>names for different operands? Augend and addend, minuend
> >>and subtrahend, and the rest?

> >
> >
> > multiplicand

>
> Yes, of course. Not sure why you didn't mention its
> co-operand (co-conspirator?), the multiplier.


"multiplicand" was the only other one that I could remember.
I used it recently in a post on this newsgroup.

--
pete


All times are GMT. The time now is 03:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.