0
#include <stdio.h>
#include <string.h>
void main(){
    char sss[0]; //array with 0 elements
    sss[0]= 'h'; sss[1]= 'o'; sss[2]= 'w'; //how does this line compile wihtout error?
    printf("sss after 3 chars added: %s\n", sss);
    strcpy(sss, "n");
    printf("sss after strcpy: %s\n", sss);
    strcat(sss, " stuff");
    printf("sss after strcat: %s\n", sss);
}

Here, I declared a character array 'sss' with a size of 0. Thus, it wouldn't be able to assign any char to any elements. However, the array behaves like a dynamically allocated one, allowing assignment of any number of chars. The code above produces the following output.

sss after 3 chars added: how
sss after strcpy: n
sss after strcat: n stuff

I thought C was strict with array allocations and expected it to throw "array size out of range" error. Why/how is this happening?

James
  • 51
  • 7
  • 2
    see this: http://stackoverflow.com/questions/15646973/how-dangerous-is-it-to-access-an-array-out-of-bounds – Alex Libov Nov 03 '16 at 21:20
  • `"array size out of range" error` Where did you get the idea that this is even a thing in C? – John3136 Nov 03 '16 at 21:22
  • 1
    sss is just a pointer. and sss[X] is equivalent to *(sss+X) – Alex Libov Nov 03 '16 at 21:22
  • 1
    what you have is Undefined Behavior. UB can include seeming to work under 100% of test cases then crashing at midnight on your customers production system. Or allowing hackers to download all user info in your account database. Or turning on the lights of the White House xmas tree – pm100 Nov 03 '16 at 21:23
  • if this is linux, try running this code under valgrind – pm100 Nov 03 '16 at 21:24
  • I remember seeing such error somewhere sometime.. maybe java. thank you guys for quick response. – James Nov 03 '16 at 21:26
  • C does not check whether or not you are out of array bounds - it is a low level language and you are expected to keep track of this yourself. When you assign something to sss[x], you are overwriting memory that was not allocated for this. In fact, if you add something after sss (int ss2=0), you may or may not see it being overwritten depending on your compiler, os etc. – Jakub Judas Nov 03 '16 at 21:31
  • `void main()` is an invalid signature for `main` **and** a deprecated signature for any function. Use `int main(void)` if you don't use the arguments passed. – too honest for this site Nov 03 '16 at 21:53

2 Answers2

0

C doesn't allow you to define an array with zero.

C11, 6.7.6.2 Array declarators says:

In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero.

Most of what you do, including assigning values to that array, is undefined behaviour.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
P.P
  • 117,907
  • 20
  • 175
  • 238
0

The behavior is undefined because you haven't allocated memory for the array items. Try this: char sss[3]; Now you can assign to the individual array indexes without undefined behavior.

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47