我知道是什么原因导致了段错误,但是为什么呢?
好的,所以我有一个简单的 C++ 程序,它应该在由整数组成的数组上运行几个排序算法,并跟踪每个算法所花费的时间.. 非常基本,但是我遇到了问题。
当程序第一次启动时,它会询问您想要数组中有多少项。我的作业涉及将数组设置为特定长度,从 100 个项目一直到 750000。它将处理许多值,包括最多 600000 左右。当我尝试 750000 时,它立即出现段错误。一些检查让我发现当第四个数组(长度都相同)初始化时会发生错误。奇怪的是,它只发生在我的操作系统上;在我的学校,它工作没有问题。 (我使用的是最新的 ubuntu,而我的学校使用的是 redhat。不确定这是否有用)
我将包含完整的代码仅供参考,但段错误发生在第 27 行:
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
我知道这一点,因为我在单独的行上初始化了每个数组,并且将 cout 放在中间。 array1、2 和 3 已初始化,然后出现段错误。同样,只有当数组长度超过 600000 左右时才会发生这种情况。少一点就可以了。
完整代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void insertionSort(int array[], int size);
void bubbleSort(int array[], int size);
void mergeSort(int array[], int first, int last, int size);
void quickSort(int array[], int size);
int main()
{
cout << endl << endl << "\t\t**** Extra Credit Assignment- Sorting ****" << endl << endl << endl;
cout << "Enter the number of items to sort: ";
int num;
cin >> num;
while(cin.fail()) // while cin does not recieve an integer
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid entry. Try again: "; // try again
cin >> num;
}
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
int randNum, sizeOfArray = sizeof(array1)/sizeof(array1[0]); // random number, size of the arrays
srand(time(NULL)); // random seed (used with rand())
for(int i = 0; i < sizeOfArray; i++) // traverse through the array
{
randNum = rand() % 2147483647+1; // establish random number (from 1 to 2147483647)
array1[i] = array2[i] = array3[i] = array3[i] = randNum; // set randNum to all arrays at current index
}
time_t beginTime, endTime;
double elapsedTime;
cout << endl << "Elapsed time:" << endl << "\tInsertion Sort-\t";
time(&beginTime);
insertionSort(array1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tBubble Sort-\t";
time(&beginTime);
bubbleSort(array2, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tMerge Sort-\t";
time(&beginTime);
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds"<< endl;
/* ********************* TESTING *************************
// *******************************************************
cout << "Insertion->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
insertionSort(array1, sizeOfArray);
cout << "Insertion->Sorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
cout << "Bubble->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
bubbleSort(array2, sizeOfArray);
cout << "Bubble->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
cout << "Merge->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl;
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
cout << "Merge->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl; */
return 0;
}
void insertionSort(int array[], int size)
{
for(int i = 1; i < size; i++)
{
int item = array[i], index = i;
while(index > 0 && array[index-1] > item)
{
array[index] = array[index-1];
index--;
}
array[index] = item;
}
}
void bubbleSort(int array[], int size)
{
bool sorted = false;
for(int i = 1; i < size && !sorted; i++)
{
sorted = true;
for(int i2 = 0; i2 < size-i; i2++)
{
int nextI = i2+1;
if(array[i2] > array[nextI])
{
swap(array[i2], array[nextI]);
sorted = false;
}
}
}
}
void merge(int array[], int first, int mid, int last, int size)
{
int tempArray[size];
int first1 = first, first2 = mid+1;
int last1 = mid, last2 = last;
int index = first1;
while(first1 <= last1 && first2 <= last2)
{
if(array[first1] < array[first2])
{
tempArray[index] = array[first1];
first1++;
}
else
{
tempArray[index] = array[first2];
first2++;
}
index++;
}
while(first1 <= last1)
{
tempArray[index] = array[first1];
first1++;
index++;
}
while(first2 <= last2)
{
tempArray[index] = array[first2];
first2++;
index++;
}
for(index = first; index <= last; index++)
{
array[index] = tempArray[index];
}
}
void mergeSort(int array[], int first, int last, int size)
{
if(first < last)
{
int mid = (first+last)/2;
mergeSort(array, first, mid, size);
mergeSort(array, mid+1, last, size);
merge(array, first, mid, last, size);
}
}
非常感谢任何帮助。这可能是我的系统的内存限制?我真的不知道哈哈,只是一个想法。
Ok, so I have a simple c++ program that's supposed to run a couple sorting algorithms on an array composed of ints and track the time each one takes.. pretty basic, however I've run into a problem.
When the program first starts, it asks how many items you would like in the array. My assignment involves setting the array at specific lengths from 100 items all the way to 750000. It'll handle many values, including up to around 600000. When I try 750000 however it immediately segfaults. A couple couts here and there led me to discover that the error happens when the fourth array (all of the same length) is initialized. The weird thing is that it only happens on my OS; at my school it works no problem. (i'm on the latest ubuntu while my school uses redhat. not sure if that's useful)
I'll include the complete code just for reference but the segfault occurs at line 27:
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
I know this because I initialized each array on separate lines and put couts in between. array1, 2, and 3 were initialized, then it segfaults. Again this ONLY happens when the arrays are longer than about 600000 or so. Anything less works fine.
Full code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void insertionSort(int array[], int size);
void bubbleSort(int array[], int size);
void mergeSort(int array[], int first, int last, int size);
void quickSort(int array[], int size);
int main()
{
cout << endl << endl << "\t\t**** Extra Credit Assignment- Sorting ****" << endl << endl << endl;
cout << "Enter the number of items to sort: ";
int num;
cin >> num;
while(cin.fail()) // while cin does not recieve an integer
{
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid entry. Try again: "; // try again
cin >> num;
}
int array1[num], array2[num], array3[num], array4[num]; // initialize arrays
int randNum, sizeOfArray = sizeof(array1)/sizeof(array1[0]); // random number, size of the arrays
srand(time(NULL)); // random seed (used with rand())
for(int i = 0; i < sizeOfArray; i++) // traverse through the array
{
randNum = rand() % 2147483647+1; // establish random number (from 1 to 2147483647)
array1[i] = array2[i] = array3[i] = array3[i] = randNum; // set randNum to all arrays at current index
}
time_t beginTime, endTime;
double elapsedTime;
cout << endl << "Elapsed time:" << endl << "\tInsertion Sort-\t";
time(&beginTime);
insertionSort(array1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tBubble Sort-\t";
time(&beginTime);
bubbleSort(array2, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds" << endl << "\tMerge Sort-\t";
time(&beginTime);
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
time(&endTime);
elapsedTime = difftime(endTime, beginTime);
cout << elapsedTime << " seconds"<< endl;
/* ********************* TESTING *************************
// *******************************************************
cout << "Insertion->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
insertionSort(array1, sizeOfArray);
cout << "Insertion->Sorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array1[i] << " ";
}
cout << endl;
cout << "Bubble->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
bubbleSort(array2, sizeOfArray);
cout << "Bubble->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array2[i] << " ";
}
cout << endl;
cout << "Merge->Unsorted:\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl;
mergeSort(array3, 0, sizeOfArray-1, sizeOfArray);
cout << "Merge->Sorted:\t\t";
for(int i = 0; i < sizeOfArray; i++)
{
cout << array3[i] << " ";
}
cout << endl; */
return 0;
}
void insertionSort(int array[], int size)
{
for(int i = 1; i < size; i++)
{
int item = array[i], index = i;
while(index > 0 && array[index-1] > item)
{
array[index] = array[index-1];
index--;
}
array[index] = item;
}
}
void bubbleSort(int array[], int size)
{
bool sorted = false;
for(int i = 1; i < size && !sorted; i++)
{
sorted = true;
for(int i2 = 0; i2 < size-i; i2++)
{
int nextI = i2+1;
if(array[i2] > array[nextI])
{
swap(array[i2], array[nextI]);
sorted = false;
}
}
}
}
void merge(int array[], int first, int mid, int last, int size)
{
int tempArray[size];
int first1 = first, first2 = mid+1;
int last1 = mid, last2 = last;
int index = first1;
while(first1 <= last1 && first2 <= last2)
{
if(array[first1] < array[first2])
{
tempArray[index] = array[first1];
first1++;
}
else
{
tempArray[index] = array[first2];
first2++;
}
index++;
}
while(first1 <= last1)
{
tempArray[index] = array[first1];
first1++;
index++;
}
while(first2 <= last2)
{
tempArray[index] = array[first2];
first2++;
index++;
}
for(index = first; index <= last; index++)
{
array[index] = tempArray[index];
}
}
void mergeSort(int array[], int first, int last, int size)
{
if(first < last)
{
int mid = (first+last)/2;
mergeSort(array, first, mid, size);
mergeSort(array, mid+1, last, size);
merge(array, first, mid, last, size);
}
}
Any help is greatly appreciated. It might be a memory limitation on my system? I really don't know lol just a thought.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法在具有固定有限大小的堆栈上分配这么大的数组,请尝试:
You can't allocate such big arrays on the stack which has a fixed limited size, try:
正如其他人所指出的,您获得的 SEGV 是由于堆栈溢出造成的。它发生在你的 ubuntu 机器上而不是你学校的 redhat 机器上的原因可能是由于默认堆栈大小的差异。
您可以使用
ulimit -s
更改默认堆栈大小,在没有其他参数的情况下,它将打印当前堆栈大小(以千字节为单位)。例如,在我的机器上,打印8192
或 8 MB。我可以使用 ulimit -s 16384 将其提高到 16MB750000 个整数的数组将需要大约 3MB 的堆栈空间(每个整数 4 个字节),因此 4 个这样的数组(就像您所拥有的)将需要 12MB。 ..
As others have noted, the SEGV you're getting is due to overflowing the stack. The reason it happens on your ubuntu machine and not your school's redhat machine is likely due to differences in the default stack size.
You may be able to change your default stack size with
ulimit -s
which, with no additional arguments, will print your current stack size in kilobytes. For example, on my machine, that prints8192
or 8 megabytes. I can raise it to 16MB withulimit -s 16384
An array of 750000 ints will require about 3MB of stack space (4 bytes per int), so 4 such arrays (like you have) will require 12MB...
您在堆栈上分配了非常大的数组,并且堆栈溢出。如果您新建它们或将它们设为静态,它们将被分配在堆上,并且您不会失败。
You've allocated very large arrays on the stack and you're overflowing the stack. If you new them or make them static, they'll be allocated on the heap and you won't fail.
这是因为堆栈不是无限的资源。当您执行 int x[big_honkin_number] 时,它会尝试在堆栈上为该数组分配足够的空间。
您通常可以编译/链接代码来为您提供更多堆栈,但更好的解决方案是使用动态内存分配(即
new
)。It's because the stack is not an infinite resource. When you do
int x[big_honkin_number]
, it tries to allocate enough space on the stack for that array.You can usually compile/link your code to give you more stack but a better solution is to use dynamic memory allocation (i.e.,
new
).