Can inclusion of a printf() effect a calculation? Please help!!

A

almurph

Anyone herd of this before



I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.

I am doing stuff like:

printf("%.8f", someArrName[7]);

I can see the variables but the net result is different.


When i exclude the printf statements I get a different net result!
Strange no?


In other words the inclusion of just some printf() statements seems
to effect the result. Anyone heard of this before?

Would greatly appreciate any comments/ideas/suggestions/user-
experiences that you may be able able to share with me. I've never
seen anything like this before...

Thanka,
Al.
 
B

Ben Bacarisse

Anyone herd of this before

I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.

I am doing stuff like:

printf("%.8f", someArrName[7]);

I can see the variables but the net result is different.

When i exclude the printf statements I get a different net result!
Strange no?

Code? There are too many options to consider without real code. Can
you cut down the program so that it still does this? If not, what is
it that you removed just before it started to behave again?

It is possible, with a highly chaotic calculation[1], that simply
altering how the compiler manages its floating point registers (which
can be a side effect of adding a printf call) can alter the result.
In such a case, the problem is really in the chaotic calculation or in
overly precise output, not with the printfs.

[1] This is not really a technical term. In the old days it might have
been call ill-conditioned, but because of what I think you are doing,
chaotic might be a better fit.
 
B

Bartc

Anyone herd of this before



I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.

I am doing stuff like:

printf("%.8f", someArrName[7]);

I can see the variables but the net result is different.


When i exclude the printf statements I get a different net result!
Strange no?

What do you mean by a net result? How do you display the 'net result' to
establish that it gives different results (assuming it has some sort of
value)?

Does someArrname[7] have any connection with the net result?
 
A

almurph

Anyone herd of this before
   I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.
   I am doing stuff like:
printf("%.8f", someArrName[7]);
   I can see the variables but the net result is different.
   When i exclude the printf statements I get a different net result!
Strange no?

Code?  There are too many options to consider without real code.  Can
you cut down the program so that it still does this?  If not, what is
it that you removed just before it started to behave again?

It is possible, with a highly chaotic calculation[1], that simply
altering how the compiler manages its floating point registers (which
can be a side effect of adding a printf call) can alter the result.
In such a case, the problem is really in the chaotic calculation or in
overly precise output, not with the printfs.

[1] This is not really a technical term.  In the old days it might have
been call ill-conditioned, but because of what I think you are doing,
chaotic might be a better fit.

Just found out that the arrays are pointers. How can i print out the
content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

correction appreciated...

Al
 
F

Fred

Anyone herd of this before
   I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.
   I am doing stuff like:
printf("%.8f", someArrName[7]);
   I can see the variables but the net result is different.
   When i exclude the printf statements I get a different net result!
Strange no?
Code?  There are too many options to consider without real code.  Can
you cut down the program so that it still does this?  If not, what is
it that you removed just before it started to behave again?
It is possible, with a highly chaotic calculation[1], that simply
altering how the compiler manages its floating point registers (which
can be a side effect of adding a printf call) can alter the result.
In such a case, the problem is really in the chaotic calculation or in
overly precise output, not with the printfs.
[1] This is not really a technical term.  In the old days it might have
been call ill-conditioned, but because of what I think you are doing,
chaotic might be a better fit.
- Show quoted text -

Just found out that the arrays are pointers. How can i print out the
content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

correction appreciated...
if someArrName[7] is of type (double *), then

printf("%.8f", *someArrName[7]);
 
B

Ben Bacarisse

<snip my reply>

If you are not commenting on some text, please consider cutting it.

And certainly cut people's sig blocks. OK, mine two lines, but even
then it is worth cutting.
Just found out that the arrays are pointers.

I don't follow. Do the arrays contain pointer to numbers rather
numbers? Are you simply referring to the fact that array names are
converted to pointers in most contexts in C? If it is the latter, you
probably don't need to worry about that.
How can i print out the
content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

If you post code, at least include the definition (or declaration) of
the things involved. In this case is does not matter much since

printf("%.8f", *&someArrName[7]);

is the same as

printf("%.8f", someArrName[7]);

The *& just cancel out. But that still leaves me wondering what is it
that you have just found out that you think affects this line of code.
 
K

Keith Thompson

Anyone herd of this before
   I am debuging a pre 99 C program. However I have noticed that when I
include some printf() statements (in order to see some float
variables) I get a different type of net answer than when I did not
insert the printf() statements.
   I am doing stuff like:
