As stated in the title, in the c language, there was a question about an array that did not specify the number of elements when declaring the array, so I asked you a question.
For example, in the following example,
So, the result is
You can see from the first line of the above result that the number of elements in text1 is 12 char types for the initial value + '\0'. However, even though strcat() contains more than 11 characters in text1, the result was output without any errors.
Why is this?If you try to store it in an array that exceeds the defined number of elements,
I thought you might get an error like...
If anyone knows anything, please let me know.c
For Microsoft Visual C++, /RTC (Runtime Error Check) is available.If this feature is enabled,
Run-Time Check Failure #2 - Stack around the variable'text1' was corrupted.
A runtime error is detected.
strcat has a security flaw such as the question, so
strcat_s is recommended.
strcat_s is pre-recorded.The receives the buffer size to write to so that it can detect buffer overflows before writing.
I don't know the environment, but if it's old C, the specification is not to check the stack size.
If I'm lucky, I'll just move.
If you are unlucky, you will destroy the area next to you. I don't know if stack over flow is the case, but it's a bad behavior.(Environmental Dependency)
A long time ago, I was lucky enough to move when there was a large unused array next to me.
This is the result of
strcat bulldozing the adjacent memory area of
text1 like a bulldozer.
The code below identifies the adjacent memory space for
[?] part is the memory area adjacent to
strcat cannot specify the size of the array, so it destroys the memory in this way and proceeds.
Note that there are many functions of this specification in C's standard library.
© 2023 OneMinuteCode. All rights reserved.