printf on alcohol

Discussion in 'C Programming' started by estantep@gmail.com, Oct 19, 2007.

  1. Guest

    Has anybody ever seen a thing like this?

    ### code:

    double link_bw_cost(int a, int b){

    double ro;

    ro = ( (double) link_usage[a].bandw / (double) link[a].bandw);

    printf("DEBUG - a = %d e b = %d\n", a, b);
    printf("DEBUG - link_usage[%d][%d].bandw = %ld link[%d][%d].bandw
    = %ld \n", a, b, link_usage[a].bandw, a, b, link[a].bandw);


    ### when running:

    DEBUG - a = 0 e b = 2
    DEBUG - link_usage[0][2].bandw = 0 link[1078689792][0].bandw = 2
     
    , Oct 19, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    >Has anybody ever seen a thing like this?


    >### code:


    >double link_bw_cost(int a, int b){


    > double ro;


    > ro = ( (double) link_usage[a].bandw / (double) link[a].bandw);


    >printf("DEBUG - a = %d e b = %d\n", a, b);
    >printf("DEBUG - link_usage[%d][%d].bandw = %ld link[%d][%d].bandw
    >= %ld \n", a, b, link_usage[a].bandw, a, b, link[a].bandw);


    >### when running:


    >DEBUG - a = 0 e b = 2
    >DEBUG - link_usage[0][2].bandw = 0 link[1078689792][0].bandw = 2


    Yes, I've seen things like that when link_usage[a].bandw is wider
    than a long (%ld). You didn't happen to supply us with the structure
    definition, so we can't tell for sure.

    --
    "I will speculate that [...] applications [...] could actually see a
    performance boost for most users by going dual-core [...] because it
    is running the adware and spyware that [...] are otherwise slowing
    down the single CPU that user has today" -- Herb Sutter
     
    Walter Roberson, Oct 19, 2007
    #2
    1. Advertising

  3. Guest

    Bingo!

    The structure member was re-defined from long int to double.

    Thank you very much Walter!


    On Oct 18, 8:55 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > <> wrote:
    > >Has anybody ever seen a thing like this?
    > >### code:
    > >double link_bw_cost(int a, int b){
    > > double ro;
    > > ro = ( (double) link_usage[a].bandw / (double) link[a].bandw);
    > >printf("DEBUG - a = %d e b = %d\n", a, b);
    > >printf("DEBUG - link_usage[%d][%d].bandw = %ld link[%d][%d].bandw
    > >= %ld \n", a, b, link_usage[a].bandw, a, b, link[a].bandw);
    > >### when running:
    > >DEBUG - a = 0 e b = 2
    > >DEBUG - link_usage[0][2].bandw = 0 link[1078689792][0].bandw = 2

    >
    > Yes, I've seen things like that when link_usage[a].bandw is wider
    > than a long (%ld). You didn't happen to supply us with the structure
    > definition, so we can't tell for sure.
    >
    > --
    > "I will speculate that [...] applications [...] could actually see a
    > performance boost for most users by going dual-core [...] because it
    > is running the adware and spyware that [...] are otherwise slowing
    > down the single CPU that user has today" -- Herb Sutter
     
    , Oct 19, 2007
    #3
  4. writes:
    > Has anybody ever seen a thing like this?
    >
    > ### code:
    >
    > double link_bw_cost(int a, int b){
    >
    > double ro;
    >
    > ro = ( (double) link_usage[a].bandw / (double) link[a].bandw);
    >
    > printf("DEBUG - a = %d e b = %d\n", a, b);
    > printf("DEBUG - link_usage[%d][%d].bandw = %ld link[%d][%d].bandw
    > = %ld \n", a, b, link_usage[a].bandw, a, b, link[a].bandw);
    >
    >
    > ### when running:
    >
    > DEBUG - a = 0 e b = 2
    > DEBUG - link_usage[0][2].bandw = 0 link[1078689792][0].bandw = 2


    It's helpful to tell us what you think is wrong with the output,
    though it's obvious enough in this case (the value of ``a'' is shown
    as 0 and as 1078689792 in the same printf call).

    Here's the final printf statement reformatted with comments added; the
    comments indicate which format specifier is used for each argument.

    printf("DEBUG - link_usage[%d][%d].bandw = %ld "
    "link[%d][%d].bandw = %ld \n",
    a, /* %d */
    b, /* %d */
    link_usage[a].bandw, /* %ld */
    a, /* %d */
    b, /* %d */
    link[a].bandw); /* %ld */

    We know that a and b are of type int, but we don't know the type of
    bandw. (I'm assuming that link_usage[a] and link[a] are of the
    same type, and therefore that both bandw are of the same type, but I
    can't even be sure of that.)

    The printf statement assumes that bandw is of type long int. The most
    likely explanation for the misbehavior is that bandw is actually of
    some other type. Using an incorrect printf format causes undefined
    behavior.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 19, 2007
    #4
  5. Default User Guest

    Re: printf on alcohol - TPA

    wrote:

    > Bingo!


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or:
    <http://www.caliburn.nl/topposting.html>
     
    Default User, Oct 19, 2007
    #5
    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. Alex Polite

    java printf

    Alex Polite, Jun 5, 2004, in forum: Java
    Replies:
    18
    Views:
    1,238
    Dale King
    Apr 15, 2006
  2. ben
    Replies:
    4
    Views:
    657
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,325
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
  5. guru
    Replies:
    8
    Views:
    301
Loading...

Share This Page