printf("%.8f", someArrName[7]);
   I can see the variables but the net result is different.
   When i exclude the printf statements I get a different net result!
Strange no?
[...]
Just found out that the arrays are pointers. How can i print out the
content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

correction appreciated...

What do you mean by "the arrays are pointers"? Either you're
mis-stating something, or you're misunderstanding something. Arrays
are not pointers; pointers are not arrays. See section 6 of the
comp.lang.c FAQ, <http://www.c-faq.com/>, for more information.

Without seeing some actual code, at least including the declaration of
someArrName, preferably a full compilable and executable program, we
can't do more than guess. (*&someArrName[7] is almost certainly not
what you're looking for, the "*" and "&" operators would cancel out.)

Note that some compilers are able to warn about mismatches between
printf format strings and argument, at least if you give them the
right options. (gcc can do this, for example.) That should at least
help you get the types right -- though of course that's only part of
getting the code right.
 
C

CBFalconer

.... snip ...

Just found out that the arrays are pointers. How can i print out
the content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

correction appreciated...

%p prints the value of a pointer, which must be cast to void*.

i.e:
printf("%p\n", (void *)someArrName);

Note that someArrName is automatically converted into a pointer to
the first array element when passed to a subroutine.

Please snip quotations that are meaningless to your message.
 
A

Andrey Tarasevich

...
Just found out that the arrays are pointers.

It is good that you "just" found it out, since it will be easier to
forget about it. Arrays are _not_ pointers (even though in some contexts
they might appear to behave as pointers).
How can i print out the
content of the location of the pointers? that is,

Would you please clarify? What is "content of the location of the
pointers" in this case?
 
K

Keith Thompson

CBFalconer said:
... snip ...

Just found out that the arrays are pointers. How can i print out
the content of the location of the pointers? that is,

printf("%.8f", *&someArrName[7]);

correction appreciated...

%p prints the value of a pointer, which must be cast to void*.

i.e:
printf("%p\n", (void *)someArrName);

Note that someArrName is automatically converted into a pointer to
the first array element when passed to a subroutine.

That *might* be what almurph is looking for, but given his statement
"Just found out that the arrays are pointers", it's impossible to tell
what he really means. In particular, since on his first attempt he
was trying to print floating-point values using "%.8f", I seriously
doubt that he's interested in printing pointer values.

I suggest waiting for him to post some actual code rather than trying
to guess.
 
G

Guest

oh, we've heard of them. Look up Heisenbug. Have you tried using
a debugger instead of printf()s. It might disturb your program
less.

Besides undefined behaviour and ill conditioned calculations
the other possibility is that by inserting the printf()s you have
changed the program code so it is actually doing a different
calculation.

   I am doing stuff like:
printf("%.8f", someArrName[7]);
   I can see the variables but the net result is different.
   When i exclude the printf statements I get a different net result!
Strange no?
Code?  There are too many options to consider without real code.  Can
you cut down the program so that it still does this?  If not, what is
it that you removed just before it started to behave again?

hello? Did you see this bit?

POST YOUR CODE


<snip>


--
Nick Keighley

the unemployed programmer had a problem. "I know", said the
programmer,
"I'll just learn perl." the unemployed programmer now had two
problems.
Eric Naggum
 
A

almurph

oh, we've heard of them. Look up Heisenbug. Have you tried using
a debugger instead of printf()s. It might disturb your program
less.

Besides undefined behaviour and ill conditioned calculations
the other possibility is that by inserting the printf()s you have
changed the program code so it is actually doing a different
calculation.
   I am doing stuff like:
printf("%.8f", someArrName[7]);
   I can see the variables but the net result is different.
   When i exclude the printf statements I get a different net result!
Strange no?
Code?  There are too many options to consider without real code.  Can
you cut down the program so that it still does this?  If not, what is
it that you removed just before it started to behave again?

hello? Did you see this bit?

POST YOUR CODE

<snip>

--
Nick Keighley

the unemployed programmer had a problem.  "I know", said the
programmer,
  "I'll just learn perl."  the unemployed programmer now had two
problems.
                   Eric Naggum

Thanks everyone for your comments. Here is some code that tries to
explain the problem?

float *MyArray1;
float *MyArray2;


float MethodName(var1, var2)
{
somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);
printf("%.15f\n", MyArray1[Att]); /* this is my diagnostic line */
printf("%.15f\n", MyArray2[Att]); /* this is also my diagnostic line
*/

