用calloc初始化的随机数填充指针数组的问题

时间:2019-03-19 16:39:03

标签: c for-loop pointers crash calloc

  1. 我的程序应该创建10个向量,每个向量的随机维度在1到10之间,每个维度都填充有0到24之间的随机数。

    int * makeVector(int dimension) {
      int * Vector = NULL;
    
      Vector = (int*) calloc(dimension - 1, sizeof(int));
      if (NULL == Vector) exit(1);
    
      srand(time(0));
    
      for (int i = 0; i < (dimension - 1); i++) {
        Vector[i] = rand() % 25;
      }
    
      return Vector; 
    }
    
    int main() {
      srand(time(0));
    
      for (int i = 1; i <= 10; i++) {
        printf("-----%d. Vector-----\n", i);
        fflush(stdout);
        int dimension = (rand() % 10) + 1;
        int *Vector = NULL;
    
        memcpy(Vector, makeVector(dimension), dimension);
    
        for (int k = 1; k <= dimension; k++) {
          printf("%d. Dimension: %d\n", k, Vector[k - 1]);
        }
        if (Vector) {
          free(Vector);
          Vector = NULL;
        }
      }
      return 0;
    }
    
  2. 我的问题:没有警告,也没有错误! 尽管makeVector函数中的For-Loop似乎根本不起作用。

    for (int i = 0; i < (dimension - 1); i++) {
         Vector[i] = rand() % 25;
    }
    

int我第一次只等于0,什么也没有发生。 For-Loop跳过,程序终止于退出值:-1.073.741.819

PS:我是编程新手,可能会解释Debugger错误。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

您的主要问题是:

memcpy(Vector, makeVector(dimension), dimension);

Vector 是空指针,因此行为未定义

Vector = makeVector(dimension);

另一个问题是要重做srand(time(0)); makeVector ,执行时间几乎为零,因此您需要重新初始化随机数生成以再次获得相同的值(时间会改变)。

在执行过程中仅执行一次srand(time(0));


for (int k = 1; k <= dimension; k++) {
  printf("%d. Dimension: %d\n", k, Vector[k - 1]);
}

您可以访问向量的未初始化的最后一个条目,因为在 makeVector 中:

for (int i = 0; i < (dimension - 1); i++) {
    Vector[i] = rand() % 25;
 }

必须

for (int k = 1; k < dimension; k++) {
  printf("%d. Dimension: %d\n", k, Vector[k - 1]);
}

或者当然可以修改初始化循环,以免丢失最后一个条目


更改后的编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra m.c
pi@raspberrypi:/tmp $ ./a.out
-----1. Vector-----
1. Dimension: 18
2. Dimension: 21
3. Dimension: 22
4. Dimension: 21
5. Dimension: 2
6. Dimension: 0
7. Dimension: 24
-----2. Vector-----
1. Dimension: 20
2. Dimension: 24
3. Dimension: 13
-----3. Vector-----
1. Dimension: 3
-----4. Vector-----
1. Dimension: 5
2. Dimension: 24
3. Dimension: 10
4. Dimension: 4
5. Dimension: 20
-----5. Vector-----
1. Dimension: 19
2. Dimension: 7
3. Dimension: 24
4. Dimension: 24
5. Dimension: 16
6. Dimension: 11
-----6. Vector-----
1. Dimension: 10
2. Dimension: 14
3. Dimension: 17
4. Dimension: 9
5. Dimension: 9
-----7. Vector-----
1. Dimension: 9
2. Dimension: 8
3. Dimension: 19
4. Dimension: 9
5. Dimension: 7
-----8. Vector-----
1. Dimension: 6
2. Dimension: 8
3. Dimension: 23
-----9. Vector-----
1. Dimension: 14
2. Dimension: 13
3. Dimension: 12
4. Dimension: 13
-----10. Vector-----
1. Dimension: 18
2. Dimension: 10
3. Dimension: 3
4. Dimension: 12
5. Dimension: 20
pi@raspberrypi:/tmp $ 

valgrind

下执行
pi@raspberrypi:/tmp $ valgrind ./a.out
==29590== Memcheck, a memory error detector
==29590== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==29590== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==29590== Command: ./a.out
==29590== 
-----1. Vector-----
1. Dimension: 21
2. Dimension: 16
3. Dimension: 3
4. Dimension: 11
5. Dimension: 11
6. Dimension: 8
7. Dimension: 13
8. Dimension: 8
9. Dimension: 23
-----2. Vector-----
1. Dimension: 11
2. Dimension: 3
3. Dimension: 5
4. Dimension: 21
5. Dimension: 18
6. Dimension: 4
-----3. Vector-----
1. Dimension: 20
2. Dimension: 15
3. Dimension: 4
4. Dimension: 17
5. Dimension: 0
6. Dimension: 2
7. Dimension: 21
-----4. Vector-----
1. Dimension: 24
2. Dimension: 6
3. Dimension: 20
4. Dimension: 10
5. Dimension: 1
6. Dimension: 19
-----5. Vector-----
1. Dimension: 19
2. Dimension: 24
3. Dimension: 19
-----6. Vector-----
1. Dimension: 7
2. Dimension: 10
-----7. Vector-----
1. Dimension: 5
2. Dimension: 6
3. Dimension: 4
4. Dimension: 10
5. Dimension: 11
6. Dimension: 2
7. Dimension: 4
-----8. Vector-----
1. Dimension: 0
2. Dimension: 1
-----9. Vector-----
1. Dimension: 6
2. Dimension: 18
3. Dimension: 12
4. Dimension: 10
5. Dimension: 16
-----10. Vector-----
==29590== 
==29590== HEAP SUMMARY:
==29590==     in use at exit: 0 bytes in 0 blocks
==29590==   total heap usage: 11 allocs, 11 frees, 1,212 bytes allocated
==29590== 
==29590== All heap blocks were freed -- no leaks are possible
==29590== 
==29590== For counts of detected and suppressed errors, rerun with: -v
==29590== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)