Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Composition and dynamic memory allocation C program     - Can not empty the Recycle Bin to repair problems in Ubuntu 14.04 (Linux)

- How to migrate MySQL to MariaDB under linux (Database)

- Linux kernel update error, update-initramfs: failed Solution (Linux)

- Java reflection by calling the class method (Programming)

- Linux / Unix desktops interesting: Christmas tree on the terminal (Linux)

- Safety testing Unix and Linux server entry succinctly (Linux)

- MySQL master-slave delay problem (Database)

- Android Service service applications and the phone SMS Listener Listener (Programming)

- Generic mechanism C11 standard (Programming)

- Using open source software to build XWiki Wiki system installed within the company (Linux)

- Linux 64-bit porting (Programming)

- Sublime Text 3 best features, plug-ins and settings (Linux)

- Android judgment toward camera pictures (Programming)

- Recovery from MySQL master data consistency summary (Database)

- grep command Series: How to Use the grep command in Linux / UNIX (Linux)

- Security Knowledge: How to hide a backdoor PHP file tips (Linux)

- Linux operating system security can not be ignored (Linux)

- Standardized UNIX command-line tool (Linux)

- Android Application Development: Genymotion can not start solving (Linux)

- Hadoop vs spark (Server)

 
         
  Composition and dynamic memory allocation C program
     
  Add Date : 2018-11-21      
         
         
         
  For a program, the general understanding is that the source code compiled into machine code, and then interpret run through the machine. But how is compiled into machine code, and how it works, no doubt, is a question worth exploring. How compiled into machine code, the process is to compile the source code, linking, the compiler to do these things. And how to run, which is not single-handedly own the device can be done. Machine code to run on the machine, you have to request hardware resources. Involving the most is the CPU and memory. CPU logic control and computing, memory for fast interactive place during the operation of the data.

A C program from the point of view of the code structure itself, but after the compiler is a piece of code. And this code is loaded from disk into memory system is called the code segment or a text segment (code / text segment) place. Text segment in memory is shared. So, when we run multiple processes of the same program in memory, only a copy of the program code. Of course, in order to prevent people to do bad things, permission text segment often are read-only.

Only a strategy, while men generals without soldiers on the battlefield is the do nothing. Code, of course, not all purely logical description. Also we have to have the role of logic objects and results - data. That program variables. Different variables, status in the program looks in addition to the scope and lifetime, but much the same. However, their implementation is indeed very different.

Program uninitialized global variables are stored in a data segment is called uninitialized place. That is, bbs (block started by symbol) segments. At the same time, the kernel will automatically remove data in this segment is initialized to zero or null pointer. For example, outside the function declaration:

int sum [10];

So that the variable is stored in the bbs section.

Since there is uninitialized global variables, of course, also have to initialize the global variables. They are stored in a place called the initialized data section (often referred to as data segments). Life cycle have global scope of the whole program. (? Right) include any statement outside the function:

int tmp = 99;

So that the variable is stored in the data segment.

Of course, there is a thing called the stack segment. Automatic variable and each function call is required to save the information are stored here. Stack is characterized by last-out (FILO) and data stored in the short period that push and pop operations are frequent. Automatic variables are often used as temporary variables exist, there is a short period, so when placed on the stack. (Right?) And field information function call, you can use the FIFO stack characteristics very convenient for the protection and recovery. Typically, a recursive implementation is to use the stack because the stack is a layer of growth downward, so the subroutine is not overwrite the parameters of the calling function. To give special attention to the fact that the main function main variables, but also automatic variables. Because the main function is the function ah!

Finally, as the C and C ++ specific dynamic memory allocation. At runtime, use dynamic heap memory allocation. So dynamically allocated memory has a global scope (assigned from the beginning). The lifetime but until it is released before. Dynamic memory allocation, in fact, a request to the kernel memory resources are released, that is, the resources returned to the kernel. So dynamically allocated memory must be released when no longer needed. Otherwise it may cause annoying problems and dynamic allocation of memory leaks and other failures.

See here, we can find. A C program, its code is loaded into the code segment of memory, and its code segment variables one by one, and there is no actual store data, depending on how it is stored in the bbs segment, data segment, stack segment and heap snippet variable is stored in a pointer to the actual storage of various parts of the pointer.

Let me say a C dynamic memory management. In C, the main function of several standard header < stdlib.h> defined in the memory management:

void * malloc (size_t size)

void * calloc (size_t nobj, size_t size)

void * realloc (void * p, size_t size)

void * free (void * p)

The above four function that returns a void * pointer. In C, void * pointer can receive any type of pointer, at the same time, may not be passed directly through to cast any type of pointer. In C ++, the case of the former, the latter must be cast.

Let us talk about malloc, it receives a parameter of type size_t as the size of memory request in bytes. Therefore, memory allocation, it is common to use the sizeof operator to obtain the type of data to be allocated size. Such as:

int * p = malloc (sizeof (int));

As an int pointer p is assigned a defined memory area.

calloc function is used to allocate memory for an array object, the first parameter is the size of the array, the second parameter is the size of each data type. Usually calloc used much, can directly replace malloc.

realloc function for adjusting the size of the allocated memory. When you find your current memory too much or too small, you can use realloc to adjust the memory.

All three memory allocation function failure returns NULL, so you can check the return value to determine the memory allocation is successful.

Finally, free function for releasing the above three functions dynamically allocated memory. And must be released.

Achieve C memory allocation function is accomplished by the system. Therefore, different systems have different implementations. UNIX-like systems, usually called by the sbrk system to achieve. (more detail?)

In C ++, memory management with major operators new and delete. C is relatively more convenient, and more efficient. However, there was a place I did not understand is why the STL memory manager to write a single allocator to achieve. Or new and delete is not strong enough? Ah, after all, do not understand the new and delete implementation.
     
         
         
         
  More:      
 
- Firewall - Internet Militarization (Linux)
- Linux / CentOS 7.0 installation and configuration under Tomcat 8.0 (Server)
- Java reflection mechanism explained in detail and Method.invoke explanation (Programming)
- Java Annotation Comments (Programming)
- Linux maximum number of threads and limit the number of queries the current thread (Linux)
- mysql_config_editor encryption and decryption of the new features of MySQL realization (Database)
- Struts2 : combobox label use (Programming)
- Linux System Getting Started tutorial: Ubuntu desktop using the command line to change the system proxy settings (Linux)
- Upgrading to MySQL 5.7.9 MySQL 5.6.23 (Database)
- Use Ansible to bulk manage remote servers (Server)
- Timing task Crontab under Linux system (Linux)
- CentOS 6 Install Xen4.2 Virtualization Practice (Linux)
- Use regular expressions to check whether the input box to enter a URL (Programming)
- Spark On YARN cluster installation deployment (Server)
- Automatic and Manual Proxy Settings Switch GNOME Shell Extension Proxy Switcher Recommend (Linux)
- Execute command sentence can result in equipment permanently bricked in Linux laptop (Linux)
- Simple comparison of MySQL and Oracle in a sql analytical details (Database)
- Linux and Windows virtual machine shared folders ---- Linux install VMware tools (Linux)
- Use OpenSSL carried BASE64 encoding and decoding (Linux)
- A custom implementation of the Android sidebar (Programming)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.