if (somefloatVal == someothervale)
{
//do something
}

}


The problem is that when I insert the printf() statements I get one
net result. When I comment them out I get another result. Conclusion?
Insertion of the prinf statements effects the variables in some way. I
thinks its because they are pointers.

Question: How do I print up the contents of them without effection
them? ie how do i view the contents at the memory location without
"disturbing" the contents?

I know that &MyArray1[Att] will pump out the memory location but I
need undisturbed contents.

Would appreciate any code samples/comments/suggestions/diagnostics
that you may be able to offer.

Cheers,
Al.
 
C

Chris Dollin

Thanks everyone for your comments. Here is some code that tries to
explain the problem?

float *MyArray1;
float *MyArray2;

Those variables will be set to null.
float MethodName(var1, var2)
{
somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);
printf("%.15f\n", MyArray1[Att]); /* this is my diagnostic line */
printf("%.15f\n", MyArray2[Att]); /* this is also my diagnostic line

If something hasn't set `MyArray1` and `MyArray2` (and `Att`) to
sane values, this has undefined behaviour -- probably, but not
certainly, accessing vaguely random parts of the machine store.

Adding more code can change the vaguely random parts that are
accessed, or their contents.
The problem is that when I insert the printf() statements I get one
net result. When I comment them out I get another result. Conclusion?

You broke something.
Insertion of the prinf statements effects the variables in some way.

"Affects", not "Effects". To effect something or something is to
make it happen or become; to affect something is to change it.

The behaviour you see can be explained without any change to the
variables at all. It would be sufficient for the things they point
to to be different. Suppose, for example, that `MyArray1[Att]`
had a value that could be treated as the address of code following
`MethodName`. Inserting an extra `printf` could shuffle the rest
of the code downwards, so that `MyArray1[Att]` pointed at "earlier",
ie different code.

I'm not saying that /is/ what happened; it's something that /could/
happen, and hence casts doubt on the certainty of your conclusion.
Would appreciate any code samples/comments/suggestions/diagnostics
that you may be able to offer.

Fixing code we can't see is a trifle hard.
 
M

mark.bluemel

Thanks everyone for your comments. Here is some code that tries to
explain the problem?

What you've given us barely counts as code...
float *MyArray1;
float *MyArray2;

Do you ever initialise these pointers to point at anything?

If you don't, you can't expect anything particular to happen.

If you do, why haven't you shown us what you initialise them to?
float MethodName(var1, var2)
{
        somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);

What is "Att"? For that matter what is "somefloatVal"?
        printf("%.15f\n", MyArray1[Att]);     /* this is my diagnostic line */
        printf("%.15f\n", MyArray2[Att]);     /* this is also my diagnostic line
*/

        if (somefloatVal == someothervale)
        {
                //do something
        }

}

        The problem is that when I insert the printf() statements I get one
net result. When I comment them out I get another result. Conclusion?
Insertion of the prinf statements effects the variables in some way. I
thinks its because they are pointers.

It might be related to them being uninitialised pointers...
        Question: How do I print up the contents of them without effection
them? ie how do i view the contents at the memory location without
"disturbing" the contents?

        I know that &MyArray1[Att] will pump out the memory location but I
need undisturbed contents.

        Would appreciate any code samples/comments/suggestions/diagnostics
that you may be able to offer.

Main suggestion - give us enough of your code to be able to see what
you are really doing.

As someone (Eric?) has commented before - if you don't know what's
wrong, you don't know enough to decide which bits of code we need to
see...
 
J

James Kuyper

As Nick said "Did you see this bit?" Why is it so hard to convince you
to post a complete program?

....
Thanks everyone for your comments. Here is some code that tries to
explain the problem?

float *MyArray1;
float *MyArray2;


float MethodName(var1, var2)
{
somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);
printf("%.15f\n", MyArray1[Att]); /* this is my diagnostic line */
printf("%.15f\n", MyArray2[Att]); /* this is also my diagnostic line
*/

if (somefloatVal == someothervale)
{
//do something
}

}

Your code doesn't #include <stdio.h>. It doesn't initialize MyArray1,
MyArray2. It doesn't define Att or somefloatVal. It doesn't use var1 or
var2. It doesn't call MethodName(). Whatever is going wrong almost
certainly has something to do with the code you DID NOT provide us. In
particular, if you never #included <stdio.h>, or never initialized the
two pointers, the behavior of your code is undefined, which could easily
explain the results you got.

