I know this use-case might sound a bit strange, but I need to understand if it is possible to do something similar to this.
This is my code, and it causes crash on Aborted (core dumped)
:
char *my_str = "Hello World";
my_str = realloc(my_str, 50);
-Can you please help me to understand why it crashes?
-Is there a standard/elegant way to copy the original string to the dynamic memory, except for the below one? It is really important to me to use realloc()
and not malloc() + memcpy()
int len = strlen(my_str);
char *new_str = (char*)malloc(len + 1);
memcpy(new_str, my_str, len + 1);