呼叫堆疊

调用堆栈(英語:Call stack,台湾稱呼叫堆疊)别称有:执行栈(execution stack)、控制栈(control stack)、运行时栈(run-time stack)与机器栈(machine stack),是電腦科學中存儲有關正在執行的子程式的訊息的堆疊。英文有時直接简称“”(the stack),但堆疊中不一定僅存儲子程式訊息。幾乎所有電腦程式都依賴於呼叫堆疊,而高階語言一般將呼叫堆疊的細節隱藏至後台。

呼叫堆疊最經常被用於存放子程式的返回位址。在呼叫任何子程式時,主程式都必須暫存子程式執行完畢後應該返回到的位址。因此,如果被呼叫的子程式還要呼叫其他的子程式,其自身的返回位址就必須存入呼叫堆疊,在其自身執行完畢後再行取回。在遞迴程式中,每一層次遞迴都必須在呼叫堆疊上增加一條位址,因此如果程式出現無限遞迴(或僅僅是過多的遞迴層次),呼叫堆疊就會產生堆疊溢位

功能

呼叫堆疊的主要功能是存放返回位址。除此之外,呼叫堆疊還用於存放:

  • 本地變數:子程式的變數可以存入呼叫堆疊,這樣可以達到不同子程式間變數分離開的作用。
  • 參數傳遞:如果暫存器不足以容納子程式的參數,可以在呼叫堆疊上存入參數。
  • 環境傳遞:有些語言(如PascalAda)支援「多層子程式」,即子程式中可以利用主程式的本地變數。這些變數可以通過呼叫堆疊傳入子程式。

實例

組合語言

以下MIPS組合語言程式計算,並將結果存至暫存器s0

main:
    li      $a0, 3
    li      $a1, 4
    jal     sumsq
    move    $s0, $v0
    j       mainend
sumsq:
    addi    $sp, $sp, -4        # 在堆疊上分配空間
    sw      $ra, 0($sp)         # 將sumsq的返回位址存入堆疊中
    jal     square
    move    $t0, $v0
    move    $a0, $a1
    jal     square
    add     $v0, $v0, $t0
    lw      $ra, 0($sp)         # 從堆疊中取回sumsq的返回位址
    addi    $sp, $sp, 4         # 釋出堆疊上分配的空間
    jr      $ra
square:
    mult    $a0, $a0
    mflo    $v0
    jr      $ra
mainend:

這裡,主程式(main)呼叫「sumsq」子程式並將返回位址存入暫存器ra,但是「sumsq」子程式需要呼叫「square」子程式。為保證sumsq的返回位址不被重寫,這個位址被存儲在堆疊中。在square子程式返回後,sumsq再從堆疊中取回其自身的返回位址。

安全性

在較底層語言(如組合語言C語言中),程式控制訊息與資料可能一同被存入呼叫堆疊中,因此造成安全隱患,可能允許惡意程式通過栈缓冲区溢出(stack buffer overflow)來獲取程式的控制權。

參見

Read other articles:

AlojzovMunicipality BenderaLambang kebesaranAlojzovKoordinat: 49°25′24″N 17°2′29″E / 49.42333°N 17.04139°E / 49.42333; 17.04139Koordinat: 49°25′24″N 17°2′29″E / 49.42333°N 17.04139°E / 49.42333; 17.04139Country CekoRegionOlomoucDistrictProstějovLuas • Total4,64 km2 (179 sq mi)Ketinggian333 m (1,093 ft)Populasi • Total232 • Kepadatan0,50/km2 (1,3/sq mi...

 

Star AllianceDidirikan14 Mei 1997Armada5.033Tujuan1.294 bandara195 negaraSloganThe Way the Earth Connects.Kantor pusatFrankfurt am Main, JermanSitus webstaralliance.comStar Alliance, diluncurkan pada 14 Mei 1997, adalah sebuah aliansi maskapai penerbangan pertama di dunia, dan yang terbesar sampai saat ini dengan berbagai bentuk kerjasama.[1] Per April 2018, Star Alliance adalah aliansi maskapai penerbangan terbesar berdasarkan jumlah penumpang dengan 762,27 juta penumpang, mengunggul...

 

Asia Tengah, 1848. Permainan Besar, atau The Great Game, adalah istilah yang pertama kali disebutkan oleh Arthur Connolly, yang mengacu pada persaingan dan konflik strategi antara Imperium Britania dan Kekaisaran Rusia demi supremasi di Asia Tengah. Periode Permainan Besar berlangsung sejak Perjanjian Gulistan antara Rusia dan dinasti Qajar di Persia (1983), hingga Konvensi Inggris-Rusia 1907. Setelah Revolusi Bolshevik pada 1917, konflik Inggris-Rusia kembali terjadi antara Britania Raya dan...

Rusalkas, a type of minor goddesses, represented by Franciszek Siedlecki. In Slavic paganism there are a variety of female tutelary spirits associated with water. They have been compared to the Greek Nymphs,[1] and they may be either white (beneficent) or black (maleficent).[2] They may be called Boginki, Navki, Rusalki, and Vily. The Proto-Slavic root *navь-, which forms one of the names for these beings, means dead,[3] as these minor goddesses are conceived as the s...

 

TechetechetèLogo in uso dal 2022 al 2024PaeseItalia Anno2012 – 2024 Generemontaggio Edizioni13 Puntate951 + 9 Techetechete' Superstar + 3 TechetecheShow + 61 speciali (all'8 settembre 2023) Durata50 min (20:35-21:25) Lingua originaleitaliano RealizzazioneIdeatoreMichele Bovi AutoriMassimiliano Canè, Emilio Levi, Luca Rea, Giulio Calcinari, Ermanno Labianca, Francesco Valitutti, Claudia Di Giuseppe, Francesca Di Ciccio, Tiziana Torti, Claudia Bonadonna, Chiara Morellato, Roberto Patrone, C...

 

