Memory alloc for a Function

Discussion in 'C Programming' started by Ryan Wang, Nov 14, 2005.

  1. Ryan Wang

    Ryan Wang Guest

    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
    Ryan Wang, Nov 14, 2005
    #1
    1. Advertising

  2. In article <dl8sm7$ed$99.com>,
    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 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.
    Walter Roberson, Nov 14, 2005
    #2
    1. Advertising

  3. "Ryan Wang" <> 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) <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.
    Keith Thompson, Nov 14, 2005
    #3
  4. Ryan Wang

    Ryan Wang Guest

    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.

    --
    Ryan Wang
    email:wangrm1979#at#gmail#dot#com
    msn:wangrm1979#at#hotmail#dot#com
    Ryan Wang, Nov 14, 2005
    #4
  5. Ryan Wang

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Nov 14, 2005
    #5
  6. Ryan Wang

    pete Guest

    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
    pete, Nov 14, 2005
    #6
  7. Ryan Wang

    Thad Smith Guest

    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
    Thad Smith, Nov 14, 2005
    #7
  8. Eric Sosman <> 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) <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.
    Keith Thompson, Nov 14, 2005
    #8
  9. Ryan Wang

    Eric Sosman Guest

    [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
    lid
    Eric Sosman, Nov 15, 2005
    #9
  10. Ryan Wang

    pete Guest

    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
    pete, Nov 15, 2005
    #10
  11. Re: [OT] Re: Memory alloc for a Function

    In article <> Eric Sosman <> writes:
    > 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.


    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?
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Nov 15, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    1
    Views:
    610
    .NET Follower
    Feb 13, 2004
  2. B. Gandhi

    Memory Alloc/Dealloc query

    B. Gandhi, Sep 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    597
    Peter Shaggy Haywood
    Sep 25, 2003
  3. Vulcan Fire

    string vs const int memory alloc

    Vulcan Fire, Apr 12, 2005, in forum: C Programming
    Replies:
    6
    Views:
    633
    Vulcan Fire
    Apr 19, 2005
  4. Replies:
    1
    Views:
    392
    Victor Bazarov
    Dec 26, 2005
  5. Angus
    Replies:
    29
    Views:
    849
    Nick Keighley
    Jul 29, 2010
Loading...

Share This Page