strange output ?

Discussion in 'C Programming' started by RB, Jul 25, 2010.

  1. RB

    RB Guest

    I am newbee novice programmer, and I ran across some strange outputs
    in an app I was creating that I could not understand. So I reproduced the
    strangeness in this very small app for posting.
    It must have something to do with mixing printf and cout together in the same
    function, which it appears one should not do that. I'm sure you will know what
    is going on but I have never ran across this before.
    I am compiling with MS VS 6.0 (and old version)
    Appreciate any comments or input.

    ===== entire small app code below, outputs to follow ============
    #include <windows.h>
    #include <stdio.h>
    #include <iostream.h>

    struct Hold
    {
    DWORD pHeld;
    } *pHold;

    void FuncA( );
    void FuncB( );

    void main( )
    {
    DWORD FuncA_addr = (DWORD)FuncA;
    __asm
    {
    push [FuncA_addr]
    pop [pHold]
    }
    FuncA( );
    FuncB( );
    }

    void FuncA( )
    {
    printf("Inside FuncA( )\n");
    printf("printf Addr of FuncA( ) is 0x%X\n", pHold);
    cout << "cout Addr of FuncA( ) is " << pHold << "\n";
    }

    void FuncB( )
    {
    printf("Inside FuncB( )\n");
    }

    ===== end code, begin outputs ==================
    output "should" be this
    ................................................
    Inside FuncA( )
    printf Addr of FuncA( ) is 0x40100A
    cout Addr of FuncA( ) is 0x0040100A
    .................................................
    but instead I get two other outputs depending on whether
    I run the app from command shell viewing output
    OR
    I run the app from command shell redirecting output to txt file
    both scenarios shown below.

    *------- running from command shell and viewing output

    Inside FuncA( ) <-correct
    printf Addr of FuncA( ) is 0x40100A <-correct
    Inside FuncB( ) <-? not correct should print last ?
    cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?

    *------- end command shell viewing output

    *_____ running from command shell and redirecting output to txt file

    cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?
    Inside FuncA( ) <-? not correct should print first ?
    printf Addr of FuncA( ) is 0x40100A <-? not correct should print second ?
    Inside FuncB( ) <-? not correct should print last ?

    *________ end redirection to file
     
    RB, Jul 25, 2010
    #1
    1. Advertising

  2. RB

    ralph Guest

    On Sat, 24 Jul 2010 19:57:32 -0400, "RB" <NoMail@NoSpam> wrote:

    >I am newbee novice programmer, and I ran across some strange outputs
    >in an app I was creating that I could not understand. So I reproduced the
    >strangeness in this very small app for posting.
    > It must have something to do with mixing printf and cout together in the same
    >function, which it appears one should not do that. I'm sure you will know what
    >is going on but I have never ran across this before.
    >I am compiling with MS VS 6.0 (and old version)
    >Appreciate any comments or input.
    >
    >===== entire small app code below, outputs to follow ============
    >#include <windows.h>
    >#include <stdio.h>
    >#include <iostream.h>
    >
    >struct Hold
    > {
    > DWORD pHeld;
    > } *pHold;
    >
    >void FuncA( );
    >void FuncB( );
    >
    >void main( )
    >{
    > DWORD FuncA_addr = (DWORD)FuncA;
    > __asm
    > {
    > push [FuncA_addr]
    > pop [pHold]
    > }
    > FuncA( );
    > FuncB( );
    >}
    >
    >void FuncA( )
    >{
    > printf("Inside FuncA( )\n");
    > printf("printf Addr of FuncA( ) is 0x%X\n", pHold);
    > cout << "cout Addr of FuncA( ) is " << pHold << "\n";
    >}
    >
    >void FuncB( )
    >{
    > printf("Inside FuncB( )\n");
    >}
    >
    >===== end code, begin outputs ==================
    >output "should" be this
    >...............................................
    >Inside FuncA( )
    >printf Addr of FuncA( ) is 0x40100A
    >cout Addr of FuncA( ) is 0x0040100A
    >................................................
    >but instead I get two other outputs depending on whether
    >I run the app from command shell viewing output
    > OR
    >I run the app from command shell redirecting output to txt file
    > both scenarios shown below.
    >
    >*------- running from command shell and viewing output
    >
    >Inside FuncA( ) <-correct
    >printf Addr of FuncA( ) is 0x40100A <-correct
    >Inside FuncB( ) <-? not correct should print last ?
    >cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?
    >
    >*------- end command shell viewing output
    >
    >*_____ running from command shell and redirecting output to txt file
    >
    >cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?
    >Inside FuncA( ) <-? not correct should print first ?
    >printf Addr of FuncA( ) is 0x40100A <-? not correct should print second ?
    >Inside FuncB( ) <-? not correct should print last ?
    >
    >*________ end redirection to file


    This is a C++ question, thus better posted to a C++ group, perhaps
    comp.lang.c++.

    The problem is using different stream i/o and subsquently
    synchronization and buffering.
    Take a look at "std::ios::sync_with_stdio"

    -ralph
     
    ralph, Jul 25, 2010
    #2
    1. Advertising

  3. On Sat, 24 Jul 2010 19:57:32 -0400, RB <NoMail@nospam> wrote:

    > to do with mixing printf and cout together


    You want comp.lang.c++, not comp.lang.c. There's no "cout" in C.

    --
    Morris Keesan --
     
    Morris Keesan, Jul 25, 2010
    #3
  4. RB

    ralph Guest

    On Sat, 24 Jul 2010 19:57:32 -0400, "RB" <NoMail@NoSpam> wrote:

    >I am newbee novice programmer, and I ran across some strange outputs
    >in an app I was creating that I could not understand. So I reproduced the
    >strangeness in this very small app for posting.
    > It must have something to do with mixing printf and cout together in the same
    >function, which it appears one should not do that. I'm sure you will know what
    >is going on but I have never ran across this before.
    >I am compiling with MS VS 6.0 (and old version)
    >Appreciate any comments or input.
    >


    This is a C++ question, thus better posted to a C++ group, perhaps
    comp.lang.c++.

    The problem is using different stream i/o and subsquently
    synchronization and buffering.
    Take a look at "std::ios::sync_with_stdio"

    -ralph
     
    ralph, Jul 25, 2010
    #4
  5. RB <NoMail@nospam> wrote:
    > I am newbee novice programmer, and I ran across some strange outputs
    > in an app I was creating that I could not understand. So I reproduced the
    > strangeness in this very small app for posting.
    > It must have something to do with mixing printf and cout together in the same
    > function, which it appears one should not do that. I'm sure you will know what
    > is going on but I have never ran across this before.


    Since you're using'cout <<' you're obviously trying to write a
    C++ and not a C program. And C is the topic of this newgroup -
    for C++ you better go to comp.lang.c++ since, while C and C++
    have some similarities, they are different languages.

    But then you're also using a lot of what looks like Microsoft
    specific stuff that even comp.lang.c++ may not be the correct
    choice but one of the Microsoft oriented groups or forums might
    look more promising...

    > I am compiling with MS VS 6.0 (and old version)
    > Appreciate any comments or input.


    > ===== entire small app code below, outputs to follow ============
    > #include <windows.h>


    This is a Microsoft only header.

    > #include <stdio.h>


    If you're writing C++, as far as I know, this should be

    #include <cstdio>

    > #include <iostream.h>


    This is a C++ header - and nowadays you should use

    #include <iostream>

    (note the missing '.h').

    > struct Hold
    > {
    > DWORD pHeld;


    DWORD is neither a standard type in C or C++, I think
    it's a Microsoftism.

    > } *pHold;


    > void FuncA( );
    > void FuncB( );


    > void main( )


    At least in C this should be

    int main( void )

    and, if I'm not completely mistaken, also in C++ main() is
    supposed to return an int.

    > {
    > DWORD FuncA_addr = (DWORD)FuncA;


    Casting function pointers to an integer type (that's what
    DWORD is supposed to be, I think) invokes, at least in C,
    undefined behaviour - there are a number of architectures
    where an integer can't be used to store a function pointer.
    (And neither it's guaranteed that an object pointer can
    be used to store a function pointer and vise versa.)


    > __asm
    > {
    > push [FuncA_addr]
    > pop [pHold]
    > }


    By using inlined assembler your program automatically is non-
    portable and will only run on your architecture (assuming that
    the assembler part is correct;-)

    > FuncA( );
    > FuncB( );
    > }


    > void FuncA( )
    > {
    > printf("Inside FuncA( )\n");
    > printf("printf Addr of FuncA( ) is 0x%X\n", pHold);


    At least in C the correct format specifier for object
    addresses is "%p" (and the corresponding argumemt of
    printf() should be cast to void *. With "%X" printf()
    will expect an unsigned int instead of an address -
    while this works on some architectures on others it
    may fail miserably.

    > cout << "cout Addr of FuncA( ) is " << pHold << "\n";
    > }


    > void FuncB( )
    > {
    > printf("Inside FuncB( )\n");
    > }


    > ===== end code, begin outputs ==================
    > output "should" be this
    > ...............................................
    > Inside FuncA( )
    > printf Addr of FuncA( ) is 0x40100A
    > cout Addr of FuncA( ) is 0x0040100A
    > ................................................
    > but instead I get two other outputs depending on whether
    > I run the app from command shell viewing output
    > OR
    > I run the app from command shell redirecting output to txt file
    > both scenarios shown below.


    > *------- running from command shell and viewing output


    > Inside FuncA( ) <-correct
    > printf Addr of FuncA( ) is 0x40100A <-correct
    > Inside FuncB( ) <-? not correct should print last ?
    > cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?


    > *------- end command shell viewing output


    > *_____ running from command shell and redirecting output to txt file


    > cout Addr of FuncA( ) is 0x0040100A <-? not correct should print third ?
    > Inside FuncA( ) <-? not correct should print first ?
    > printf Addr of FuncA( ) is 0x40100A <-? not correct should print second ?
    > Inside FuncB( ) <-? not correct should print last ?


    > *________ end redirection to file


    This might indeed be a result of mixing printf() and the C++
    'cout <<' stuff, but that's a pure C++ question since there's
    no 'cout <<' in C (except if 'cout' is a (unsigned) integer and
    '<<' is the left-shift operator and then that's followed by another
    integer;-) so you really should ask the C++ experts.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jul 25, 2010
    #5
  6. RB

    RB Guest

    Thanks guy the includes fixed it.
    All I changed was this
    ----------------
    // #include <stdio.h>
    // #include <iostream.h>
    #include <cstdio>
    #include <iostream>
    .........
    ..........
    ...........
    // and then added the "std" in instead of, using namespace std;
    std::cout << "cout Addr of FuncA() is " << pHold << "\n";
    ...........
    ...........
    And now everything prints like it should.

    Inside FuncA( )
    printf Addr of FuncA( ) is 0x401195
    cout Addr of FuncA( ) is 00401195
    Inside FuncB( )
    .......................................................
    So I have been using the wrong includes. Thanks for all your suggestions !
     
    RB, Jul 25, 2010
    #6
    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. Nikk & Jak Anderson

    GZIP output and input strange behaviour

    Nikk & Jak Anderson, Apr 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,209
    Roedy Green
    Apr 26, 2004
  2. Miroslaw Osys

    very strange gcc output

    Miroslaw Osys, Sep 7, 2003, in forum: C Programming
    Replies:
    4
    Views:
    476
    Emmanuel Delahaye
    Sep 7, 2003
  3. Harvey Twyman
    Replies:
    8
    Views:
    590
    August Derleth
    Oct 25, 2003
  4. Materialised

    Strange output problem

    Materialised, Apr 1, 2004, in forum: C Programming
    Replies:
    15
    Views:
    519
    Dan Pop
    Apr 6, 2004
  5. Paul
    Replies:
    1
    Views:
    447
Loading...

Share This Page