Browse Call stack (invocation stack) (i)

Source: Internet
Author: User

Stacks in the computer field is a frequently mentioned noun, the data structure of the stack, network transport has a protocol stack. The call stack we are discussing today refers to a dynamic data structure that stores function call information during the execution of a program.

This definition may be too abstract, before giving a specific example, please think about a question, which information is required in the function call process? Or so, what code does a compiler generate when it faces a function's call instruction?

First, the return address of the function should be saved. It's like you and your puppy playing still UFO games, every function call is like throwing a UFO, when your dog hums to pick up the UFO, when the function is done, it must know where to return the UFO to you.

Then, the function of the parameter is an essential element, this is very intuitive, not much wordy. Third, the local variables of the called function are also stored on the stack. Because according to the definition of local scalars, different calls to the same function, local variables have different storage space, does not affect each other, so this data is closely related to the function call information.

Next, let's look at the information in the call stack for the function by using an example:

For the following section of C + + program

#include <stdio.h> int sumfromone (int d) {int sum = 0XABCD; if (d = = 1) sum = 1; Else sum = d + sumfromone (d-1); re Turn sum; void Main () {int sum = Sumfromone (); printf ("sum=%d", sum);}

  [Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.