There's nothing unusual about this - if someone doesn't understand why a
program is malfunctioning, by definition, they don't know what part of
the program is causing the problem. In my experience, it's almost always
the case that when someone provides only part of their program asking
why it doesn't work, it's not the part that contains the most important
error.

Please provide a small, simple, complete program that demonstrates your
problem. Without that information, it's like asking a Doctor "Why am I
coughing", while only allowing him to examine your mouth - the cause of
your cough is quite likely somewhere else. The same is true of your program.
The problem is that when I insert the printf() statements I get one
net result. When I comment them out I get another result.

That's almost completely useless information. Tell us precisely what
results you get with each run of the program. Often, the details of what
those results are can give us a clue as to why they were produced. Don't
assume that you know that it doesn't matter. By definition, you don't
know why your program is misbehaving; therefore, your judgment cannot be
trusted as to which things are relevant and which things aren't. Just
give us all of the information - we'll know how to skip the truly
irrelevant parts.
 
J

Jens Thoms Toerring

Thanks everyone for your comments. Here is some code that tries to
explain the problem?
float *MyArray1;
float *MyArray2;
float MethodName(var1, var2)
{
somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);
printf("%.15f\n", MyArray1[Att]); /* this is my diagnostic line */
printf("%.15f\n", MyArray2[Att]); /* this is also my diagnostic line
*/
if (somefloatVal == someothervale)
{
//do something
}

Sorry, but that's not "YOUR CODE", it's cut down tha much that it
doesn't give any clue about what you're doing. You don't even tell
what your mysterious "net result" is supposed to be. Is it 'some-
floatVal'? Where is it defined? Where and how are 'MyArray1' and
'MyArray2' initialized? What's 'Att'?
The problem is that when I insert the printf() statements I get one
net result. When I comment them out I get another result. Conclusion?
Insertion of the prinf statements effects the variables in some way. I
thinks its because they are pointers.

What pointers?

All your descriptions are much too vague to allow any diagnosis.
You don't even tell how much the "net value" changes between the
original program and the one with the additional printf() state-
ments. Is it completely off or does it differ by just some place
far after the decimal point? Are you using the comparison of
'somefloatVal' and 'someothervale' as a measure even though
comparing two floating point values for equality is nearly always
the wrong thing to do?
Question: How do I print up the contents of them without effection
them? ie how do i view the contents at the memory location without
"disturbing" the contents?

You don't disturb the content of a memory location by looking up
it's content. This isn't quantum mechanics;-)
I know that &MyArray1[Att] will pump out the memory location but I
need undisturbed contents.

What means "pump out the memory location"? '&MyArray1[Att]' is the
address of a memory location, not it's content.
Would appreciate any code samples/comments/suggestions/diagnostics
that you may be able to offer.

Please post some real code - as long as you don't know what the
problem actually is you also can't be sure what is relevant and
what is not. At the moment my bet would be on either some memory
corruption in the code you don't show or a misunderstanding of
how floating point calculations work. But as long as you don't
give any really relevant information to go by it's impossible
to be sure. So, please, tell exactly what you observe, how and
why it is different from what you expect and show some real code.

Regards, Jens
 
G

Guest

Did you try that?


It's normal not to quote sigs.


Thanks everyone for your comments. Here is some code that tries to
explain the problem?

float *MyArray1;
float *MyArray2;

why did you call these arrays as they plainly aren't?
float MethodName(var1, var2)

what types are var1 and var2? Are you using implicit int?
Should you be using it? [no, you shouldn't]. What parameters are
passed to MethodName? What type are they?

{
        somefloatVal = Method2(MyArray1[Att], MyArray2[Att]);
        printf("%.15f\n", MyArray1[Att]);     /* this is my diagnostic line */
        printf("%.15f\n", MyArray2[Att]);     /* this is also my diagnostic line
*/

        if (somefloatVal == someothervale)
        {
                //do something
        }

}

"I refer the Honorable Member to the reply I gave to his previous
question"

<snip>
 
C

CBFalconer

Jens said:
No, I was unfortunately a bit too late for that;-)

Core store normally had several available cycles. One was
read/restore. Another was read and leave zeroed (preparatory for
write). A third was write to zeroed location. These covered the
requirements.
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top