455,434 Members | 1,858 Online
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

# Memory alloc for a Function

 P: n/a 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 Nov 15 '05 #1
10 Replies

 P: n/a In article , 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 forit when calling the function?(4 Bytesfor 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. Nov 15 '05 #2

 P: n/a 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 is another good resource. -- Thad Nov 15 '05 #3

 P: n/a "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) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 15 '05 #4

 P: n/a 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 Nov 15 '05 #5

 P: n/a 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." 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 es*****@acm-dot-org.invalid Nov 15 '05 #6

 P: n/a Eric Sosman wrote: 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 Nov 15 '05 #7

 P: n/a 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) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Nov 15 '05 #8

 P: n/a pete wrote: Eric Sosman wrote: Remember when there used to be differentnames for different operands? Augend and addend, minuendand 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 es*****@acm-dot-org.invalid Nov 15 '05 #9

 P: n/a Eric Sosman wrote: pete wrote: Eric Sosman wrote: Remember when there used to be differentnames for different operands? Augend and addend, minuendand 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 Nov 15 '05 #10

 P: n/a In article Eric Sosman writes: pete wrote: Eric Sosman wrote: Remember when there used to be differentnames for different operands? Augend and addend, minuendand 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/ Nov 15 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.