1. Why Nesting of comments is not supported by c.
code does not involve much complexity to have that.

2. Why Array partial initialization have zero appended.
why it cannot do in normal declaration.

3. continued....

Thanks,
3R

1. Because they said so. Why would you want to comment out the comments? If you want to comment out a large block of code that may contain comments, then there are other ways to do it. I do it like this.

#if 0
// code here
#endif

2. Not sure what you are asking. int array[200] = {1,2,3,4,5} In this array the first five elements are initialized to the values shown, all remaining array elements are initialized to 0. Is that your question?

Edited 6 Years Ago by Ancient Dragon: n/a


1. Because they said so. Why would you want to comment out the comments? If you want to comment out a large block of code that may contain comments, then there are other ways to do it. I do it like this.

#if 0
// code here
#endif
int main( void )
{
           some statements;
   
                /* some comments
                                          /*  comments
                                                      end of comments */
                  end of some comments */
 some more statements
return 0;
}

in the above code the below line creates a problem as its not any valid C statement. end of some comments */ my question is---- why compiler implementers/preprocessor implementers have not handled such a simple situation .

push the /* in to stack and pop when matching end */ is found.


2. Not sure what you are asking. int array[200] = {1,2,3,4,5} In this array the first five elements are initialized to the values shown, all remaining array elements are initialized to 0. Is that your question?

yes the same.

when an array is initialized partially why it initializes remainig elements to zero.


3. why array bound check is not implemented in C.

i want to know the technical difficulties involved in implementing these.

i want to know the technical difficulties involved in implementing these.

To use your overused word, why?

If you plan on writing your own compiler that breaks C syntax, then you obviously are capable of working out the level of technical difficulty yourself. And if you can't figure out the technical difficulty, you're not a competent enough coder to write a compiler...

Why? Because the ISO standards committee said so. Didn't your momma ever tell you "because I said so" in response to your why question???

Edited 6 Years Ago by Ancient Dragon: n/a

Nested Comments
>1. Why Nesting of comments is not supported by c.
>code does not involve much complexity to have that.

Because it adds unnecessary complexity to the grammar and as a result, the compiler. Since a language feature already exists which handles the intended function of nested comments (ie. commenting out code), proposals to add nested comments were rejected.

>why compiler implementers/preprocessor implementers
>have not handled such a simple situation .

Just because you think it's a simple situation doesn't make it so. Implementations are welcome to support nested comments as an extension, or (more commonly) recognize the error and throw a warning. But nested comments weren't valuable enough to make it through the standardization process.

It's not especially difficult to implement nested comments, assuming they're supported. For example, each comment symbol can maintain a level count which is incremented on entering a comment and decremented on exiting a comment. Everything between two comment symbols with a level of 0 would be removed during the comment removal phase.

Zero Initialization
>2. Why Array partial initialization have zero appended.
>why it cannot do in normal declaration.

Zero initialization was deemed too expensive to be the default behavior. You'll find a lot of things in C have that rationale.

>when an array is initialized partially why it initializes remainig elements to zero.
Automatic variables aren't initialized, but that's only a single object and easy to manage. However, partial initialization of an array is much harder to manage if the same rule is applied. Note that merely accessing an indeterminate value invokes undefined behavior, so it's easy to see best practice following something like so if the rest of the array weren't implicitly zeroed out:

int a[N] = {1, 2, 3, 4, 5};
int i;

for ( i = 5; i < N; i++ ) {
  a[i] = 0;
}

The bug of leaving part of an array initialized is easy to cause, potentially devastating, and trivial to fix, so the language blesses the fix and the compiler does it for you internally.

Bounds Checking
>3. why array bound check is not implemented in C.
It was deemed too expensive at the time. Bounds checking might be included in future revisions of the standard though.

Bounds checking is really just the same thing internally that you would do explicitly:

if ( i >= 0 && i < N )
  access ( a[i] );
else
  panic();

That extra branch on every subscript operation can be terribly expensive though. Take an implementation of bubble sort, for example:

void jsw_bubblesort ( int a[], int n )
{
  int i;

  for ( i = 0; i < n; i++ ) {
    int j, done = 1;

    for ( j = n - 1; j > 0; j-- ) {
      if ( a[j] < a[j - 1] ) {
        jsw_swap ( &a[j], &a[j - 1] );
        done = 0;
      }
    }

    if ( done )
      break;
  }
}

Now add that test and branch for every subscript operation:

