printf on alcohol

E

estantep

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
 
W

Walter Roberson

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.
 
E

estantep

Bingo!

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

Thank you very much Walter!


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
 
K

Keith Thompson

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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top