PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > printf on alcohol
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
printf on alcohol

Réponse
 
LinkBack Outils de la discussion
Vieux 19/10/2007, 01h03   #1
estantep@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut printf on alcohol

Has anybody ever seen a thing like this?

### code:

double link_bw_cost(int a, int b){

double ro;

ro = ( (double) link_usage[a][b].bandw / (double) link[a][b].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][b].bandw, a, b, link[a][b].bandw);


### when running:

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

  Réponse avec citation
Vieux 19/10/2007, 01h55   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printf on alcohol

In article <1192748632.436515.173000@q5g2000prf.googlegroups. com>,
<estantep@gmail.com> 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][b].bandw / (double) link[a][b].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][b].bandw, a, b, link[a][b].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][b].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
  Réponse avec citation
Vieux 19/10/2007, 02h07   #3
estantep@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printf on alcohol

Bingo!

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

Thank you very much Walter!


On Oct 18, 8:55 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <1192748632.436515.173...@q5g2000prf.googlegroups. com>,
>
> <estan...@gmail.com> 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][b].bandw / (double) link[a][b].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][b].bandw, a, b, link[a][b].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][b].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



  Réponse avec citation
Vieux 19/10/2007, 02h12   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printf on alcohol

estantep@gmail.com 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][b].bandw / (double) link[a][b].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][b].bandw, a, b, link[a][b].bandw);
>
>
> ### when running:
>
> DEBUG - a = 0 e b = 2
> DEBUG - link_usage[0][2].bandw = 0 link[1078689792][0].bandw = 2


It's ful 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][b].bandw, /* %ld */
a, /* %d */
b, /* %d */
link[a][b].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][b] and link[a][b] 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) kst-u@mib.org <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"
  Réponse avec citation
Vieux 19/10/2007, 18h51   #5
Default User
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printf on alcohol - TPA

estantep@gmail.com 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>
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h14.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11521 seconds with 13 queries