代写代考

Used for local variables
Also when function calls are made the arguments and return address are placed on the stack

Copyright By PowCoder代写 加微信 powcoder

Used for dynamically allocated memory (new, delete)

Lifetime of data items

Local variables are removed from the stack when a return is made from the function in which they are declared
(or in the case of a variable local to a block, when the block is left)
Items pushed onto the stack when a function is called are also removed when a return is made from the function

Items on the heap remain there until they are released using delete (or until the program terminates if they are not released), i.e. their lieftime is controlled by the programmer

2019 question 3(a)(ii)(

float *p=new float[10]: memory allocated on the heap for 10 floats; its address is stored in the local variable p on the stack

for (int i=0; i<10; i++): local variable i is stored on the stack and incremented during loop p[i] = 0.5*i: values of local variables i and p obtained from the stack; used to calculate address of p[i] and value of 0.5*i; heap contents at the calculated address updated myfun(p): contents of local varibles p (i.e. memory address) pushed onto stack for use as argument, return address also pushed onto stack; after execution of fun these items are removed from the stack delete [] p: memory on heap released for reuse; local variable p still holds the address of that block of memory (but its lifetime is porobably about to expire) 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com