Hello, Im pretty new to C.
Im allocating memory for a string of length 38 (+ null terminator) wit malloc. I then iterate over the blocks of memory Ive allocated to set the values of each char in the string.
However, no matter what number I give to malloc, I am still capable to set the value of each char in the string.
For example,
char* str = malloc(38 * sizeof(char)). // 37 character string, and null-terminator vs char* str2 = malloc(1); // Only enough memory for one char.
using malloc(1), Id expect that as I iterate from 1 to 38 over str2, Id get a seg-fault very quickly. But Im not, and I end up successfully initialize the 38-character string on only 1 byte of memory. Can anyone explain what is going on?
My code is in the pastebin below to perpetuate formatting:
https://pastebin.com/jNPTF7nE
Thanks for any help! Im compiling with "gcc -g -pedantic -o 1.5 1.5.c" Explanation of my code: the convert function replaces every occurrence of a space with the string "%20"
|