[英]Why “*** stack smashing detected ***: <unknown> terminated Aborted (core dumped) ” error is showing in this code?
I am implementing a simple selection sort code in this way:我正在以这种方式实现一个简单的选择排序代码:
int* in_sort (int a[], int l)
{
int i, j, k, m;
for (i = 0; i < l - 1; i++) {
m = a[i];
k = i;
for (j = i; j < l; j++) {
if (a[j] < m) {
k = j;
m = a[j];
}
}
if (i != k) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
return a;
}
int main()
{
int a [] = {2, 5, 2, 56, 2, 567, 322, 456, 5, 34};
int *b = in_sort(a, 10);
for (int i = 0; i < 10; i++) cout << b[i] << ' ';
return 0;
}
But I'm getting this error:但我收到了这个错误:
* stack smashing detected * : terminated Aborted (core dumped)
*检测到堆栈粉碎* :终止中止(核心转储)
Why is this happening and how can I solve this?为什么会发生这种情况,我该如何解决? I have watched other answers related to this particular error but can not understand what is wrong in my case.
我已经看过与此特定错误相关的其他答案,但无法理解我的情况出了什么问题。
I think it's this code:我认为是这段代码:
if (i != k) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
You probably meant a[k]
, not a[j]
here.您可能指的是
a[k]
,而不是a[j]
。 j
will equal l
, and so a[j]
is just outside the bounds of the array. j
将等于l
,因此a[j]
刚好在数组的边界之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.