https://dpaste.org/v59Da
In this code I thought it will throw stack error, but it runs without error.But when I change array size of got to 15, it throws stack error.
My question is this: How 21 letter size string could settle into 16 size RAM.And if can, why 15 size RAM couldn't?
In the given code, you have two character arrays and you are concatenating one string to another using strcat. Let's break down what is happening:
char got[16] = "Kral \"iyidir\""; // declares an array of 16 characters and initializes it with the string "Kral "iyidir""
char kralis[] = "Heheheha"; // declares an array to hold the string "Heheheha"
strcat(got, kralis); // concatenates the string in kralis to the end of got
printf("%ld, %s", sizeof(got), got); // prints the size of the array got and its contents
1. Initialization and Concatenation:
The array got is initialized with the string "Kral \"iyidir\"". This string is 13 characters long (Kral (5) + \"iyidir\" (8)) plus the null terminator (\0), making it 14 characters in total.
When you use strcat(got, kralis), the string "Heheheha" (8 characters + 1 null terminator) is concatenated to the end of got.
2. Array Sizes:
- got is declared as char got[16]. It has space for 16 characters, including the null terminator.
- kralis is declared and initialized as char kralis[] = "Heheheha". It has space for 9 characters, including the null terminator.
3. Concatenation Result:
After concatenation, got will contain "Kral "iyidir"Heheheha". This string is 14 (initial string) + 8 (kralis) = 22 characters long. However, because got was initially declared with a size of 16, it can only store up to 15 characters plus the null terminator.
This results in buffer overflow and undefined behavior because you are writing beyond the allocated memory of got. The fact that it runs without an error when got is of size 16 is due to the specifics of how the memory is managed and used in your environment. It is not safe and will likely lead to a crash or other unpredictable behavior.
4. Why it crashes with size 15:
When got is changed to char got[15], the buffer size is even smaller. It can only hold 14 characters plus the null terminator. Given that the initial string "Kral \"iyidir\"" is already 14 characters, any attempt to concatenate more characters will immediately overflow the buffer and result in a more likely and immediate crash.
Key Points:
- Buffer Size: Always ensure that the buffer you are writing to has enough space to accommodate the entire resulting string including the null terminator.
- Undefined Behavior: Writing beyond the allocated memory leads to undefined behavior which can manifest differently depending on the environment and specifics of memory layout at runtime.
- Safe Practices: Use functions like strncat to avoid buffer overflows and always check the buffer sizes.
Corrected Code Example:
To avoid such issues, you should allocate enough space or use safe string functions:
char got[24] = "Kral \"iyidir\""; // 24 to safely include additional strings
char kralis[] = "Heheheha";
strncat(got, kralis, sizeof(got) - strlen(got) - 1); // ensure not to exceed the buffer size
printf("%ld, %s", sizeof(got), got);
This ensures that got has enough space to hold the entire resulting string, and strncat ensures that the buffer is not overflowed.