3

I just started to learn memory management in C, and I didn't understand something. I want to allocate memory to a buffer that holds 12 bytes. which is the exact size of Hello World! without null terminator.

Then I want to append a string to the current string with strcat, and of course I cannot do that because I will get core dumped error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    char mystr[12] = "Hello World!";
    # allocate memory to mystr?
    char *ptr = (char*) malloc(13 * sizeof(char));
    
    strcat(mystr, "Hello");
    return 0;
}

So, I don't know how can I allocate memory to the mystr variable if malloc doesn't take any other arguments except the target size.

Acorn
  • 24,970
  • 5
  • 40
  • 69

3 Answers3

3

I don't know how can I allocate memory to the mystr variable if malloc doesn't take any other arguments except the target size.

It is not possible to allocate extra memory to an array. Instead, what you want to do is allocate a new block of memory, copying the original string into the beginning of that memory (strcpy), then append the rest (strcat):

char *p = (char*) malloc((12 + 5 + 1) * sizeof(char));

strcpy(p, myptr);
strcat(p, "Hello");

12 for the first string, plus 5 for the second, plus one for the null-terminator.

Of course, since you know the final size, you could also simply allocate a big enough array instead of using malloc (and you can also use memcpy, too).

Acorn
  • 24,970
  • 5
  • 40
  • 69
0

The problem should be that a string in C always end with a NULL character (also noted '\0'), so your string is actually 13 characters long. (That character is always automatically added with string literals and serves at telling where the string stops, because a string doesn't have a fixed length.)

So the strcat tries to read the string Hello world! followed by garbage (since the null-terminator is not included in the string).

P.S.: the error is not the core dumped but the Segmentation fault that precedes it, and this tells you that you are trying to change something in a segment you are not supposed to change (or execute/read something you are not supposed to -- this is a security feature).

Edit: after modifying the string mystr, you also need to change the length you allocate (in the malloc: use 13 * sizeof(char), or more simply here in this case sizeof(mystr)).

P.S. 2: also comments in C are started by //, not # (those are preprocessor directives).

rajdakin
  • 74
  • 6
  • The main problem wasn't with the buffer size of the string. It was that I didn't know **that I can't change the buffer size of a string** that I already declared. Anyway, thank you. – ArlichBachman Aug 17 '20 at 20:13
  • That is not entirely true. The original question defined `char mystr[12] = "Hello World!";` which stores the 12 characters but no null terminator. This is quite legal except the array is not a "string". – Weather Vane Aug 17 '20 at 20:14
0

you cant change the size of the array. mystr has to be also dynamically allocated.

int main(int argc, char const *argv[])
{
    const char *ptr = "Hello World!";
    const char *ptr2 = "hello";
    char *mystr = malloc(strlen(ptr)+1);

    strcpy(mystr, ptr);
    mystr = realloc(mystr, strlen(mystr) + strlen(ptr2) + 1);
    strcat(mystr, ptr2);
    return 0;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    @Gerhardh it is also no good to do no check result if `malloc`. But this time intentionally I have sacrificed the correctness. – 0___________ Aug 17 '20 at 20:15
  • @Gerhardh Thanks for letting me know! I will be fine, thanks fam. Before that I need to learn first what `const` is ('; – ArlichBachman Aug 17 '20 at 20:31