c++ malloc 分段错误
我有一个关于 malloc() 的问题。这很奇怪。 我的代码如下。 我使用随机生成器来生成数组的元素。 该数组由 malloc() 打开。 如果数组大小小于8192,就可以。如果大小大于8192,则显示段错误。
void random_generator(int num, int * array) {
srand((unsigned)time(0));
int random_integer;
for(int index=0; index< num; index++){
random_integer = (rand()%10000)+1;
*(array+index) = random_integer;
cout << index << endl;
}
}
int main() {
int array_size = 10000;
int *input_array;
input_array = (int*) malloc((array_size));
random_generator(8192, input_array); // if the number is larger than 8192, segment fault
free(input_array);
}
I have a problem about malloc(). It is weird.
My code is in the following.
I use random generator to generate elements for an array.
The array is opened by malloc().
If the array size is smaller than 8192, it is OK. If the size is larger than 8192, it shows segment fault.
void random_generator(int num, int * array) {
srand((unsigned)time(0));
int random_integer;
for(int index=0; index< num; index++){
random_integer = (rand()%10000)+1;
*(array+index) = random_integer;
cout << index << endl;
}
}
int main() {
int array_size = 10000;
int *input_array;
input_array = (int*) malloc((array_size));
random_generator(8192, input_array); // if the number is larger than 8192, segment fault
free(input_array);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
malloc()
采用字节大小,而不是元素数量。int
的大小通常为 4 个字节,因此您实际上只为 2500 个整数分配了足够的内存。您正在分配array_size
字节,而您应该分配array_size * sizeof(int)
字节。修复
因此,该错误将通过
input_array = (int*) malloc(array_size * sizeof(int));
。 PS 永远不要假设您知道
int
或任何其他数据类型,因为它依赖于平台。始终使用sizeof()
。PPS 这实际上是一个 C 问题,而不是一个 C++ 问题。如果您实际上使用 C++,您应该考虑使用
new
和delete []
而不是malloc()
和free()< /code>,或者更好的是使用
std::vector
而不是数组,正如 Neil 指出的那样。malloc()
takes the size in bytes, not the number of elements. The size of anint
is typcially 4 bytes, so you are actually allocating only enough memory for 2500 integers. You are allocatingarray_size
bytes, while you should be allocatingarray_size * sizeof(int)
bytes.So, the error will be fixed by
input_array = (int*) malloc(array_size * sizeof(int));
P.S. Never assume that you know the size of an
int
or any other data type, as it is platform dependent. Always usesizeof()
.P.P.S. This is really a C question, rather than a C++ question. If you are actually using C++, you should consider using
new
anddelete []
instead ofmalloc()
andfree()
, or better yet usestd::vector
instead of an array, as Neil pointed out.您想要:
您可能会考虑更简单的:
甚至:
而不必担心调用 free 或 delete,或者担心异常。
You want:
you might consider the much simpler:
or even:
and not have to worry about calling free or delete, or about exceptions.
看到它的 C++,你最好执行以下操作:
编辑:或者更好:
甚至不用担心释放:D
Seeing as its C++ you'd be much better off doing the following:
Edit: or better still:
And not even worry about the deallocating :D
input_array = (int*) malloc(sizeof(int) * (array_size));
这是因为 malloc 的参数是字节数,而 int 一般为 4 个字节长。
input_array = (int*) malloc(sizeof(int) * (array_size));
It's because the param to malloc is the byte count and int is generally 4 bytes long.
您使用的 malloc 调用是错误的。你应该使用:
我认为。
The malloc call you are using is wrong. You should be using:
I think.
您正在分配 array_size 字节,但需要 array_size 整数。尝试在 malloc 中分配 array_size*sizeof(int) 字节。
You're allocating array_size bytes, but you need array_size integers. Try allocating array_size*sizeof(int) bytes in malloc.
其他人都已经掩盖了动态分配中的错误。
但你真的想要动态分配吗?
Everybody else has coverted the mistake in dynamic allocation.
But do you really want dynamic allocation?