函数中大小为 4 的 read/write 无效
Invalid read/write of size 4 in function
我正在尝试实现一个从 int 数组中提取一些随机数的函数,而不更改原始数组。我写了这个函数
int get_random_int(int * from, int from_length, int * result, int result_length) {
int i, pos, tmp;
int * source = malloc(sizeof(int) * from_length); // Line 720
/* Make sourcet to avoid to manipulate int array */
for (i = 0; i < from_length; i++) {
source[i] = from[i];
};
for (i = 0; i < result_length; i++) {
/* Get random pos */
pos = rand() % from_length;
/* Swap first random position with last position */
tmp = source[pos];
source[pos] = source[from_length]; // Line 732
source[from_length] = tmp; // Line 733
/* Decrease last index */
from_length--;
/* Set random value in result array */
result[i] = tmp;
};
free(source);
return 1;
}
然后我在main中这样调用这个函数
receivers = malloc(sizeof(int) * y);
get_random_int(int_arr, x, receivers, y);
// Some useful stuff
free(receivers);
int_arr 是我要提取值的 int 数组,x 是数组的大小,receivers 是我要保存随机值的指针,y 是他的长度。
例程似乎有效,但是当我尝试 运行 使用 valgrind 的程序时,出现此错误。我真的不明白这是什么问题
==7== Invalid read of size 4
==7== at 0x10BD30: get_random_int (main.c:732)
==7== by 0x10A5A1: main (main.c:198)
==7== Address 0x4bced08 is 0 bytes after a block of size 40 alloc'd
==7== at 0x483877F: malloc (vg_replace_malloc.c:307)
==7== by 0x10BC8F: get_random_int (main.c:720)
==7== by 0x10A5A1: main (main.c:198)
==7==
==7== Invalid write of size 4
==7== at 0x10BD4B: get_random_int (main.c:733)
==7== by 0x10A5A1: main (main.c:198)
==7== Address 0x4bced08 is 0 bytes after a block of size 40 alloc'd
==7== at 0x483877F: malloc (vg_replace_malloc.c:307)
==7== by 0x10BC8F: get_random_int (main.c:720)
==7== by 0x10A5A1: main (main.c:198)
如果您想访问索引长度为 1 的最后一个元素,C 使用从零开始的数组。例如:
source[pos] = source[from_length - 1]; // Line 732
source[from_length - 1] = tmp; // Line 733
我正在尝试实现一个从 int 数组中提取一些随机数的函数,而不更改原始数组。我写了这个函数
int get_random_int(int * from, int from_length, int * result, int result_length) {
int i, pos, tmp;
int * source = malloc(sizeof(int) * from_length); // Line 720
/* Make sourcet to avoid to manipulate int array */
for (i = 0; i < from_length; i++) {
source[i] = from[i];
};
for (i = 0; i < result_length; i++) {
/* Get random pos */
pos = rand() % from_length;
/* Swap first random position with last position */
tmp = source[pos];
source[pos] = source[from_length]; // Line 732
source[from_length] = tmp; // Line 733
/* Decrease last index */
from_length--;
/* Set random value in result array */
result[i] = tmp;
};
free(source);
return 1;
}
然后我在main中这样调用这个函数
receivers = malloc(sizeof(int) * y);
get_random_int(int_arr, x, receivers, y);
// Some useful stuff
free(receivers);
int_arr 是我要提取值的 int 数组,x 是数组的大小,receivers 是我要保存随机值的指针,y 是他的长度。
例程似乎有效,但是当我尝试 运行 使用 valgrind 的程序时,出现此错误。我真的不明白这是什么问题
==7== Invalid read of size 4
==7== at 0x10BD30: get_random_int (main.c:732)
==7== by 0x10A5A1: main (main.c:198)
==7== Address 0x4bced08 is 0 bytes after a block of size 40 alloc'd
==7== at 0x483877F: malloc (vg_replace_malloc.c:307)
==7== by 0x10BC8F: get_random_int (main.c:720)
==7== by 0x10A5A1: main (main.c:198)
==7==
==7== Invalid write of size 4
==7== at 0x10BD4B: get_random_int (main.c:733)
==7== by 0x10A5A1: main (main.c:198)
==7== Address 0x4bced08 is 0 bytes after a block of size 40 alloc'd
==7== at 0x483877F: malloc (vg_replace_malloc.c:307)
==7== by 0x10BC8F: get_random_int (main.c:720)
==7== by 0x10A5A1: main (main.c:198)
如果您想访问索引长度为 1 的最后一个元素,C 使用从零开始的数组。例如:
source[pos] = source[from_length - 1]; // Line 732
source[from_length - 1] = tmp; // Line 733