Character array dynamically allocated space and statically allocated space

Dynamically allocated space and statically allocated space, the most intuitive understanding is to allocate space size when writing code or dynamically allocate space size at runtime. The former cannot be changed after the space allocation is completed, and the latter can allocate the space according to its own needs at runtime. E.g:

char buffer[1000]; //Static allocation  
  
/*  
 *Dynamic allocation  
 */  
scanf(“%d”,&MAXSIZE);  
char *buffer=(char *)malloc(sizeof(char)*MAXSIZE);
The biggest difference between dynamic allocation and static allocation is the difference in memory space. The dynamic allocation system allocates the heap area in memory, and the static allocation allocates the stack area in memory. For char, if the value of MAXSIZE is not very large, that is, when the length of the string is not very large, the difference between the two may not be reflected, then we will open up the array space to a very large size, for example, 10M, which is 10 *1024*1024 size, then many systems will report Segmentation fault for static allocation when running, but the above error will not occur for dynamic allocation.

Below I have made some analysis on the above problem (only for gcc environment under Linux),

For static allocation, the stack area in memory is occupied, and the system's limit on the size of the stack area determines the space for error reporting during static allocation.

First, let's take a look at the system's limit on the size of the Stack space:

$ulimit -a |grep stack  
stack size                     (kbytes, -s) 8192  
We found that the limit size for the stack area is 8MB, but the stack space used by our program is indeed 10MB, so it will inevitably lead to a segmentation fault at runtime, so now there are two solutions to this problem. The first is to give up static allocation and use dynamic allocation instead. For larger areas, it is recommended to use the heap area, because if a stack overflow occurs, it is easy to cause some vulnerabilities, resulting in system permissions being attacked.

The second is to insist on exploring the solution, then all you have to do now is to temporarily modify the size of the stack, using the following command:

$ulimit -s 20480                             #20M  
$ulimit -s  
20480  
In this way, the system's limit on the maximum stack space is temporarily modified, and no error will be reported when compiling and running.

For the second method is only suitable for exploring this problem, the first method is recommended, which is safer.

Finally, add the default stack space size for linux to view and modify threads: ulimit -s
1. Use the command ulimit -s to view the default stack space size of linux. By default, it is 10240, which is 10M
. 2. Use the command ulimit -s to temporarily change the size value. Stack space size: ulimit -s 102400, that is, modify it to 100M
3. You can add ulimit -s 102400 in /etc/rc.local to set the stack space size at boot


Many times it will be found that some problems will not appear when there are not many values, but when we increase the value, many unknown errors will appear. When we solve these problems, there will be new gains.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324799668&siteId=291194637