lkml.org 
[lkml]   [2000]   [Feb]   [29]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: string-486.h?
On Tue, 29 Feb 2000, Bill Wendling wrote:

> Also sprach Ben Kosse:
> } > Well the problem is bad 'C' code. It's not the compiler's fault. It's
> } > just doing what it's told to do.
> } >
> } > char str[12]="mystring";
> } >
> } > declares a string on the stack with room for 12 bytes. However it is
> } > initialized with only 9 (8 characters + the implied \0). So the rest
> } > of the area has to be zeroed, which the compiler does.
> } No it doesn't. For all C is required to care about, it could be stored as:
> } "mystring\0bla" The definition is 12 bytes on the stack, the first nine
> } being "mystring\0".
> }
> Depends on where the char str[12] is placed. If it is global, then it is
> zeroed cause it's static...If it's an auto, then it can (and probably
> does) have garbage after the terminating \0.
>

No! I showed exactly what happened. I did not guess, nor was it theory.
I showed exactly what happens. The generated code is exact, explicit,
absolute. It is not what 'C' is required to care about. It is not what
some "rule" says. It is exactly and precisely what the 'C' compiler
did with the code cited by one who submitted the sample.

I simply explained what was happening. Now, if you don't think it should
have done this then that's another matter. Since there is no known rule
about what the compiler should have done it could have left it alone
after the required terminating \0 or even written a copyright message in
the remaining string space.

Most real programmers would not have used such a construct because it
requires that strings be duplicated. You need the string template
someplace known only to the compiler, then it has to be copied at
runtime to the place defined by the programmer. Normally one would
use an initialized pointer or just a static string declaration.

Cheers,
Dick Johnson

Penguin : Linux version 2.3.41 on an i686 machine (800.63 BogoMips).


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:56    [from the cache]
©2003-2011 Jasper Spaans