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

problem with filling a pointer array with random numbers initialised by 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 i 第一次只等于 0,没有任何反应。 For 循环跳过,程序终止,退出值:-1.073.741.819

PS:我是编程新手...可能会错误地解释调试器。

提前致谢!

你的主要问题是:

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)