Interview Frequently asked C-language variables in memory distribution (VC6.0)

Source: Internet
Author: User

C + + variable in memory distribution in the written test often test, although simple, but also easy to forget, so make a summary in order to deepen the impression.

One:

#include <stdio.h> #include <malloc.h>int g_i = 100;int G_j = 200;int G_k, g_h;int Main () {const int MAXN = 100; int *p = (int*) malloc (MAXN * sizeof (int)); static int s_i = 5;static int s_j = 10;static int s_k;static int S_h;int *pi = n  ew int (1), int *pj = new int (1), int i = 5;int j = 10;int k = 20;int F, h;char *pstr1 = "MoreWindows123456789"; char *pstr2 = "MoreWindows123456789"; char *pstr3 = "Hello";p rintf ("Data address in Heap: 0x%08x\n", p);p rintf ("Data address in Heap: 0x%08x\n", pi);p rintf (" Data address in Heap: 0x%08x\n ", PJ);p Utchar (' \ n ');p rintf (" Data address in stack (with initial value): 0x%08x =%d\n ", &i, i);p rintf (" Data address in stack (with initial value): 0x%08x =%d\n " , &j, J);p rintf ("Data address in stack (with initial value): 0x%08x =%d\n", &k, K);p rintf ("Data address in stack (no initial value): 0x%08x =%d\n", &f, F);p rintf (" Data address in stack (no initial value): 0x%08x =%d\n ", &h, h);p Utchar (' \ n ');p rintf (" Static data address (with initial value): 0x%08x =%d\n ", &s_i, s_i);p rintf (" static data address (with initial value): 0x%08x =%d\n ", &s_j, S_j);p rintf (" Static data address (no initial value): 0x%08x =%d\n ", &s_k, S_k);p rintf (" Static data address (no initial value): 0x%08x =%d\ N ", &s_h, S_h);p Utchar (' \ n ');p rintf (" Global data address (with initial value): 0x%08x =%d\n ", &g_i, G_i);p rintf (" Global data address (with initial value): 0x%08x =%d\n ", &g_j, G_j);p rintf (" Global data address (without initial value): 0x%08x =%d\n ", & Amp;g_k, G_k);p rintf ("Global data address (without initial value): 0x%08x =%d\n", &g_h, G_h);p Utchar (' \ n ');p rintf ("String constant data address: 0x%08x points to 0x%08x content is- %s\n ", &pstr1, Pstr1, pstr1);p rintf (" String constant data address: 0x%08x points to 0x%08x content for-%s\n ", &AMP;PSTR2, PSTR2, PSTR2);p rintf (" string constant data address: 0x%08x points to 0x%08x content for-%s\n ", &AMP;PSTR3, PSTR3, PSTR3); free (p); return 0;}

Run results(The results (relative position) of different machines will be slightly different, because the distribution of stacks and global variables is determined by the operating system)


In summary, string constants and data in the stack are contiguous (the stack has no initial values always together), there are initial values global and static variables together, no disposition of global and static variables together, and adjacent; The heap itself again occupies a piece, and is not continuous, is the form of the list. In addition to the variables in the stack (local variables and parameters), the first declared address is high, then declared at low.

Two:

#include <stdio.h>void fun (int i) {int j = i;static int s_i = 100;static int s_j;printf ("parameter of the child function:        0x%p =%d\n", &A Mp;i, i);p rintf ("Data address in the child function stack: 0x%p =%d\n", &j, J);p rintf ("Child function static data address (with initial value): 0x%p =%d\n", &s_i, s_i);p rintf ("Sub-function static Data address (no initial value): 0x%p =%d\n ", &s_j, S_j);} int main () {int i = 5;static int s_i = 100;static int s_j;printf ("Data address in main function stack: 0x%p =%d\n", &i, i);p rintf ("main function static data address (with Initial value): 0x%p =%d\n ", &s_i, s_i);p rintf (" sub-function static data address (no initial value): 0x%p =%d\n ", &s_j, S_j);p Utchar (' \ n '); fun (i); return 0;}


In short, the address of the stack in the main function is higher than the parameters and the stack address in the child function, which proves that the extension direction of the stack is extended from high address to low address. The address of the static data in the main function and the child function is also adjacent, stating that the program assigns the initialized global variable and the argument variable together, and the uninitialized global variable and the argument variable are assigned in another.

Three knowledge Additions:

How many areas does C + + memory divide?
1. Stack (stack)-Automatically allocated by the compiler to release, store the function parameter value, local variable value and so on. It operates in a manner similar to a stack in a data structure.
2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.
3, Global Zone (Static)-, the storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. -The system is released after the program finishes.
4, literal constant area-the constant string is put here. Released by the system after the program is finished.
5. Program code area-binary code to store function body


1 variables can be stored in different parts of the memory, depending on their generation period. variables defined in the upper part of the function (global variables or static external variables) and static variables defined inside the function, whose lifetime is the whole process of running the program . These variables are stored in the data Segment. A data segment is a fixed amount of space in memory for these variables, which is divided into two parts, one for initializing variables, and the other to holding uninitialized variables.
2 The generation period of the auto variable defined inside the function (a variable not defined with the keyword static) starts when the program starts executing its block code, until the program leaves the block. A variable that is a function parameter exists only during the call to the function. These variables are stored in the stack. A stack is a piece of space in memory that starts small and gradually becomes larger automatically until a predefined boundary is reached.
3 When assigning an address space to a pointer using a function such as malloc, the allocated memory block is in a memory space called the heap. The heap starts small, but it grows when you call a memory allocation function such as malloc or Clloc. a heap can share a memory segment with a data segment or stack, or it can have its own segment of memory, depending entirely on the compilation options and operating system. like stacks, heaps also have a growth limit, and the rules that determine this boundary are the same as the stacks.


Interview Frequently asked C-language variables in memory distribution (VC6.0)

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.