0

I have a program which I expect it to crash but it doesn't. Can you please let me know the reason.

char a[5];
strncpy(a,"abcdefg",7);
a[7] = '\0';
printf("%s\n",a);

Shouldn't the program crash at strncpy() or at a[7]='\0' which is greater than array size of 5. I get output as abcedefg. I'm using gcc compiler.

foo_l
  • 591
  • 2
  • 10
  • 28

4 Answers4

2

Size of a array is five char a[5]; and your are assigning at 7th location that is buffer overrun problem and behavior of your code is Undefined at run time.

strncpy(a,"abcdefg",7);
a[7] = '\0';

Both are wrong, you need to defined array like:

#defined size 9  // greater then > 7
char a[size];

notice "abcdefg" need 8 char one extra for \0 null char.

read: a string ends with a null character, literally a '\0' character

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

In your example, your program has access to memory beyond a (starting address of array) plus 5 as the stack of the program may be higher. Hence, though the code works, ideally it is undefined behavior.

Ganesh
  • 5,880
  • 2
  • 36
  • 54
1

C often assumes you know what your doing, even (especially) when you've done something wrong. There is no bounds to an array, and you'll only get an error if your lucky and you've entered into an undefined memory location and get a segmentation fault. Otherwise you'll be able to access change memory, to whatever results.

J.Miller
  • 477
  • 1
  • 3
  • 14
  • *"C is not a type safe language."* Oh, then explain `int x = "0";`? *"It often assumes you know what your doing,"* No. Implementations of C assume, quite logically, that you're communicating the language rather than gibberish. When you write something wrong, like most other languages (eg. English) the definition becomes obscure. *"There is no bounds to an array"* I want to put 42 eggs in my carton that stores only 12 eggs. The container most certainly has bounds, but you won't know what happens to the 30 remaining eggs, so the behaviour is undefined. – autistic Apr 11 '13 at 07:02
  • Correct me if I'm wrong, but we do know what happens to the remaining 30. They will reside be assigned in memory as if the array had been properly declared that size. What we DON'T know is what was in those memory locations before you decided to put some eggs there. – J.Miller Apr 11 '13 at 07:21
  • No. They'll be thrown off my balcony at passing hooligans, and since *the behaviour is undefined* I'm most certainly within my rights to do that. – autistic Apr 11 '13 at 07:26
  • Also your right. While C has some features that are would in some ways be considered not type safe, I misspoke in my use of it in this context. I edited the answer. – J.Miller Apr 11 '13 at 07:26
1

You can't give a definition to undefined behaviour, as you are attempting by stating that it should crash. Another example of undefined behaviour that doesn't commonly crash is int x = INT_MAX + 1;, and int x = 0; x = x++ + ++x;. These might work on your system, if only by coincidence. That doesn't stop them from wreaking havoc on other systems!

Consider "Colourless, green ideas sleep furiously", or "The typewriter passed the elephant to the blackness". Do either of these statements make any sense in English? How would you interpret them? This is a similar situation to how C implementations might treat undefined behaviour.

Let us consider what might happen if you ask me to put 42 eggs in my carton that can store at least 12 eggs. The container most certainly has bounds, but you insist that they can all fit in there. I find that the container can only store 12 eggs. You won't know what happens to the 30 remaining eggs, so the behaviour is undefined.

autistic
  • 1
  • 3
  • 35
  • 80