'C : testing stack size
For educational purposes, I trying to generate a segmentation fault for stack exeeding size. After recovering the stack size with ulimits -s (telling me that the maximum dimensions is 8192 kB), I tried the following minimal code:
int main()
{
char v[1024*8192];
return 0;
}
Effectively, executing it after gcc file.c -O0 it raises a segmentation fault. However, it returns seg. fault also with a smaller size, e.g., 1024*8190. I can think that a part of the stack is reserved with values needed for the execution. But, curiously, if I give value as 1024*8184 or similar and execute the resulting program several times, it raises seg.fault sometimes yes and sometimes no. If the stack occupation by the program is the same, why sometimes it raises segmentation fault and other times no?
UPDATE: as requested, the used code is exactly what I have written.
gcc version is gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)
the used compile command is gcc file.c -O0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
