Nearly three top internal use invitation code which 35,003,700

Baidu search Ah Yan 57-56-83-12 code: 10771077 Network hct97 point C0M

Before the official start of today's post, look at a piece of code, the code thinking there may be a case of:

Copy the code
int main() 
{
        int  j, b[10];
          for(j = 1; j <= 10; ++j){
                  b[j] = 0;
          }
}
Copy the code

After reading this code, this code if they feel there is no pit, it may wish to take note j ranges from 1 to 10 and b [10] will lead to an array of cross-border b oh. Taking into results yet? Let's look at some memory knowledge of it.

We in three variables in C / C ++ high school: local variables, global variables (static and global variables), stack variables

When the local variables defined variables by a programmer of ordinary compiler stack space in memory assigned memory section, such as:

int b[10], j;

Global variable space allocated by the compiler in the memory static memory, such as:

int x, y; // global

int main () {

  static int m, n; // static

}

Stack variables by a programmer using malloc or new application period of the heap memory, such as:

int * a = new int [10], * i = new int; // dynamic memory allocation

 

 

Visible compiler provided by the memory logically divided into different sections rights programmer access memory, and the size of each segment has its own space, once the size of programmer Oh exhausted in the course, there will be insufficient memory errors, such as a typical Stack OverFlow 

Learn storage variables after, again thinking about a problem, when I defined several different variables in the same variable region, in memory of how they arranged?

For example, I define two local variables

int a,b;

So in the end is the first compiler, or to assign addresses to a b assign addresses? After (this also used to think it? Certainly define the first allocation friends) to assign addresses, in the end is a b address above address, or the address above a b address? That is below two maps which map is correct?

Want to know the answer just these two addresses are printed out to look. Xiao Bian is not printed here, the reader is advised to print their own.

The result is a print of an address higher than the address of b, that is left is correct.

But note here a, b are local variables, i.e. variables are stored in the stack area , based on the results we found that the variables in the stack area defining the variables defined earlier allocated in the high address area, the variable after the definition is assigned in lower address area (left) .

If that is the heap and static memory of it?

static int a, b;

int *a = new int, *b = new int;

 Print address yourself about it.

Found, heap and static memory contrary to the stack area, the heap area variable / static memory, are variables defined in the first address area allocated in the low, the variable defined in a high address area allocation (right).

In conclusion, is a local variable with high deposit low, global / static / low stack variables with low deposit.

(High memory (low memory) describes how the address is occupied (high) address from a high (low) when the lower address to a plurality of simultaneous variable definitions)

(With low understood how: Each variable in the memory cells are assigned a byte address of the variable from a low to a high percentage unit address byte is stored / read data)

Returning now to look at the code above:

int j, b[10];

Two local variables, remember the high-low deposit with it? DIY painting their memory about the distribution of it.

 

 

Clear memory allocation and b j arrays after, look at the code (a figure above the array is actually an array b, drawing changed over time to forget)

Copy the code
int main() 
{
        int  j, b[10];
          for(j = 1; j <= 10; ++j){
                  b[j] = 0;
          }
}
Copy the code

When the j value of 10, b [10] array bounds cause, the result is a result of cross-border b [10] will continue to occupy a high address int (4 bytes) of storage space, it is clear, b [ 10] of the address j occupied, then b [10] = 0 is equivalent to j = 0, results in an endless loop.

(Theoretical results here that is, the actual operation of the array and then b 1-2 bounds memory cells will be accounted for where the address j, the actual operation only need to j <= 10 becomes the condition j <= 11 or j <= to 12)

If the j and b [10] in order to exchange the definition:

int b[10], j;

They will not appear dead cycle Oh, explain why it yourself.

Here we only discuss local variables with high deposit low, do not forget there are low memory global / static / low stack variable to use it.

We will j local variables and b [10] redefined, from the application heap memory, that is,

int *j = new int, *b = new int[10];

Then traverse again, there would not be an infinite loop, oh

Copy the code
int main() 
{
        int *j = new int, *b = new int[10];
        for(*j = 1; *j <= 10; ++*j){
                b[*j] = 0;
        }
     }
Copy the code

In this case j, b are on the heap, and j b located at a relatively low address, no matter how out of bounds b will only continue to occupy high address space, accounting for less than j address. (Custom order a change to the cycle of death, oh) 

Here attached my test code

Copy the code
#include <iostream>
using namespace std;

int x, y;

int main()
{
        cout << "Global variable:\n";
        cout << "&x: " << &x << "  &y: " << &y << endl;

        static int m, n;
        cout << "Static variable:\n";
        cout << "&m: " << &m << "  &n: " << &n << endl;


        cout << "Heap variable:\n";
        int* a = new int[10], *i = new int;
        //int i, a[10];
        for(*i = 1; *i <= 11; ++*i){
                a[*i] = 0;
        }
        for(*i = 0; *i <=11; ++*i){
                cout << "&a[" << *i << "]:" << &a[*i] << endl;
        }
        cout << "&i:" << i << endl;

        the Delete [] a; 
        // the Delete i; // here due to the cross-border account for a space of i, delete [] a fact i have space occupied by the release, then release once the error will be here pointer invaild 

        cout << "Stack variable: \ n-"; 
        int B [10], J; 
        for (J =. 1; J <=. 11; J ++) { 
                B [J] = 0; 
        } 
        for (J = 0; J <=. 11; + J +) { 
                COUT << "& B [" J << << "]:" << & B [J] << endl; 
        } 
        COUT << "& J:" << endl << & J; 

}
Copy the code

 

Guess you like

Origin www.cnblogs.com/asdafw65/p/11825457.html