Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 他工作得很奇怪_C++_Increment - Fatal编程技术网

C++ 他工作得很奇怪

C++ 他工作得很奇怪,c++,increment,C++,Increment,这个程序应该给出任意大小阶乘的最后100位数字。然而,main()中的counter2++出现了一些奇怪的情况。每当循环在main()函数中运行时(99次),计数器2将递增+1。但是,这是显示的内容: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 5

这个程序应该给出任意大小阶乘的最后100位数字。然而,main()中的counter2++出现了一些奇怪的情况。每当循环在main()函数中运行时(99次),计数器2将递增+1。但是,这是显示的内容:

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
71
86
90
123
164
196
207
254
300
362
432
471
551
620
630
708
761
772
857
896
985
1036
1100
1116
1207
1209
1280
1356
1417
1452
1512
计数器2最终是1512,而不是100,但如果我从main()中删除mult(I)或carry(),它将显示100。为什么counter2最终是1512而不是100

#include <iostream>

using namespace std;

int numbers[100];
int counter2 = 0;

void init(){
//sets elements 1-99 of numbers[] to 0, increments counter2 by 1, sets numbers[0] = 1
    for (int i = 1; i < 100; i++){
        numbers[i] = 0;
    }
    numbers[0] = 1;
    counter2++;
}

void mult(int x){
//multiplies each element by 1 through n to calculate for !n
//this is used to represent a very large number without using a BigInt library
//the nth element is a placeholder for the n+1 position of the number
//e.g 2nd element represents 100-900 of the number, 4th represents 1000-9000, etc
//carry() is used to take care of overflow, so that it's only 1 digit per element
    for (int i = 0; i < 100; i++){
        numbers[i] *= x;
    }
}

void carry(){
//in order to make previous function work, this adds any overflow to the next
//element. e.g: 8 * 4 = 32, 3 is added to numbers[i+1], sets numbers[i] to 2
    int counter = 0;
    for (int i = 0; i < 100; i++){
        if (numbers[i] >= 10){
            counter = numbers[i] / 10;
            numbers[i+1] += counter;
            numbers[i] = numbers[i] % (counter * 10);
        }
    }
}

int main()
{
    init();
    for (int i = 2; i < 101; i++){
    //calculates the last 100 digits of !100, but counter2 ends up being 1512
        mult(i);
        carry();
        counter2++;
        cout << counter2 << endl;
    }
}
#包括
使用名称空间std;
整数[100];
int计数器2=0;
void init(){
//将数字[]的元素1-99设置为0,将计数器2增加1,将数字[0]=1
对于(int i=1;i<100;i++){
数字[i]=0;
}
数字[0]=1;
计数器2++;
}
无效多重(整数x){
//将每个元素乘以1到n以计算!n
//这用于表示非常大的数字,而不使用BigInt库
//第n个元素是数字的n+1位置的占位符
//e、 g第2个元素表示数字的100-900,第4个元素表示1000-9000,以此类推
//carry()用于处理溢出,因此每个元素只有1位
对于(int i=0;i<100;i++){
数字[i]*=x;
}
}
无效进位(){
//为了使前一个函数工作,这会向下一个函数添加任何溢出
//元素。例如:8*4=32,将3添加到数字[i+1],将数字[i]设置为2
int计数器=0;
对于(int i=0;i<100;i++){
如果(数字[i]>=10){
计数器=数字[i]/10;
数字[i+1]+=计数器;
数字[i]=数字[i]%(计数器*10);
}
}
}
int main()
{
init();
对于(int i=2;i<101;i++){
//计算!100的最后100位,但计数器2最终为1512
mult(一);
携带();
计数器2++;

cout您在
carry()
中写入的数字超过了
数组的末尾:

这里,
i
可以是
99
,在这种情况下,
数字[i+1]
是不允许的

从技术上讲,这是正确的。实际情况是覆盖
count2
变量,该变量恰好位于数组之后的内存中

内存错误的一个令人讨厌的地方是,它们可以在很长一段时间内无症状出现,然后在最坏的情况下出现。是检测此类问题的一个很好的工具。

在这方面

 numbers[i+1] += counter;

i==99
时,您正在写入超出数组
数字[100];

的界限,从而更改
int counter2=0;
的值(在您的情况下,但不一定)就在内存中的数字旁边。

你的问题是什么?对不起,为什么计数器2=1512?为什么它会奇怪地递增?没有其他输出,正如其他人所说,它与内存有关。你的平台/编译器是什么?使用Win7+MinGw,我得到的输出是从2到100,也是Win7+MinGw,奇怪的是,为什么这会影响计数器2虽然?@Seb
counter2
很可能与内存中的数组相邻,因此数组外的写入可能会写入
counter2
@Seb:关于未定义的行为,很难理解的是未定义意味着未定义。完全出乎意料的事情可能会发生。因此正确的问题是:它为什么不应该影响
counter2
?因为规范中说什么都可能发生…@Seb:内存错误就是这样。它们可能以看似奇怪的方式表现出来。通常症状与实际问题完全脱离。@ShivanRaptor:简单:不要写超过分配内存的末尾。
 numbers[i+1] += counter;