Memory alloc for a Function

R

Ryan Wang

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!
 
W

Walter Roberson

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.
 
K

Keith Thompson

Ryan Wang said:
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.
 
R

Ryan Wang

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 fucking 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.
 
E

Eric Sosman

Keith said:
[...]
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?"
 
P

pete

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.
 
T

Thad Smith

Ryan said:
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.
 
K

Keith Thompson

Eric Sosman said:
Keith said:
[...]
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.
 
E

Eric Sosman

pete said:
Eric Sosman wrote:




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
 
P

pete

Eric said:
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.
 
D

Dik T. Winter

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

dividend and divisor.
> I've encountered
> "abscissa" only a few times, and have never seen "surd"
> outside of fiction

Ah, sci.math is a work of fiction?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top