Escribiendo más caracteres que mallocedidos. ¿Por qué no falla?
¿Por qué funciona lo siguiente y no arroja algún tipo de falla de segmentación?
char *path = "/usr/bin/";
char *random = "012";
// path + random + \0
// so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit)
newPath = (char *) malloc(strlen(path) + strlen(random) + 1);
strcat(newPath, path);
strcat(newPath, "random");
// newPath is now: "/usr/bin/012\0" which makes 13 characters.
Sin embargo, si agrego
strcat(newPath, "RANDOMBUNNIES");
¿no debería fallar esta llamada, porque strcat usa más memoria de la asignada? En consecuencia, no debería
free(newPath)
también falla porque intenta liberar 16 bytes pero usé 26 bytes ("/ usr / bin / 012RANDOMBUNNIES \ 0")?
¡Muchas gracias de antemano