combing1のブログ -27ページ目

combing1のブログ

ブログの説明を入力します。

[Thanks @ durian little enthusiasm Su translation. If other friends also have good original or translations, you can try to recommend to Bole online. ] Thinking in C language all the stuff from the perspective of computer Nike Free Run 3 Women memory, it is quite helpful. We can imagine the computer memory as a byte array, each memory address represents 1 byte. Let's say that our computer has 4K of memory, this memory array that will have 4096 elements. When we talk about a memory address pointer when it when we are talking Nike Air Max about a phase in the memory array stores a pointer to an element index. 579756 403 Purple White Nike Black Mamba 24 Kobe Sale Dereference a pointer, you'll get an array of values ​​in the index points. All this of course is a lie. Operating adidas adiPure Crazyquick Collegiate Royal/White Q33301 Outlet system memory management is far more complicated this. Memory is not necessarily continuous, not necessarily processed sequentially. But in front of the analogy is a simple way to C language memory of discussion. If 'pointer', 'address' and 'reverse reference' confused, see 'C language pointer 5 minutes tutorial.' // Annotation: 'dereferencing' translation more, we use a 'reverse reference.' We assume that the computer has 4K of memory, open the address of the next Nike Free Run 3 index is 2048. We Air Max 2011 Womens Grey Green declare a new character variable i = 'a'. When the memory of the variable obtained by placing its value, variable names are associated with the memory of this position, our character i would get the value of a storage location in 2048. The characters are single byte so it only takes up the position of the index is 2048. If we use the address operator to i variable (\u0026 amp;), it returns to the index for the 2048 position. If the variable is another type, such as the int, it will occupy 4 bytes, occupy in the array index for the 2048-2051 position. Will use the address-of operator returns the index position of 2048, because the int type, even taking up 4 bytes, but it began in 2048 position. We look at an example: // intialize a char variable, print its address and the next addresschar charvar = '\\ 0'; printf (\u0026 quot; address of charvar =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; charvar)) ; printf (\u0026 quot; address of charvar - 1 =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; charvar - 1)); printf (\u0026 quot; address of charvar + 1 =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; charvar + 1)); // intialize an int variable, print its address and the next addressint intvar = 1; printf (\u0026 quot; address of intvar =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; intvar) ); printf (\u0026 quot; address of intvar - 1 =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; intvar - 1)); printf (\u0026 quot; address of intvar + 1 =% p \\ n \u0026 quot ;, (void * ) (\u0026 amp; intvar + 1)); run will get the following output: address of charvar = 0x7fff9575c05faddress of charvar - 1 = 0x7fff9575c05eaddress of charvar + 1 = 0x7fff9575c060address of intvar = 0x7fff9575c058address of intvar - 1 = 0x7fff9575c054address of intvar + 1 = 0x7fff9575c05c In the first example of the 1-5 line, we declare a character variable, and print out the address of Nike Lebron 10(X) the character, and then print the two addresses in memory before and after the variable is located. We are using the \u0026 amp; operator and +1 or -1 to get around the two addresses. In the second example 7-11 row we have done similar things, in addition to declare a variable of type int, print out its address and address it immediately before and after. In the output, we see that the address is in hexadecimal. It is worth noting that a difference of 1 byte characters before and after the address. int variables address after a difference of four bytes. Algorithm, pointer arithmetic memory address, are based on the size of the referenced type. A given type size is dependent on the platform, and our example of char is 1 byte, int is four bytes. The character of the address is an address change address before -1, while the int type address, -1, is before the address four addresses. In our example, we use the address operator to get the address of a variable, which represents a pointer variable and use the address is the same effect. English original Bo comment has been proposed: storage \u0026 amp; charvar-1 (an illegal address because it is located before the array) is technically unspecified behavior. C standard has been declared, not particularly pointed out and an illegal address in some storage platform will cause an error. Array address in the C language, arrays are contiguous area of ​​memory, which stores the value of a lot of the same data type (int, long, * char, etc.). When many programmers use C, it will be the first time the array as a pointer. That is wrong. Store a simple pointer memory address, Air Max 2011 Womens Purple Black Grey while an array is a contiguous area of ​​memory to store multiple values. // Initialize an array of intsint numbers [5] = {1,2,3,4,5}; int i = 0; // print the address of the array variableprintf (\u0026 quot; numbers =% p \\ n \u0026 quot ;, numbers ); // print addresses of each array indexdo {printf (\u0026 quot; numbers [% u] =% p \\ n \u0026 quot ;, i, (void *) (\u0026 amp; numbers [i])); i ++;} while (i \u0026 lt ; 5); // print the size of the arrayprintf (\u0026 quot; sizeof (numbers) =% lu \\ n \u0026 quot ;, sizeof (numbers)); Air Jordan Outlet run will get the following output: numbers = 0x7fff0815c0e0numbers [0] = 0x7fff0815c0e0numbers [1] = 0x7fff0815c0e4numbers [2] = 0x7fff0815c0e8numbers [3] = 0x7fff0815c0ecnumbers [4] = 0x7fff0815c0f0sizeof (numbers) = 20 In this example, we initialize an array of 5 int elements contained, we print the address of the array itself, note that we do not using the address-of operator \u0026 amp;. This is because the array variable has represented the address Nike Jordan Melo B Mo of the array's first element. You will see the same address as the first element of the array is the same array. Then we traverse the array and print memory address of each element. In our computer int is four bytes, the memory array is continuous, so the difference between each of the int type 4 element address. In the last line, we print the size of the array, the size of the array is equal to sizeof (type) multiplied by the number of array elements. Here there is an array of 5 int variables each occupy 4 bytes, the entire array size is 20 bytes. Address structure in the C language, the structure is generally contiguous area of ​​memory, but not necessarily absolutely continuous area. And an array of similar, they can store a variety of data types, but unlike arrays is that they can store different data types. struct measure {char category; int width; int height;}; // declare and populate the structstruct measure Air Max 2011 Womens Red ball; ball.category = 'C'; ball.width = 5; ball.height = 3; // print the addresses of the struct and its membersprintf (\u0026 quot; address of ball =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; ball)); printf (\u0026 quot; address of ball.category =% p \\ n \u0026 quot ;, (void *) ( \u0026 amp; ball.category)); printf (\u0026 quot; address of ball.width =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; ball.width)); printf (\u0026 quot; address of ball.height =% p \\ n \u0026 quot ;, (void *) (\u0026 amp; ball.height)); // print the size of the structprintf (\u0026 quot; sizeof (ball) =% lu \\ n \u0026 quot ;, sizeof (ball)); output after running the following : address of ball = 0x7fffd1510060address of ball.category = 0x7fffd1510060address of ball.width = 0x7fffd1510064address of ball.height = 0x7fffd1510068sizeof (ball) = 12 In this example we define a structure measure, then declare an instance of this structure ball, we assign to it 2015 Nike Free 5.0 width, height and category members, and then print out the ball address. And an array of similar structures also represents the first element of its address. It then prints the address of Nike Free Run 3 Women each member. The first category is a member that the ball has the same address. width followed by height, they all have higher than category address. You might think because the category is a character, and Nike Free 5.0 V2 Women 586590-300 Nike Kobe 8 System Elite GC Poison Green Superhero Outlet character variable occupies one byte, so the width of the address should start high than a byte. From the output perspective it wrong. According to the C99 standard (§6.7.2.1), the boundary alignment, the structure can be increased to the members of padding bytes. It does not record data members, but will add extra bytes. In practice, most compilers will make the structure of the largest members of each member of the same size and structure, in our example, you can see the char actually occupies 4 bytes, the entire struct occupy 12 bytes. What happened? 1.struct variable points to the first element of the struct address 2. Do not make assumptions about the members of a structure with respect to the other members how much memory offset possible byte boundary between structure members, or the compiler may also be that they on non-contiguous memory space. Using the address-of operator \u0026 amp; address to get members 3. Use sizeof (struct instance) to obtain the total size of the struct can not be assumed that it is the size of the total size of each member, and perhaps fill bytes. Conclusions like this blog can help you understand more how to operate in C different data types of addresses. In a future post, we will continue to study the basic pointers and arrays.C language-based memory address