Re: Next stupid cast question

Discussion in 'C Programming' started by Thomas Matthews, Aug 13, 2003.

  1. Dopey wrote:

    > 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

    Looks like addresses or locations.
    Could be the zipcodes of Nasal Demons too.



    > 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 ;
    > } ;
    >
    > char * buf ;
    >
    > struct someints * si1 ;
    > struct someints si2 ;

    Note that "si1" is a pointer, "si2" is a whole structure.



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

    There is no need for the "+1".
    Also you need to check "buf" for NULL. The malloc() function
    returns NULL for problems.



    > /* 16 */
    > printf("sizeof(struct someints) = %d\n", sizeof(struct someints));

    Where is the line containing "OUTPUT:\n"? Doesn't match your
    given output above.

    >
    > strcpy( buf, "00001000200030004" );

    Valid, if and only if size of "00001000200030004" is less than
    sizeof(struct someints). {Why does "00001" have four zeros and
    "0002" have only three?)

    > si1 = (struct someints *)buf ;

    Evil. Bad. Sets out a call for Nasal Demons.
    Execute this snippet:
    printf("sizeof integer variable: %d\n", sizeof(int));
    printf("sizeof textual integer 00001: %d\n", sizeof("00001"));

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

    This statement starts the ritual summoning those Nasal Demons.
    See reason below.

    >
    > si2 = *si1 ;

    This starts the ritual drums to beat faster.

    > printf("si2: %d %d %d %d\n", si2.a, si2.b, si2.c, si2.d);

    And this unleashes those demons.

    > return 0 ;
    > }

    You are confusing a textual integer with an integer variable.
    The fundamental theorem is '1' != 1. {Or for those nitpickers,
    a character representation of the value 1 may not be equal
    to the internal value).

    int value;
    char text[64] = {'\0'};
    value = 1;
    sprintf(text, "%05d", value);

    Mixing the textual representation with the internal
    representations of integers, floats and doubles will
    start the summoning ritual of Nasal Demons.

    --
    Thomas Matthews
    Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
     
    Thomas Matthews, Aug 13, 2003
    #1
    1. Advertising

  2. Thomas Matthews

    jemma Guest

    On Wed, 13 Aug 2003 21:26:21 GMT, Thomas Matthews
    <> wrote:

    >Dopey wrote:
    >
    >> OUTPUT:
    >> si1: 808464433 808464434 808464435 808464436
    >> si2: 808464433 808464434 808464435 808464436

    >
    >Looks like addresses or locations.
    >Could be the zipcodes of Nasal Demons too.
    >


    Could be, but contrary to grouplore, nasal demons are far rarer
    than one might think.

    808464433 is 0x30303031,
    808464434 is 0x30303032,
    etc

    Which is not suprising considering the ascii value of the character
    string the OP used and the code posted. In spite of the fact the OP
    committed a serious error by allocating too small of a buffer and
    hence invoked undefined behaviour, there was no drum, ritual, or nasal
    demons: the output that the OP saw would have been exactly the same
    even if the buffer was allocated properly.

    --Jemma
     
    jemma, Aug 13, 2003
    #2
    1. Advertising

  3. On Wed, 13 Aug 2003 22:49:11 GMT, in comp.lang.c , jemma
    <jemmaq99@hotmail!.com> wrote:

    >On Wed, 13 Aug 2003 21:26:21 GMT, Thomas Matthews
    ><> wrote:
    >
    >>Dopey wrote:
    >>
    >>> OUTPUT:
    >>> si1: 808464433 808464434 808464435 808464436
    >>> si2: 808464433 808464434 808464435 808464436

    >>
    >>Looks like addresses or locations.
    >>Could be the zipcodes of Nasal Demons too.
    >>

    >
    >Could be, but contrary to grouplore, nasal demons are far rarer
    >than one might think.


    Demons were however present in this case.

    >In spite of the fact the OP
    >committed a serious error by allocating too small of a buffer and
    >hence invoked undefined behaviour, there was no drum, ritual, or nasal
    >demons: the output that the OP saw would have been exactly the same
    >even if the buffer was allocated properly.


    Nasal demons did however appear, and their manifestation took the
    form of suppressing the OS's warning that you'd just trampled on
    memory belonging to your antivirus software which had consequently
    failed and offered you a unique chance to learn more about MSBlast.

    Nasal Demons need not be loud and vocal you know...

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Aug 14, 2003
    #3
    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:
    353
  2. goose

    Re: Next stupid cast question

    goose, Aug 14, 2003, in forum: C Programming
    Replies:
    0
    Views:
    377
    goose
    Aug 14, 2003
  3. Brandon McCombs
    Replies:
    4
    Views:
    519
    Richard Wheeldon
    Aug 28, 2006
  4. Deniz Bahar
    Replies:
    2
    Views:
    480
    Andrey Tarasevich
    Mar 9, 2005
  5. rincewind

    stupid, STUPID question!

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

Share This Page