Because of the work and all aspects of their needs, began to re-learn C, in fact, said again is not too accurate, the original is only in the university contact, and has not been much of the essence of the other development, but it is the necessary to re-learn, the basic part of the experience will be recorded through the blog, for beginners should have some useful Of course, here is only the experience, and can not be used directly to learn C, but should be less to take some detours.
Read a lot of information, and finally my personal understanding is: C + + in the c,c is the essence of the pointer, want to learn pointers to understand the computer memory.
1. Understand the necessary knowledge of memory
The introduction of memory begins with the storage unit; This article was written in the winter of 2014, when the daily life of the storage device we are exposed to is the computer hard disk, mobile phone memory card, the most frequently contacted storage units are MB (read as "Mega") and GB (read as "Ji"). In fact, the smallest storage unit in computer language is bit (read as "bit"), the computer's representation of many data are binary units, and 1 bits represents a binary digit, only 0 or 1;byte is the most commonly used in the computer unit, 8bit 1byte, Many places with 1b represent 1BIT,1B represent 1byte. 1 kilobyte equals 1000byte (10 bytes, or 1024byte, of 1kilobyte equals 2 for binary calculations in the computer). The conversion of some other units is described below:
1 kilobyte KB = $ (10^3) byte
1 megabyte MB = 1 (10^6) byte
1 gigabyte GB = 1 (10^9) byte
1 Terabyte TB = 1 (10^12) byte
1 Petabyte PB = 1 (10^15) byte
1 Exabyte EB = 1 (10^18) byte
1 Zettabyte ZB = 1 (10^21) byte
1 Yottabyte YB = 1 of the 10^24 byte
1 Brontobyte BB = 1 of the 10^27 () byte
1 Nonabyte NB = 1 of the 10^30 of a byte
The above is also the storage medium (such as: hard disk) manufacturers use units, and in the computer system is identified by the use of binary millennia conversion, so when the recognition of the capacity is always smaller, the binary conversion is as follows:
1 kilobyte KB = 1024x768 (2^10) byte
1 megabyte MB = 1 048 576 (2^20) byte
1 gigabyte GB = 1 073 741 824 (2^30) byte
1 terabyte TB = 1 099 511 627 776 (2^40) byte
1 Petabyte PB = 1 899 906 842 624 (2^50) byte
1 Exabyte EB = 1 921 504 606 846 976 (2^60) byte
1 Zettabyte ZB = 1 591 620 717 411 303 424 (2^70) byte
1 Yottabyte YB = 1 208 925 819 614 629 174 706 176 (2^80) byte
1 Brontobyte BB = 1 237 940 039 285 380 274 899 124 224 (2^90) byte
1 Nonabyte NB = 1 267 650 228 229 401 496 703 205 376 (2^100) byte
Of course, in fact, the unit behind the TB is basically not used, these things to understand, remember the computer system in addition to the beginning of the bit and byte, behind the units are 1024 times times the front;
32-bit system in some sense refers to the CPU at a time to handle the maximum number of bits, each bit of the unit is byte, binary computing, so 32-bit system CPU can process the value of 2 32 times, and finally arrive at 4GB, so the 32-bit system supports the theoretical maximum memory of 4GB, Of course, the actual situation may be smaller.
Data types in the 2.C language
The nature of the data type is the alias of a memory block of different sizes, and the different data types correspond to different parsing methods, which is my understanding of the data types, which are illustrated below.
1#include <stdio.h>2#include <stdlib.h>3 4 voidMain ()5 {6printf"%d\n",sizeof(int));7printf"%d\n",sizeof(Char));8System"Pause") ;9}
The above is a more complete command-line window of the code block, a little learning C should be able to understand the above code, the printf function can output a string on the command line, the sizeof function can calculate the type or variable in memory occupies the size (in bytes). A variable is the amount of memory that the type corresponds to after it is defined by a data type. 32-bit program the above code should run as follows ( other bit systems may be different, here only with 32-bit system example ):
4 1 Please press any key to continue ...
This indicates that the int type occupies 1byte in memory 4byte,char. First of all, what is the meaning of the numbers themselves, that is, 4 and 1? In fact, this number indicates how many different values the type can represent, such as int, which is 4 bytes, which is 32bit, and the preceding said that 1bit can represent 0 and 12 values, then int can represent 2 of the 32 different values, that is, 4294967296 different values. Of course, this is not to say that the int type can be set to a maximum of 4294967296, because the int type itself also has negative numbers, in fact, the range of the value of int is half negative, that is, the range is:-2147483648 to +2147483647; sometimes with no negative numbers, All can be preceded by an int with unsigned, or unsigned int, and the range of this type value is: 0 to +4294967295. The following code can be used to test, in such a number 1, will overflow, will make the variable the minimum value of its type.
1#include <stdio.h>2#include <stdlib.h>3 4 voidMain ()5 {6UnsignedintUI =4294967295;7 inti =2147483647;8printf"i=%d\n", i);9printf"ui=%u\n", UI);TenSystem"Pause"); One}
As you can infer, the 1byte memory space that char occupies can represent values ranging from 128 to +127, and similarly, unsigned char can represent a range of values of 0-255. So what's the difference between int and char in addition to the memory space they occupy? Let's look at the following test code:
1#include <stdio.h>2#include <stdlib.h>3 4 voidMain ()5 {6 inti = -;7 intj ='C';8 CharA = -;9 Charb ='b';Tenprintf"as an integer output:"); Oneprintf"i=%d,j=%d,a=%d,b=%d\n", I, J, A, b); Aprintf"as character output:"); -printf"i=%c,j=%c,a=%c,b=%c\n", I, J, A, b); -System"Pause"); the}
First of all, the above code should be compiled through, see the C data type know that int is the data type that stores integers, char is the data type that stores the characters, but the above code can be an int type variable J to assign the value of the character, you can also assign a value integer to the char type variable A, Both of the above 4 variables can be output with%d as integers, and can also be output with%c as a character. This is the so-called parsing method, the data type in addition to its allocation of memory size, the resolution is different, and int and char types of parsing can be common among each other. We all know that the final computer data will be converted to 0 and 1 of the binary data, 0 to 9 of the number is also through the binary conversion, then the character is actually through the digital conversion, binary to decimal we can directly calculate it, but the conversion of the number to the character is artificially prescribed, The table for this rule is the ASCII table. The specific correspondence of the ASCII table is not posted here, all kinds of c materials should have. The above code executes the following results:
As an integer output: i=90,j=99,a=100,b=98 as character output: I=z,j=c,a=d,b=b Press any key to continue ...
int and char types can be converted to each other because of the mapping of the ASCII table, and the test data in our code above is not out of bounds (no more than the value of char type), from the above execution results, we know that the integer 90 in ASCII corresponds to the capital Z, The integer 99 corresponds to the lowercase letter C, the integer 100 corresponds to the lowercase letter D, and the integer 98 corresponds to lowercase b. Because I used the char and int types for testing, it might not be possible to use a float or double type to react to the results of their mapping relationships, because their resolution is completely different.
Four areas of memory
This article mainly memory, before are some fragmented knowledge, now specifically, C language of memory, C language user can manipulate the memory is basically defined variables or through malloc or relloc such functions assigned to, if you want to get the address of the variable, you can pass the & symbol.
C Language Program memory, the traditional meaning is divided into four areas, respectively, code area, data area (global, constant area), stack area (temporary area), heap area.
The code area holds the binary code after the compiler compiles the C language program, and the C language cannot get its address, so there is not much explanation.
Data area, where the global variable is stored, where global variables are placed in the global zone of the data area, constants are placed in the constant area of the global zone, constants cannot get addresses, and global variables can. The area's memory is released after the program ends.
Stack area (temporary area), the program directly defined variables are placed in the stack area, when the function of the variable is completed, the function of the internal definition of the stack will be released (the meaning of the release is no longer correct to get the value of the variable). Each program can use a limited amount of memory in the stack, typically only 1 m. This is also known as a staging area because it is released after the function call is complete.
Heap area, through the allocation of functions such as malloc or Relloc are stored in the heap area, the general use of the heap area is due to the use of large chunks of memory, or if the variable memory can be used at the end of the function call, then use the heap area, The disadvantage of the heap area is that memory needs to be freed manually using the free function, which is prone to memory leaks (meaning that the variable is still occupied by the memory space if it is used by the General Assembly to affect the system or other programs running). The most memory area used in C is the heap area and the stack area. The following code is an example of a memory four-zone variable:
1#include <stdio.h>2#include <stdlib.h>3 4 intx =0;//data area Global zone variables5 intY//data region Global zone variable x, y will free memory at the end of the program6 7 8 int*Examplestack ()9 {Ten inti =0; One intJ//I and J are the stack variables, although I is defined first, but I's memory address is greater than J, at the end of the example function, I and J will be released and no longer accessible A return&i;//return to the stack address, read the value can be found when the value is not 0, indicating that the memory is reassigned. - } - the int*exampleheap () - { - //The variable p itself is in the stack area, storing the first address of the memory allocated by malloc, using * to fetch the value of the address, and *p in the heap area - int*p = (int*) malloc (sizeof(int)); +*p = -; - returnP//return variable p, read value normal + } A at voidMain () - { - int*p =Examplestack (); - int*q =exampleheap (); - Char*str ="ABCD";//str itself is in the stack area, while "ABCD" is in the data area constant area, and STR is read-only -printf"Stack area:%d\n", *p);//the P printed here is a garbage value, indicating that I was freed at the end of the Examplestack inprintf"Heap Area:%d\n", *q);//This prints out 20, indicating that the memory address in the variable q is not released - if(q! = NULL) free (q);//Freeing Memory toQ = NULL;//anti-Wild hands +p = NULL;//anti-Wild hands -System"Pause"); the}
The code in addition to the main main function also has examplestack and EXAMPLEHEAP functions as an example, for convenience as an example also use pointers, the pointer itself does not do too much introduction, do not understand the word can be learned after the pointer knowledge and then look at this code.
This article just summed up their own knowledge of C language, and can not be used as a tutorial, but as a beginner to learn from the learning.
Re-learn the C + + experience-start with memory