Hello

I was wondering if i can get some help answering these questions or pointed in the right direction.

1. How does a function pass control back to the calling function?

2. How do you declare a local variable to be static?

3. Show how to dynamically allocate an array of doubles of size N?

im having trouble finding them in my book.

thank you.
closed account (N36fSL3A)
1. How does a function pass control back to the calling function?
Not sure what you mean. Once the function goes out of scope, the calling function resumes.

2. How do you declare a local variable to be static?
1
2
3
4
void func()
{
    static int x = 0;
}


3. Show how to dynamically allocate an array of doubles of size N?
int* arr = new int[N * 2];
Array of doubles of size N? (If you didn't mean twice the size of N)

3. Show how to dynamically allocate an array of doubles of size N?

double * arr = new double[N];
Last edited on
closed account (N36fSL3A)
Yea, I read the last one wrong, wildblue is correct.
For 1), when a function is called the location from where it is called is pushed onto the call stack, and upon a return the function is popped from the stack and control returns to the last point of execution, where the caller can retrieve the return value from a register.

In assembly, this can be done through the instruction call func_name, which then jumps to func_name. Then the ret function is called, which pops the stack and returns to the call. Here is an example in NASM syntax assembly:
1
2
3
4
5
6
  call func_name    ; push the return address on the stack
  ...

func_name:
  xor eax, eax
  ret               ; pushes the return value into [esp]
Last edited on
Topic archived. No new replies allowed.