Re: Next stupid cast question

Discussion in 'C Programming' started by goose, Aug 14, 2003.

  1. goose

    goose Guest

    Dopey <> wrote in message news:<>...
    > This is not homework. GNU gcc 3.3, SuSE 8.2, Intel P4
    >
    > Referencing the code (see below).
    >
    > I'm trying to learn how to make an assignment via a cast in an (1)
    > accurate and (2) efficient way. What I'm trying to get out of the
    > assignment is a 'struct someints' of { 1, 2, 3, 4 }. I'm getting
    > something REALLY different, which is discouraging because it means
    > I'm even more clueless than I thought.
    >
    > OUTPUT:
    > si1: 808464433 808464434 808464435 808464436
    > si2: 808464433 808464434 808464435 808464436
    >
    > I've tried stuffing buf with "0001000200030004" as well. Here's that
    > output:
    >
    > si1: 808464432 808464433 808464434 808464435
    > si2: 808464432 808464433 808464434 808464435
    >
    > (I see the pattern there, but I haven't been able to figure out what
    > it means yet.)
    >
    > I've also tried malloc()'ing 4 + 1 bytes to buf, and assigning it
    > "1111" and "1234" but those results are whacked as well.
    >
    > I'm reading K&R 2nd Ed and Koenig's "C Traps..." but apparently I
    > need a little less...subtle...an explanation. (If anyone has a good
    > reference or link that helps illuminate this degree of intellectual
    > density, I'd sure love to see it.)
    >
    > Anyway, the code:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > struct someints
    > {
    > int a, b, c, d ;


    these are integers, each one is X[1] bytes in size.

    > } ;
    >
    > char * buf ;


    this is a pointer to a char. a char is 1 byte in size.

    >
    > struct someints * si1 ;
    > struct someints si2 ;
    >
    > int
    > main(void)
    > {
    > buf = malloc( sizeof(struct someints) + 1 );


    you are now creating a buffer big enough to store 4 ints plus
    one extra byte. your "pointer to char" now points to this newly
    created buffer.
    (assume that error-checking is left out for brevity).

    > that error-checking is left out for brevity).
    > /* 16 */
    > printf("sizeof(struct someints) = %d\n", sizeof(struct someints));
    >
    > strcpy( buf, "00001000200030004" ) ;


    ah!!! you created a buffer to store 4 ints plus 1 char, but you
    are now trying to store 18 chars in it.

    lets assume that an int on your machine is 2 bytes[2]. you create
    a buffer of (4*2) +1 = 9 to store 18 bytes.

    see the problem ? at this point you invoke the infamous UB.

    the *other* problem seems to be one of confusion.
    the string "0123" is, as a buffer of bytes, represented
    as the hex numbers 0x30, 0x31, 0x32, 0x33, and 0x00
    i.e. a total of 5 bytes are used to store the string.

    the *integer* 0123 is[3], as a buffer of bytes on our hypothetical
    machine, represented as the hex numbers 0x00 and 0x53.

    i.e. a total of 2 bytes are used to store the integer.


    > si1 = (struct someints *)buf ; e the integer.


    now you are assigning a buffer of chars (not number, but characters)
    to what should rightfully contain integers.

    > printf("si1: %d %d %d %d\n", si1->a, si1->b, si1->c, si1->d);


    and of course, you cannot *print* them as decimal integers. try
    printf ("si1: %s\n", (char *) &si1);

    see ? you can only see them as a string, as numbers they have no
    meaning to you.

    > to you.
    > si2 = *si1 ;
    > printf("si2: %d %d %d %d\n", si2.a, si2.b, si2.c, si2.d);
    > return 0 ;
    > }



    1. on most systems (indeed, on the one you are using), the size
    of an integer is bigger than 1.

    2. I know that its probably 4 bytes on your machine, but i'm trying
    to get a point across.

    3. In a C program, numbers starting with a 0 are octal (base8)
    numbers.


    hth,
    goose,
    my system has ints of 4 bytes too, i think
    goose, Aug 14, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dopey

    Stupid cast question

    Dopey, Aug 13, 2003, in forum: C Programming
    Replies:
    5
    Views:
    340
  2. Thomas Matthews

    Re: Next stupid cast question

    Thomas Matthews, Aug 13, 2003, in forum: C Programming
    Replies:
    2
    Views:
    405
    Mark McIntyre
    Aug 14, 2003
  3. Brandon McCombs
    Replies:
    4
    Views:
    500
    Richard Wheeldon
    Aug 28, 2006
  4. Deniz Bahar
    Replies:
    2
    Views:
    451
    Andrey Tarasevich
    Mar 9, 2005
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,011
Loading...

Share This Page