strange output ?

R

RB

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
 
R

ralph

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
 
R

ralph

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
 
J

Jens Thoms Toerring

RB said:
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 said:
#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
 
R

RB

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 !
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top