5

I'm a beginner to C but I have this code running on xcode through gcc on terminal:

#include <stdio.h>
#include <string.h> 
int main(){
    char name[12] = "Roman Mirov"; 
    printf("My name is %s\n", name);
    name[8] = 'k'; 
    printf("My name is %s\n", name);
    char greeting[] = "hello"; 
    printf("%s %s\n", greeting, name);
    strcpy(greeting, "greetings, "); 
    printf("%s%s\n", greeting, name);
    return 0;
}

And it outputs this:

My name is Roman Mirov
My name is Roman Mikov
hello Roman Mikov
Abort trap: 6

My question exactly is, why it generates error instead of showing the last line as output "greetings, Roman Mikov"?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Roman
  • 173
  • 2
  • 2
  • 12
  • 1
    There is no question here!!! – barak manos Dec 27 '16 at 07:18
  • @barakmanos I'll fix that if you can't see the question. – Roman Dec 27 '16 at 07:27
  • Yeah, I was eventually able to see it, but you should still make it clearer. – barak manos Dec 27 '16 at 07:28
  • These two threads would serve you better in understanding The key point being mentioned about writing memory that you are not owning. http://stackoverflow.com/questions/26431147/abort-trap-6-error-in-c http://stackoverflow.com/questions/29401116/abort-trap-6-in-c-program – achoora Dec 27 '16 at 07:21

3 Answers3

11

In this case, the destination greeting does not have enough space to contain the whole contents of source, so it is an out of bounds access which invokes undefined behavior.

To elaborate, the size of array greeting is determined by the size of the supplied initializer,

char greeting[] = "hello";

in this case, "hello" which makes the size as 6, including the null-terminator.

Now, later you try to put a much bigger string into the memory,

strcpy(greeting, "greetings, ");

where, the source is of 12 bytes in size, whereas, the destination only contains 6. This causes the boundary overrun and the result, UB. The crash (or abort) is one of the possible side-effects of UB.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Is there a way to then change the value of a string (the whole array) if the new value is of greater size than the previous? – Roman Dec 27 '16 at 07:26
  • 1
    @Roman well, with arrays, it's not possible to resize. You may consider dynamic memory allocation, like `malloc()` and `realloc()`, is that's necessary. – Sourav Ghosh Dec 27 '16 at 07:27
  • @Roman For this simple program you can just specify a size for `greeting` which is large enough for all the greetings you may want to place into it. Otherwise, dynamic allocation is your friend, like Sourav said. – StoryTeller - Unslander Monica Dec 27 '16 at 07:29
  • Ok. Thanks. Now, what confuses me is how one tutor easily avoids my problem [here](https://youtu.be/7F-Q2oVBYKk?t=5m1s) – Roman Dec 27 '16 at 07:31
  • @Roman 1) UB is UB, there's no explaining it. 2) stop and discontinue watching that tutorial, N O W. – Sourav Ghosh Dec 27 '16 at 07:32
0

In this line, you are allocating an array of 5+1 characters:

char greeting[] = "hello";

In this line, you are attempting to write 11+1 characters into that array:

strcpy(greeting, "greetings, ");
barak manos
  • 29,648
  • 10
  • 62
  • 114
0

In this case,greeting variable is array of char with size is 6 (Because hello and \0 ).

So when you use strcpy(greeting, "greetings, "); to copy "greetings, " into greeting is cannot. Because greeting not enough to contain array with 11+1 character

=> Has error abort trap : 6 here

nartoan
  • 368
  • 2
  • 9