Voci principali: Avezzano, Sora (Italia). Questa voce o sezione sull'argomento strade d'Italia non cita le fonti necessarie o quelle presenti sono insufficienti. Puoi migliorare questa voce aggiungendo citazioni da fonti attendibili secondo le linee guida sull'uso delle fonti. Strada statale 690Avezzano-Sora Superstrada del LiriLocalizzazioneStato Italia Regioni Abruzzo Lazio Province L'Aquila Frosinone DatiClassificazioneStrada statale InizioA25 uscita Avezzano...

Questa voce o sezione sull'argomento vini non cita le fonti necessarie o quelle presenti sono insufficienti. Puoi migliorare questa voce aggiungendo citazioni da fonti attendibili secondo le linee guida sull'uso delle fonti. Segui i suggerimenti del progetto di riferimento. Calice a tulipano per spumante Il vino spumante è un vino caratterizzato da rilevante effervescenza, dovuta ad anidride carbonica disciolta. Il termine spumante si riferisce allo sviluppo di spuma, che è una manife...

 

Sceaux 行政国 フランス地域圏 (Région) イル=ド=フランス地域圏県 (département) オー=ド=セーヌ県郡 (arrondissement) アントニー郡小郡 (canton) 小郡庁所在地INSEEコード 92071郵便番号 92330市長(任期) フィリップ・ローラン(2008年-2014年)自治体間連合 (fr) メトロポール・デュ・グラン・パリ人口動態人口 19,679人(2007年)人口密度 5466人/km2住民の呼称 Scéens地理座標 北緯48度4...

 

We Can't Have EverythingSutradaraCecil B. DeMilleProduserCecil B. DeMilleDitulis olehWilliam C. de MilleBerdasarkanWe Can't Have Everythingoleh Rupert HughesPemeranKathlyn WilliamsSinematograferAlvin WyckoffPenyuntingAnne BauchensCecil B. DeMillePerusahaanproduksiFamous Players-LaskyDistributorParamount PicturesTanggal rilis 7 Juli 1918 (1918-07-07) Durasi60 menitNegaraAmerika SerikatBahasaBisu (intertitel Inggris) We Can't Have Everything adalah sebuah film drama bisu Amerika Serikat ta...

Cuban baseball player In this Spanish name, the first or paternal surname is Herrera and the second or maternal family name is Villavicencio. Baseball player Pancho HerreraHerrera in 1961First basemanBorn: (1934-06-16)June 16, 1934Santiago de Las Vegas,[1] CubaDied: April 28, 2005(2005-04-28) (aged 70)Miami, Florida, U.S.Batted: RightThrew: RightMLB debutApril 15, 1958, for the Philadelphia PhilliesLast MLB appearanceOctober 1, 1961, for the ...

 

2020年夏季奥林匹克运动会波兰代表團波兰国旗IOC編碼POLNOC波蘭奧林匹克委員會網站olimpijski.pl(英文)(波兰文)2020年夏季奥林匹克运动会(東京)2021年7月23日至8月8日(受2019冠状病毒病疫情影响推迟,但仍保留原定名称)運動員206參賽項目24个大项旗手开幕式:帕维尔·科热尼奥夫斯基(游泳)和马娅·沃什乔夫斯卡(自行车)[1]闭幕式:卡罗利娜·纳亚(皮划艇)&#...

 

