Forum: PC-Programmierung int overflow zeitmessung stürzt ab


von X. H. (shadow0815)


Lesenswert?

ich wollte messen wie lange t braucht bis zum overflow. aber wenn ich
das Nötige
// end = clock();
einkommentiere stürtzt das programm ab. jemand ne idee wieso?

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <limits.h>
5
#include <time.h>
6
7
8
int t=0,s=0;
9
clock_t start, end;
10
double cpu_time_used;
11
12
int main(void)
13
{
14
    printf("size of int: %d bits\n",sizeof(t)*8);
15
    printf("The maximum value of INT = %d\n", INT_MAX);
16
    while(1) {
17
        t++;
18
        if(t<0) {
19
            end = clock();
20
            s++;
21
            t=0;
22
            printf("overflow: %i\n",s);
23
            cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
24
            printf("cpu time used: %i\n",cpu_time_used);
25
            start = clock();
26
        }
27
    }
28
29
    return 0;
30
}

EDIT:
1 Fehler gefunden:

Ach misst, natürlich gehört die zeile
// end = clock();
ins if(..)

aber trotzdem ist die Zeitmessung falsch. mal ne positive zeit, beim 
Nächsten Programmstart ne negative ...

: Bearbeitet durch User
von Dirk B. (dirkb2)


Lesenswert?

X. H. schrieb:

> double cpu_time_used;
> ...
>             printf("cpu time used: %i\n",cpu_time_used);


Welche Warnung gibt denn der Compiler zu der printf-Zeile aus?
(Wenn es keine ist, setz den Warnlevel höher!)

von Peter II (Gast)


Lesenswert?

X. H. schrieb:
> einkommentiere stürtzt das programm ab. jemand ne idee wieso?

starte es einfach im Debugger, dann zeigt er doch zumindest die Zeile an 
wo es ein Problem gibt.

von X. H. (shadow0815)


Lesenswert?

Dirk B. schrieb:
> X. H. schrieb:
>
>> double cpu_time_used;
>> ...
>>             printf("cpu time used: %i\n",cpu_time_used);
>
>
> Welche Warnung gibt denn der Compiler zu der printf-Zeile aus?
> (Wenn es keine ist, setz den Warnlevel höher!)

Ah, danke. Daran hats gelegen. Iss ja gleitpunkt. hab irgendwie immer an 
ganzzahl-ergebnis gedacht.
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <stdint.h>
4
#include <limits.h>
5
#include <time.h>
6
7
8
int t=0,s=0;
9
clock_t start, end;
10
double cpu_time_used;
11
12
int main(void)
13
{
14
    printf("size of int: %lu bits\n",(unsigned long)sizeof(t)*8);
15
    printf("The maximum value of INT = %d\n", INT_MAX);
16
    printf("----------\n");
17
    start = clock();
18
    while(1) {
19
        t++;
20
        if(t<0) {
21
            end = clock();
22
            s++;
23
            t=0;
24
            printf("overflow nr.: %d\n",s);
25
            cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
26
            printf("cpu time used: %fs\n",cpu_time_used);
27
            printf("----------\n");
28
            start = clock();
29
        }
30
    }
31
32
    return 0;
33
}

: Bearbeitet durch User
von Dirk B. (dirkb2)


Lesenswert?

Für die Nachwelt:

Beim printf stand ein %i, cpu_time_used ist aber vom Type double.
Das passt nicht zusammen.

von Peter II (Gast)


Lesenswert?

Dirk B. schrieb:
> Beim printf stand ein %i, cpu_time_used ist aber vom Type double.
> Das passt nicht zusammen.

das sollte aber nichts abstürzen, es kommt nur nicht die richtige Zahl 
raus.

von Rolf M. (rmagnus)


Lesenswert?

Peter II schrieb:
> Dirk B. schrieb:
>> Beim printf stand ein %i, cpu_time_used ist aber vom Type double.
>> Das passt nicht zusammen.
>
> das sollte aber nichts abstürzen,

Das darf auch abstürzen, ist aber in der Regel eher unwahrscheinlich.

> es kommt nur nicht die richtige Zahl raus.

Das hat er doch auch geschrieben:

X. H. schrieb:
> aber trotzdem ist die Zeitmessung falsch. mal ne positive zeit, beim
> Nächsten Programmstart ne negative ...

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.