Re: low-level question



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

jesso wrote:
> I couldn't get this on a midterm. Darn!
>
> Anyone want to help?
>
> 1. Why does the following program output a 0?

The program does not /necessarily/ output a 0. The program invokes
undefined behaviour, and /any/ output (or lack of output) is valid.

> 2. Explain in detail.

buf is defined as an array of 4 characters (char buf[4];)
However, the program uses the standard function strcpy() to modify the
contents of this array. The string given to strcpy() to copy into buf
consists of 4 characters, /plus/ a string-termination character of \0.

When strcpy() copies the initialization string into buf, it will copy 4
characters (which will fit into buf, as buf is defined as a 4 character
array), and will terminate the copied string with a \0 character. This
terminating character will not fit within the confines of buf (which is
already full), and will be written to some other area of memory.

Assuming specific environmental and compiler characteristics, this \0
character /may/ be written in such a manner as to overwrite the
significant bits of the ii variable, setting ii to 0.

*However*, there is no guarantee that this can happen. It would require
- - ii to start /immediately/ after buf in memory, and
- - ii to be stored as a 'little-endian' binary value

There is no guarantee, from the code and details provided, that the
compiler will
- - align ii to a 4-byte boundary,
- - order ii to /follow/ buf in memory, or
- - store int values as little-endian binary numbers

> 3. How could you prevent this outcome without changing the code?

Don't run the program.

>
> #include <stdio.h>
> #include <string.h>
>
> int
> main( int argc, char *argv[] )
> {
> int ii = 1;
> char buf[ 4 ];
>
> strcpy( buf, "AAAA" );
>
> printf( "%d\n", ii );
>
> return 0;
> }
>


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDFFRJagVFX4UWr64RAgk8AJ90VvPuywZWEnLXgjgDM0bd/udhRQCgvu3U
+cNqEFIlcQbqc1J8T1BxITo=
=Jp1t
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: Standard function to convert " " to (etc.)?
    ... decodes all these escapes back into a string. ... corresponding character. ... int convert_escape{ ... #define ESCAPE 1 ...
    (comp.lang.c)
  • RE: DTS How to parse a varcharfield
    ... Nick Barclay created a UDF function that parses a varchar field. ... It returns the position of the character AFTER the nth(i.e. ... you know the positions you could update the varchar string by substituting ... returns int ...
    (microsoft.public.sqlserver.dts)
  • Re: RC4 algorithm problem
    ... > int lengthOfData; ... default character encoding, at least when any of the characters are out ... Is using an ASCII string directly as the RC4 key a recommended practice? ...
    (sci.crypt)
  • Re: atoi
    ... int char2int ... functions for character classification. ... so you want to give the caller a bit more flexibility. ... But fgetsdoesn't *always* leave a '\n' in the string. ...
    (comp.lang.c)
  • Re: Help a beginner - simple lowercase to uppercase and so on function
    ... add some more string functions on the model of the function ... void UppStrg(char *Low, char *Upp, int cnt); ... Once you've detected a character is not lower case, you want to break out of the inner loop. ...
    (comp.lang.c)