gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:20:33 25 4
gpt4 key购买 nike

  1. 我的程序应该创建 10 个 vector ,每个 vector 的随机维度在 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]);
}

您可以访问 vector 的未初始化的最后一个条目,因为在 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)

关于c - 用 calloc 初始化的随机数填充指针数组的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55245965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com