Knowledge (XXG)

Static variable

Source đź“ť

1262: 1252: 1242: 1232: 1222: 617: 340: 231: 774:
The compiler allocates required memory space for a declared variable. By using the addressof operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variables have static memory, this way of assigning pointer value to a pointer
425:
which has local scope. A static local variable is different from a local variable as a static local variable is initialized only once no matter how many times the function in which it resides is called and its value is retained and accessible through many calls to the function in which it is
188:
Those data items whose extents lasts as long as the program execution time; such data items have manifest constant Lvalues. Every static data item must have been declared either in a function or routine definition, in a global declaration or as a label set by colon.
166:
This definition is subtly different from a static variable: it only specifies behavior, and hence lifetime, not storage: an own variable can be allocated when a function is first called, for instance, rather than at program load time.
699:(objects), and is accessible as a member variable of these objects. A class variable of a dynamically defined class, in languages where classes can be defined at run time, is allocated when the class is defined and is not static. 298:
can only be used with static variables, because those are the only kinds of variables whose location is known by the compiler at compile time. When the program (
1202: 863: 911: 315: 1255: 1059: 883: 1291: 1245: 1265: 86:(where a variable can be used): "global" and "local" refer to scope, not lifetime, but scope often implies lifetime. In many languages, 118: 834: 811: 664: 387: 278: 638: 361: 252: 152:
quantities will be unchanged from their values at the last exit, while the values of declared variables that are not marked with
1082: 856: 727: 1207: 821: 642: 365: 256: 1067: 905: 762: 1044: 678: 407: 401: 114: 1102: 627: 350: 241: 1286: 1225: 1112: 1092: 945: 849: 646: 631: 369: 354: 260: 245: 1171: 110: 69: 1031: 411: 83: 1235: 1097: 1072: 978: 1049: 1001: 899: 683: 435: 179: 45: 420: 203:
variable (local, stack-allocated), not for heap-allocated objects, which is the current use of the term
1077: 742: 707: 37: 955: 303: 1197: 1181: 1107: 307: 57: 973: 872: 830: 826: 807: 79: 53: 49: 1150: 1145: 988: 715: 431: 292: 211: 61: 31: 1140: 791: 775:
variable is known as static memory allocation. Memory is assigned during compilation time.
732: 711: 692: 295: 87: 410:, static variables have extent the entire run of the program, but may have more limited 1155: 1122: 1117: 963: 922: 795: 703: 696: 688: 91: 1280: 1132: 935: 930: 800: 737: 56:(or "extent") is the entire run of the program. This is in contrast to shorter-lived 710:
of classes are usually allocated statically. A statically defined value can also be
427: 311: 106: 217:
is used in C and related languages both for static variables and other concepts.
1176: 616: 339: 323: 319: 230: 73: 17: 426:
declared, e.g. to be used as a count variable. A static variable may also have
940: 418:, which has global scope and thus is in context throughout the program, and a 299: 65: 148:
This has the following effect: upon a re-entry into the block, the values of
1087: 968: 490:// will get incremented five times after these calls. The final value of |x| 487:// |x| is initialized only once across five calls of |Func| and the variable 1021: 1016: 1006: 996: 130: 706:, are usually allocated statically. In object-oriented programming, the 194:
The BCPL Reference Manual, 7.2 Space Allocation and Extent of Data Items
1011: 199:
Note that BCPL defined a "dynamic data item" for what is now called an
841: 182:, which was heavily influenced by BCPL. The BCPL definition reads: 27:
Programming variable that persists for the lifetime of the program
763:"What is static memory allocation and dynamic memory allocation?" 90:
are always static, but in some languages they are dynamic, while
322:(if uninitialized), and are stored in corresponding sections of 175: 845: 610: 333: 224: 161:
Revised report on ALGOL 60, section "5. Declarations", p. 14
806:(2nd ed.). Upper Saddle River, NJ: Prentice Hall PTR. 144:
A declaration may be marked with the additional declarator
72:
objects, whose storage is allocated and deallocated in
884:
Memory management as a function of an operating system
109:, before the associated program is executed, unlike 1190: 1164: 1131: 1058: 1030: 987: 954: 921: 892: 402:
Variable (computer science) § Scope and extent
799: 438:, which is a form of file scope or module scope. 718:value is used throughout a run for consistency. 702:Object constants known at compile-time, such as 310:into memory, static variables are stored in the 184: 142: 174:to refer to these variables dates at least to 857: 8: 1203:International Symposium on Memory Management 695:of a given class which is shared across all 446:An example of a static local variable in C: 94:are generally automatic, but may be static. 645:. Unsourced material may be challenged and 368:. Unsourced material may be challenged and 259:. Unsourced material may be challenged and 864: 850: 842: 691:" of a statically defined class, i.e., a 665:Learn how and when to remove this message 388:Learn how and when to remove this message 279:Learn how and when to remove this message 117:where memory is allocated as required at 178:(1966), and has been popularized by the 753: 7: 643:adding citations to reliable sources 366:adding citations to reliable sources 257:adding citations to reliable sources 912:Input–output memory management unit 414:. A basic distinction is between a 129:Static variables date at least to 25: 681:, there is also the concept of a 1261: 1260: 1251: 1250: 1241: 1240: 1231: 1230: 1221: 1220: 615: 338: 229: 133:(1960), where they are known as 1083:Concurrent mark sweep collector 728:Constant (computer programming) 714:in its scope ensuring the same 105:is the allocation of memory at 52:"statically", meaning that its 1208:Region-based memory management 1: 1256:Memory management algorithms 1068:Automatic Reference Counting 906:Translation lookaside buffer 822:The C++ Programming Language 1292:Variable (computer science) 1246:Automatic memory management 1045:C dynamic memory allocation 679:object-oriented programming 607:Object-oriented programming 529:// outputs the value of |x| 115:automatic memory allocation 1308: 1266:Memory management software 1113:Tracing garbage collection 946:Virtual memory compression 802:The C Programming Language 399: 29: 1216: 879: 430:or some variant, such as 318:(if initialized), or the 111:dynamic memory allocation 1040:Static memory allocation 1032:Manual memory management 448: 101:static memory allocation 1098:Garbage-first collector 1073:Boehm garbage collector 979:x86 memory segmentation 829:(Addison Wesley, 2000; 64:and deallocated on the 1103:Mark–compact algorithm 900:Memory management unit 684:static member variable 416:static global variable 197: 186:(1) Static data items: 180:C programming language 164: 825:(special edition) by 708:virtual method tables 421:static local variable 70:dynamically allocated 68:; and in contrast to 1050:new and delete (C++) 743:Thread-local storage 639:improve this section 362:improve this section 253:improve this section 170:The use of the word 38:computer programming 956:Memory segmentation 792:Kernighan, Brian W. 82:is contrasted with 60:, whose storage is 58:automatic variables 1198:Automatic variable 1182:Unreachable memory 1108:Reference counting 1078:Cheney's algorithm 1060:Garbage collection 796:Ritchie, Dennis M. 326:prior to loading. 205:dynamic allocation 1287:Memory management 1274: 1273: 1226:Memory management 974:Virtual 8086 mode 873:Memory management 827:Bjarne Stroustrup 675: 674: 667: 398: 397: 390: 314:of the program's 289: 288: 281: 80:Variable lifetime 16:(Redirected from 1299: 1264: 1263: 1254: 1253: 1244: 1243: 1234: 1233: 1224: 1223: 1151:Dangling pointer 1146:Buffer over-read 1118:Strong reference 989:Memory allocator 866: 859: 852: 843: 817: 805: 778: 777: 771: 770: 758: 670: 663: 659: 656: 650: 619: 611: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 432:internal linkage 408:scope and extent 393: 386: 382: 379: 373: 342: 334: 293:absolute address 284: 277: 273: 270: 264: 233: 225: 214: 195: 162: 103: 102: 88:global variables 32:Static (keyword) 21: 18:Static variables 1307: 1306: 1302: 1301: 1300: 1298: 1297: 1296: 1277: 1276: 1275: 1270: 1212: 1186: 1160: 1141:Buffer overflow 1127: 1054: 1026: 983: 950: 917: 888: 875: 870: 840: 814: 790: 786: 781: 768: 766: 760: 759: 755: 751: 733:Global variable 724: 704:string literals 693:member variable 671: 660: 654: 651: 636: 620: 609: 604: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 454:<stdio.h> 453: 450: 444: 404: 394: 383: 377: 374: 359: 343: 332: 296:addressing mode 285: 274: 268: 265: 250: 234: 223: 212: 196: 193: 187: 163: 160: 127: 100: 99: 92:local variables 62:stack allocated 42:static variable 34: 28: 23: 22: 15: 12: 11: 5: 1305: 1303: 1295: 1294: 1289: 1279: 1278: 1272: 1271: 1269: 1268: 1258: 1248: 1238: 1236:Virtual memory 1228: 1217: 1214: 1213: 1211: 1210: 1205: 1200: 1194: 1192: 1188: 1187: 1185: 1184: 1179: 1174: 1168: 1166: 1162: 1161: 1159: 1158: 1156:Stack overflow 1153: 1148: 1143: 1137: 1135: 1129: 1128: 1126: 1125: 1123:Weak reference 1120: 1115: 1110: 1105: 1100: 1095: 1090: 1085: 1080: 1075: 1070: 1064: 1062: 1056: 1055: 1053: 1052: 1047: 1042: 1036: 1034: 1028: 1027: 1025: 1024: 1019: 1014: 1009: 1004: 999: 993: 991: 985: 984: 982: 981: 976: 971: 966: 964:Protected mode 960: 958: 952: 951: 949: 948: 943: 938: 933: 927: 925: 923:Virtual memory 919: 918: 916: 915: 909: 903: 896: 894: 890: 889: 887: 886: 880: 877: 876: 871: 869: 868: 861: 854: 846: 839: 838: 818: 812: 787: 785: 782: 780: 779: 752: 750: 747: 746: 745: 740: 735: 730: 723: 720: 689:class variable 687:, which is a " 673: 672: 623: 621: 614: 608: 605: 449: 443: 440: 396: 395: 346: 344: 337: 331: 328: 287: 286: 237: 235: 228: 222: 219: 191: 158: 156:is undefined. 126: 123: 48:that has been 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1304: 1293: 1290: 1288: 1285: 1284: 1282: 1267: 1259: 1257: 1249: 1247: 1239: 1237: 1229: 1227: 1219: 1218: 1215: 1209: 1206: 1204: 1201: 1199: 1196: 1195: 1193: 1189: 1183: 1180: 1178: 1175: 1173: 1172:Fragmentation 1170: 1169: 1167: 1163: 1157: 1154: 1152: 1149: 1147: 1144: 1142: 1139: 1138: 1136: 1134: 1133:Memory safety 1130: 1124: 1121: 1119: 1116: 1114: 1111: 1109: 1106: 1104: 1101: 1099: 1096: 1094: 1091: 1089: 1086: 1084: 1081: 1079: 1076: 1074: 1071: 1069: 1066: 1065: 1063: 1061: 1057: 1051: 1048: 1046: 1043: 1041: 1038: 1037: 1035: 1033: 1029: 1023: 1020: 1018: 1015: 1013: 1010: 1008: 1005: 1003: 1000: 998: 995: 994: 992: 990: 986: 980: 977: 975: 972: 970: 967: 965: 962: 961: 959: 957: 953: 947: 944: 942: 939: 937: 936:Memory paging 934: 932: 931:Demand paging 929: 928: 926: 924: 920: 913: 910: 907: 904: 901: 898: 897: 895: 891: 885: 882: 881: 878: 874: 867: 862: 860: 855: 853: 848: 847: 844: 836: 835:0-201-70073-5 832: 828: 824: 823: 819: 815: 813:0-13-110362-8 809: 804: 803: 797: 793: 789: 788: 783: 776: 764: 757: 754: 748: 744: 741: 739: 738:Static method 736: 734: 731: 729: 726: 725: 721: 719: 717: 713: 709: 705: 700: 698: 694: 690: 686: 685: 680: 669: 666: 658: 648: 644: 640: 634: 633: 629: 624:This section 622: 618: 613: 612: 606: 493:// will be 5. 447: 441: 439: 437: 433: 429: 424: 422: 417: 413: 409: 403: 392: 389: 381: 371: 367: 363: 357: 356: 352: 347:This section 345: 341: 336: 335: 329: 327: 325: 321: 317: 316:address space 313: 309: 305: 301: 297: 294: 283: 280: 272: 262: 258: 254: 248: 247: 243: 238:This section 236: 232: 227: 226: 220: 218: 216: 208: 206: 202: 190: 183: 181: 177: 173: 168: 157: 155: 151: 147: 141: 139: 137: 132: 124: 122: 120: 116: 112: 108: 104: 95: 93: 89: 85: 81: 77: 75: 71: 67: 63: 59: 55: 51: 47: 43: 39: 33: 19: 1039: 820: 801: 773: 767:. Retrieved 756: 701: 682: 676: 661: 652: 637:Please help 625: 445: 428:module scope 419: 415: 406:In terms of 405: 384: 375: 360:Please help 348: 324:object files 312:data segment 290: 275: 266: 251:Please help 239: 209: 204: 200: 198: 185: 171: 169: 165: 153: 149: 145: 143: 135: 134: 128: 107:compile time 98: 97:In general, 96: 78: 41: 35: 1177:Memory leak 765:. MeritHub 761:Jack Rons. 589:// prints 5 580:// prints 4 571:// prints 3 562:// prints 2 553:// prints 1 320:BSS segment 74:heap memory 1281:Categories 941:Page table 784:References 769:2011-06-16 400:See also: 300:executable 221:Addressing 66:call stack 30:See also: 1088:Finalizer 969:Real mode 716:immutable 697:instances 655:June 2023 626:does not 378:June 2023 349:does not 269:June 2023 240:does not 201:automatic 138:variables 50:allocated 1022:ptmalloc 1017:mimalloc 1007:jemalloc 997:dlmalloc 893:Hardware 798:(1988). 722:See also 511:"%d 451:#include 192:—  159:—  131:ALGOL 60 119:run time 54:lifetime 46:variable 1093:Garbage 1012:libumem 914:(IOMMU) 647:removed 632:sources 442:Example 370:removed 355:sources 304:library 261:removed 246:sources 215:keyword 125:History 1165:Issues 833:  810:  712:global 592:return 517:" 505:printf 469:static 308:loaded 213:static 172:static 1191:Other 1002:Hoard 908:(TLB) 902:(MMU) 749:Notes 412:scope 330:Scope 306:) is 84:scope 44:is a 831:ISBN 808:ISBN 630:any 628:cite 583:Func 574:Func 565:Func 556:Func 547:Func 538:main 460:Func 457:void 353:any 351:cite 291:The 244:any 242:cite 210:The 176:BCPL 146:own. 40:, a 677:In 641:by 586:(); 577:(); 568:(); 559:(); 550:(); 535:int 472:int 434:in 364:by 302:or 255:by 154:own 150:own 136:own 113:or 36:In 1283:: 794:; 772:. 541:() 526:); 514:\n 499:++ 463:() 207:. 140:: 121:. 76:. 865:e 858:t 851:v 837:) 816:. 668:) 662:( 657:) 653:( 649:. 635:. 601:} 598:; 595:0 544:{ 532:} 523:x 520:, 508:( 502:; 496:x 484:; 481:0 478:= 475:x 466:{ 436:C 423:, 391:) 385:( 380:) 376:( 372:. 358:. 282:) 276:( 271:) 267:( 263:. 249:. 20:)

Index

Static variables
Static (keyword)
computer programming
variable
allocated
lifetime
automatic variables
stack allocated
call stack
dynamically allocated
heap memory
Variable lifetime
scope
global variables
local variables
compile time
dynamic memory allocation
automatic memory allocation
run time
ALGOL 60
BCPL
C programming language
static keyword

cite
sources
improve this section
adding citations to reliable sources
removed
Learn how and when to remove this message

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