0

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);
this
  • 5,229
  • 1
  • 22
  • 51
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • You cannot `realloc` a constant string. You will need to copy it and modify that. – Mgetz Apr 30 '14 at 20:08
  • 10
    The fundamental rule of `realloc` is *only `realloc` something that was `malloc`d in the first place*. You're breaking that rule. If it hurts when you do that, don't do that. – Eric Lippert Apr 30 '14 at 20:08
  • 4
    @ShafikYaghmour: I know. And it can also be a pointer that was `calloc`d. Let's not confuse the issue further with obscure details when the user doesn't have the basics down yet. – Eric Lippert Apr 30 '14 at 20:10
  • Why not to write these comments as answers and rate this question with better rate? Don't you think it's good that other programmers would see that this use case is not possible? – SomethingSomething Apr 30 '14 at 20:10
  • @EricLippert sure but it is not just for the OP, it should be for everyone reading the question. – Shafik Yaghmour Apr 30 '14 at 20:11
  • Possibly related: [Are string literals const?](http://stackoverflow.com/questions/4493139/are-string-literals-const) – Jongware Apr 30 '14 at 20:24
  • @user3322273 You wrote "It is really important to me to use realloc() and not malloc() + memcpy()" Why??? – Jiminion Apr 30 '14 at 20:28
  • Nevermind, I delete this post. It is indeed not possible and not helpful. EDIT: oops, can't delete it :) – SomethingSomething Apr 30 '14 at 20:30
  • It's amazing how many wrong answers such a simple question elicits... – Ed S. Apr 30 '14 at 20:34
  • @EricLippert: You can properly `realloc` anything created by `malloc`, `calloc`, `realloc`, and `strdup`, but not `alloca`. Are there other functions? – abelenky Apr 30 '14 at 21:25
  • Apparently there is also `asprintf` and `vasprintf`... maybe even more? – abelenky Apr 30 '14 at 21:27

1 Answers1

3

The question code:

char *my_str = "Hello World";

assigns the pointer 'my_str' to point at a static string which is not part of the "heap" from where memory is allocated. Only memory allocated from the "heap" can be resized with 'realloc()', or eventually returned to the heap using 'free()';

If you replace the code with:

char *my_str = strdup("Hello World");

Now the pointer 'my_str" is pointing to memory allocated from the heap, and it can be resized as indicated in the question code:

my_str = realloc(my_str, 50);

Remember to free this allocated memory when it is no longer needed:

free(my_str);
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28