I have a program with creates an array and fills its with random numbers. It then checks for any duplicates and prints out how many times each duplicate has occurred. All works fine except Memcheck is telling me there are 39 errors and I cannot figure out what is causing them (I think the issue is coming from the repeated() method)?
我有一个程序创建一个数组并用随机数填充它。然后,它检查任何副本,并打印出每个副本发生了多少次。除了Memcheck告诉我有39个错误,我不知道是什么导致了这些错误(我认为问题来自于repeat()方法)之外,其他都可以正常工作。
Cheers, Harry
干杯,哈利
Code -
代码,
#include <stdio.h>
#include <stdlib.h>
#include "random.h"
int main(void) {
int array_size = 0;
int *my_array;
int i = 0;
printf("Enter the size of the array:\n");
scanf("%d", &array_size);
my_array = malloc(array_size * sizeof my_array[0]);
if (NULL == my_array) {
fprintf(stderr, "memory allocation failed!\n");
return EXIT_FAILURE;
}
for (i = 0; i < array_size; i++) {
my_array[i] = rand() % array_size;
}
printf("What's in the array:\n");
for (i = 0; i < array_size; i++) {
printf("%d ", my_array[i]);
}
printf("\n");
repeated(my_array, array_size);
free(my_array);
return EXIT_SUCCESS;
}
void repeated(int *my_array, int array_size) {
int *array_tracker;
int i;
array_tracker = malloc(array_size * sizeof array_tracker[0]);
for (i = 0; i < array_size; i++) {
array_tracker[my_array[i]]++;
}
for (i = 0; i < array_size; i++) {
if (array_tracker[i] > 1) {
printf("%d occurs %d times\n", i, array_tracker[i]);
}
}
free(array_tracker);
}
Memcheck output -
Memcheck输出-
==23999== Memcheck, a memory error detector
==23999== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==23999== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==23999== Command: ./lab20c-prog
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x40091F: repeated (random.c:15)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x519148A: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Use of uninitialised value of size 8
==23999== at 0x518DD8B: _itoa_word (in /usr/lib64/libc-2.24.so)
==23999== by 0x5192552: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x518DD95: _itoa_word (in /usr/lib64/libc-2.24.so)
==23999== by 0x5192552: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x5192669: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x5191536: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999== Conditional jump or move depends on uninitialised value(s)
==23999== at 0x51915B9: vfprintf (in /usr/lib64/libc-2.24.so)
==23999== by 0x5199718: printf (in /usr/lib64/libc-2.24.so)
==23999== by 0x40092E: repeated (random.c:16)
==23999== by 0x400799: main (random.c:42)
==23999==
==23999==
==23999== HEAP SUMMARY:
==23999== in use at exit: 0 bytes in 0 blocks
==23999== total heap usage: 4 allocs, 4 frees, 1,052,792 bytes allocated
==23999==
==23999== All heap blocks were freed -- no leaks are possible
==23999==
==23999== For counts of detected and suppressed errors, rerun with: -v
==23999== Use --track-origins=yes to see where uninitialised values come from
==23999== ERROR SUMMARY: 39 errors from 7 contexts (suppressed: 0 from 0)
1 个解决方案
#1
1
You never initialized the contents of array_tracker
. So when you do:
您从未初始化array_tracker的内容。所以,当你做的事:
array_tracker[my_array[i]]++;
you're incrementing an uninitialized value. That's why you get lots of complaints about using uninitialized values.
您正在递增一个未初始化的值。这就是为什么使用未初始化值会引起很多抱怨。
You could use calloc()
instead of malloc()
to allocate space and initialize all the elements to 0
.
您可以使用calloc()代替malloc()来分配空间并将所有元素初始化为0。
array_tracker = calloc(array_size, sizeof array_tracker[0]);
#1
1
You never initialized the contents of array_tracker
. So when you do:
您从未初始化array_tracker的内容。所以,当你做的事:
array_tracker[my_array[i]]++;
you're incrementing an uninitialized value. That's why you get lots of complaints about using uninitialized values.
您正在递增一个未初始化的值。这就是为什么使用未初始化值会引起很多抱怨。
You could use calloc()
instead of malloc()
to allocate space and initialize all the elements to 0
.
您可以使用calloc()代替malloc()来分配空间并将所有元素初始化为0。
array_tracker = calloc(array_size, sizeof array_tracker[0]);