Massachusetts daily newspaper (1845–1967) Daily Evening Traveller, 1866 The Boston Evening Traveller (1845–1967) was a newspaper published in Boston, Massachusetts. It was a daily newspaper, with weekly and semi-weekly editions under a variety of Traveller titles. It was absorbed by the Boston Herald in 1912, and ceased publication in 1967. History Founding The Boston Evening Traveller was launched on April 1, 1845, by Reverend George Punchard and Deacon Ferdinand Andrews. The pair served...

豪栄道 豪太郎 場所入りする豪栄道基礎情報四股名 澤井 豪太郎→豪栄道 豪太郎本名 澤井 豪太郎愛称 ゴウタロウ、豪ちゃん、GAD[1][2]生年月日 (1986-04-06) 1986年4月6日(38歳)出身 大阪府寝屋川市身長 183cm体重 160kgBMI 47.26所属部屋 境川部屋得意技 右四つ・出し投げ・切り返し・外掛け・首投げ・右下手投げ成績現在の番付 引退最高位 東大関生涯戦歴 696勝493敗...

 

Questa voce sull'argomento artisti marziali ungheresi è solo un abbozzo. Contribuisci a migliorarla secondo le convenzioni di Wikipedia. Krisztián TóthNazionalità Ungheria Altezza174 cm Judo Palmarès Competizione Ori Argenti Bronzi Giochi olimpici 0 0 1 Mondiali 0 1 0 Europei 0 1 1 Vedi maggiori dettagliStatistiche aggiornate al 28 luglio 2021 Modifica dati su Wikidata · Manuale Krisztián Tóth (1º maggio 1994) è un judoka ungherese. Indice 1 Palmarès 1.1 Vittori...

 

This article is part of a series onConservatism in Germany Ideologies Agrarian Christian democracy Liberal Ordo Ritter School Monarchism Nationalist Neue Rechte Völkisch Paternalistic State Socialism Prussianism Cameralistic Socialist Revolutionary Young Romanticism Right-Hegelianism Historical School Principles Christian values Duty Elitism Aristocracy Meritocracy Gemeinschaft Heimat In Treue fest Kultur Medievalism Monarchism Organicism  Patriotism Prussian virtues Sittlichkeit Social...

Election 1924 Swedish general election ← 1921 19–21 September 1924 1928 → All 230 seats in the Riksdag   First party Second party Third party   Leader Hjalmar Branting Arvid Lindman Carl Gustaf Ekman Party Social Democrats Electoral League Free-minded Last election 93 62 41 Seats won 104 65 29 Seat change 11 3 12 Popular vote 725,407 461,257 228,913 Percentage 41.09% 26.12% 12.97%   Fourth party Fifth party Sixth party   Leader J...

 

American economist Heidi ShierholzChief Economist of the United States Department of LaborIn officeAugust 27, 2014 – January 20, 2017PresidentBarack ObamaPreceded byJennifer HuntSucceeded byJanelle Jones Personal detailsBorn (1971-10-10) October 10, 1971 (age 52)Political partyDemocraticEducationGrinnell College (BA)Iowa State University (MS)University of Michigan (MA, PhD) Heidi Shierholz (born October 10, 1971) is the president of the Economic Policy Institute, a left-leanin...

 

Classification of sophisticated jet fighter aircraft entering service since 2005 Fifth-generation fighter A Lockheed Martin F-35 Lightning II (top) and Lockheed Martin F-22 Raptor (bottom), two fifth-generation fighters used by the United States Air Force Role Fighter aircraftType of aircraft Manufacturer  China Russia United States First flight 1990 (YF-23) Introduction 2005 (F-22 Raptor) Status In service Developed from Fourth-generation fighter Developed into Sixth-generatio...

  此條目介紹的是中华人民共和国国务院国有资产监督管理委员会管理的特大型中央管理企业集团(简称“中国移动集团”)。关于中国移动集团控股的一家股份制上市公司(简称“中国移动”),请见「中国移动 § 中国移动有限公司」。 中国移动通信集团有限公司China Mobile Communications Group Co.,Ltd.(CMCC)中国移动通信集团公司位于北京金融街29号的总部公司類型...

 

Physical quantity of hot and cold This article is about the physical quantity. For other uses, see Temperature (disambiguation). TemperatureThermal vibration of a segment of protein's alpha helix. Its amplitude increases with temperature.Common symbolsTSI unitKOther units°C, °F, °R, °Rø, °Ré, °N, °D, °L, °WIntensive?YesDerivations fromother quantities p V n R {\displaystyle {\frac {pV}{nR}}} , d q rev d S {\displaystyle {\frac {dq_{\text{rev}}}{dS}}} Dimension Θ {\displ...