I have been unable to tolerate the slow pace of progress in programming learning, but I am always very inefficient learning, so from now on, in this series of blogs, accumulated record in the process of programming experience and reflection, hoping to really let themselves grow up quickly.
Make up your mind, be patient, focus on the present, and be firm in your fear.
Acquisition
The best way to learn other people's code is to step into the program to debug, understand the general framework and process, and then go deep into the study
The trap of strlen () in C language
The role of strlen () is to get the number of valid characters for a string, but when the character assigned to a string is greater than the length of the string, it is possible to overwrite the end of the character array with the ' the ' ' of ', so that the end of the string is not represented in contiguous memory.
The end result is that the size of the strlen (buffer) is larger than the memory defined by the buffer itself, making it out of bounds.
Workaround:
Char *buffer = new char[maxbuflen+1];
memset (Buffer, 0, maxbuflen+1]);
_snprintf (Buffer, Maxbuflen, "%s", temp); _
According to the above, when defining a character array to keep one to prevent the crossing, when assigning a value to a character array, it is based on the size of the Maxbuflen assignment, so that the buffer does not overflow.
SSCANF () function
function definition int sscanf (const char str,const char format,........);
Function description
SSCANF () Converts the string of parameter str according to the parameter format string and formats the data.
The converted results are stored in the corresponding parameters.
Access () function
function definition int access (const char *filename,int amode);
The Amode parameter of 0 indicates the existence of the check file, and if the file exists, returns 0, does not exist, returns-1.
This function can also check other file properties:
06 Check Read and Write permissions
04 Check Read permissions
02 Check Write permission
01 Check Execution Permissions
00 checking the existence of files
And this file does not have Read permission, you can determine whether the file exists in the
Existence returns 0, does not exist return-1
What is the difference between heap and stack
The main difference consists of the following points:
1. Different management methods;
2, the space size is different;
3. can produce different fragments;
4, the growth direction is different;
5. Different distribution methods;
6. Different distribution efficiency;
Management mode:
For the stack, it is managed by the compiler without our manual control, and for the heap, the release work is controlled by the programmer, which is apt to generate memory leaks (memory leak).
Space size:
Generally in 32-bit system, heap memory can reach 4G of space, from this point of view heap memory is almost no limit. But for the stack, generally there is a certain amount of space, you can set up in the integrated development environment.
Fragmentation problem:
For the heap, frequent new/delete is bound to cause the memory space discontinuity, resulting in a large number of fragments, so that the program efficiency is reduced. For the stack, this problem will not exist, because the stack is the advanced queue, they are so one by one corresponding, so that there will never be a memory block from the middle of the stack pop-up, before he pops up, on top of his last stack content has been ejected.
Growth direction:
For the heap, the growth direction is upward, that is, the direction of the increase to the memory address, for the stack, it is the direction of growth is downward, the memory address to reduce the direction of growth.
Distribution mode:
The heap is dynamically allocated, and there is no statically allocated heap. There are 2 ways to allocate the stack: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is allocated by the Alloca function, but the dynamic allocation and heap of the stack are different, and his dynamic allocation is released by the compiler without our manual implementation.
Allocation efficiency:
Stack is the machine system provides the data structure, the computer will support the stack at the bottom: the allocation of special registers to store the stack address, pressure stack out of the stack have specific instructions to execute, which determines the stack of high efficiency.
The heap is provided by the C + + function library, and its mechanism is very complex, for example, in order to allocate a piece of memory, the library function will search the heap memory for the available space of sufficient size, if there is not enough space (possibly due to too much memory fragmentation), It is possible to call the system function to increase the memory space of the program data segment, so that there is a chance to get enough memory and then return.
Obviously, the heap is much less efficient than the stack.
On the stack
System response after application
Stack: As long as the remaining space of the stack is larger than the application space, the system will provide memory for the program, otherwise it will be reported abnormal stack overflow.
Heap: First you should know that the operating system has a record of the free memory address of the list, when the system received the application of the program, will traverse the list, looking for the first space is larger than the requested space heap.
node, and then remove the node from the list of free nodes. The space of the node is assigned to the program, and for most systems, the size of this assignment is recorded at the first address in the memory space, so that the DELETE statement in the code can properly release the memory space.
In addition, because the size of the found heap node does not necessarily equal the size of the application, the system automatically puts the extra part back into the free list. In other words, the heap will have to do some follow-up work after the application, which will lead to application efficiency problems.
Application size Limit
Stacks: In Windows, stacks are data structures that extend to a low address and are a contiguous area of memory. The address of the top of the stack and the maximum capacity of the stack are predetermined by the system, in Windows, the size of the stack is 2M (also some say 1M, in short, a compile-time constant), if the application space over the stack of remaining space, will prompt overflow. As a result, the space available from the stack is small.
Heap: The heap is a data structure that is extended to a high address and is a contiguous area of memory. This is because the system is used to store the free memory address of the list, nature is discontinuous, and the link list of the traversal direction is from the low address to the high address. The size of the heap is limited by the virtual memory available in the computer system. This shows that the heap to obtain a more flexible space, but also relatively large.
Storage content in heaps and stacks
Stacks: When a function is called, the first stack is the address of the next instruction (the next executable statement of the function call statement) after the function call in the main function. Then there are the parameters of the function, in most C compilers, the parameters are pushed from right to left, and then the local variables in the function. Note that static variables are not in the stack.
When the function call is finished, the local variable first goes out of the stack, then the argument, and the last stack pointer points to the address that was first saved, the next instruction in the main function, where the program continues to run.
Heap: The size of the heap is usually stored in a byte at the head of the heap. The specifics of the heap are arranged by the programmer.