0

I'm a complete noob at C and i need some help understanding why a certain piece of code compiles correctly.

main(){
    char name[3];
    strcpy(name, "12345678912312");
    printf("%s\n",name);
}

So this code compiles correctly;however, I don't understand why it does not cause a segmentation fault. From my understanding of c, each character is 1 byte. The array name, is supposed to be able to hold 3 bytes, instead it can hold a lot more than that. Why is that?

Additionally, if I add one more character to this, I will get Illegal Instruction(core dumped).

main(){
    char name[3];
    strcpy(name, "123456789123121");
    printf("%s\n",name);
}

Then if I add another character to that code, it will throw a Segmentation fault (core dumped) error. Why are the errors different? And why did they not occur before?

And lastly, where can I find documentation for each function? I’m coming from java so I am used to referencing to the java docs.

Im using GCC compiler in Ubuntu linux.

2 Answers2

1

Both of code invokes undefined behavior because you are writing to an un-allocated memory location. In this case anything could happen. Either your program runs and may or may not give the expected output or it will crash or give segmentation fault.
Also note that strcpy doesn't check for array bound and compiler doesn't raise any warning/error for it.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Well in that case, should I allocate memory to the name variable? If this was the solution, how would I implement it? – Todor Penchev Apr 27 '14 at 00:07
  • 1
    you must make sure that `name` has size greater (you need one extra char for the null terminated string character) than the size of the string you're pasting into it. – vsoftco Apr 27 '14 at 00:10
  • 1
    For your particular problem just use `const char* name = "123456789123121"`. For general dynamic memory allocation look up `malloc`. – IllusiveBrian Apr 27 '14 at 00:10
  • @Namfuak, `name` is the destination, not the pasted string. – vsoftco Apr 27 '14 at 00:11
  • 1
    @vsoftco There's no particular reason for him to use `strcpy` if he is using a compile time constant string. If he is getting a string from input he can either allocate more space than necessary, as he probably realizes, or he can use `malloc` if he can't determine how much space to allocate at compile time. – IllusiveBrian Apr 27 '14 at 00:13
  • Thanks for the quick answers guys, each one has really helped me :) – Todor Penchev Apr 27 '14 at 00:16
  • @Namfuak, Yes of course, I would not use `strcpy` either, I thought this was just some exercise on `strcpy`. I just now realized what you meant in your comment. – vsoftco Apr 27 '14 at 00:20
1

If you read a few questions here on SO you will hear a lot about "undefined behaviour", often abbreviated to UB.

What it means is that if your program does something outside the C standards, the standards do not define what will happen. Anything can happen.

Writing past the end of an array is one example of something that can trigger UB.

C does not do array bound checking, so if you try to write beyond the end of the array, the results will depend on how the compiler implements arrays, how they are arranged in memory, and what lies after them. The point, however, is that you cannot rely on any particular behaviour.

My favourite reference site for C and C++ is cppreference. But on Linux you can also read the definition of library functions with man, eg. man strcpy.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • Thank you for explaining UB and array bound checking and how its handled. I think I understand a bit more now, and I am going to go and look into the strcpy function further. Thanks :) – Todor Penchev Apr 27 '14 at 00:15