void jsw_bubblesort ( int a[], int n )
{
  int i;

  for ( i = 0; i < n; i++ ) {
    int j, done = 1;

    for ( j = n - 1; j > 0; j-- ) {
      if ( j < 0 || j >= n )
        abort();

      if ( j - 1 < 0 || j - 1 >= n )
        abort();

      if ( a[j] < a[j - 1] ) {
        if ( j < 0 || j >= n )
          abort();

        if ( j - 1 < 0 || j - 1 >= n )
          abort();

        jsw_swap ( &a[j], &a[j - 1] );
        done = 0;
      }
    }

    if ( done )
      break;
  }
}

There are three immediate problems with this:

  1. Obviously the inner loop slows down drastically, making bubble sort even worse in practice than it currently is. The big problem with implicit bounds checking is you don't have control, and the overhead is hidden from you. This goes against the spirit of C.
  2. What if a isn't really an array? The compiler would need to implement two different checks: one for actual arrays, and one for arrays simulated using dynamic memory. It would need to determine which check to run, as well as whether the specified pointer is even valid for bounds checking, which increases the overhead even more.
  3. What if the index is valid, but the pointer isn't the first element? C allows you to do things like this:
    int a[10];
    int *p = a + 1;
    
    p[-1] = 12345; /* Access a[0] */

So, off the top of my head, maybe something like this without optimizations?

int is_array ( int *a, int *pos )
{
  int i;

  for ( i = 0; i < arrays.n; i++ ) {
    /* Make sure the addresses overlap */
    if ( a >= arrays[i].ref 
      && a < &arrays[i].ref[arrays[i].n] )
    {
      *pos = i;
      return 1;
    }
  }

  return 0;
}

int is_simarray ( int *a, int *pos )
{
  int i;

  for ( i = 0; i < memmanager.blocks.n; i++ ) {
    /* Make sure the addresses overlap */
    if ( a >= memmanager.blocks[i].ref 
      && a < &memmanager.blocks[i].ref[memmanager.blocks[i].n] )
    {
      *pos = i;
      return 1;
    }
  }

  return 0;
}

int array_in_bounds ( int *a, int pos, int index )
{
  return a + index >= arrays[pos].ref 
      && index < arrays[pos].size;
}

int simarray_in_bounds ( int *a, int pos, int index )
{
  return a + index >= memmanager.blocks[i].ref 
      && index < memmanager.blocks[pos].size;
}

void in_bounds ( int *a, int index )
{
  int pos;

  if ( is_array ( a, &pos ) ) {
    if ( !array_in_bounds ( a, pos, index ) )
      abort();
  }
  else if ( is_simarray ( a, &pos ) ) {
    if ( !simarray_in_bounds ( a, pos, index ) )
      abort();
  }
  else
    abort();
}

void jsw_bubblesort ( int a[], int n )
{
  int i;

  for ( i = 0; i < n; i++ ) {
    int j, done = 1;

    for ( j = n - 1; j > 0; j-- ) {
      in_bounds ( a, j );
      in_bounds ( a, j - 1);

      if ( a[j] < a[j - 1] ) {
        in_bounds ( a, j );
        in_bounds ( a, j - 1);
        jsw_swap ( &a[j], &a[j - 1] );
        done = 0;
      }
    }

    if ( done )
      break;
  }
}

There are many edge cases to consider as well, which I didn't in the above example, but it clearly shows the tip of the iceberg in adding array bounds checking to C and why the feature was rejected as too expensive by the standards committee.

Comments
Thanks
nice

Why? Because the ISO standards committee said so. Didn't your momma ever tell you "because I said so" in response to your why question???

To use your overused word, why?

If you plan on writing your own compiler that breaks C syntax, then you obviously are capable of working out the level of technical difficulty yourself. And if you can't figure out the technical difficulty, you're not a competent enough coder to write a compiler...

you can call me what ever you want.

but i cannot give these answer to my students.

you can call me what ever you want.

but i cannot give these answer to my students.

You are having a laugh, aren't you? If not, I feel so sorry for your students -- how the hell are they supposed to learn from a tutor who doesn't have a basic grasp of the topic that they are supposed to be teaching?

[Methinks that you are actually trolling]

>but i cannot give these answer to my students.
Sure you can. When teaching a complex subject of interrelated parts, it's generally best to gloss over the parts that aren't absolutely necessary at the student's level.

This article has been dead for over six months. Start a new discussion instead.