我遇到了一个奇怪的错误。当我编译我的代码时,它会给我以下消息:分段错误11,尽管大量的内存分配
%i??
[R?
R?
?
Desktop/prog2
Terminal
51/sls2t0f16cd4dzl3640n4p2w0000gn/T/
private/tmp/com.apple.launchd.AJrzeyltFv/Render
ION=343.6
ER=Frank
Listeners
in:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin
ank
E=/Users/Frank
nk/Desktop/prog2
Segmentation fault: 11
logout
我知道这是由于内存问题。然而,我给字典2与原始字典一样的内存,所以为什么它不能编译整个列表?
这里是我的代码(我知道这是在长边一点点,但我觉得这是所有必要的传达是怎么回事):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
struct entry
{
char word[15];
char definition[50];
};
void dictionarySort(struct entry dictionary[]) {
int i, j, k, word1, word2, dict2Length = 1;
bool bnf = false;
struct entry dictionary2[100] = {{}};
for (i = 0; i <= strlen(&dictionary->word[0]); i++) {
strcpy(&dictionary2[0].word[i], &dictionary[0].word[i]);
}
i = 0;
word1 = 1;
word2 = 0;
while (isalpha(dictionary[word1].word[0])) {
while (i <= strlen(&dictionary->word[word1])) {
//printf("%c", dictionary[word1].word[i]);
if (dictionary[word1].word[i] == dictionary2[word2].word[i]) {
i++;
bnf = false;
}
else if (dictionary[word1].word[i] < dictionary[word2].word[i]) {
//insert section to prevent back-and-forth cycling
if (bnf == false) {
word2--;
bnf = true;
}
else { //(dictionary[word1].word[i] < dictionary[word2].word[i])
//open up new index by moving everything above up one, insert at word
for (j = dict2Length; j > word2; j--) {
//word
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].word[k], &dictionary2[j].word[k]);
}
//definition
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].definition[k], &dictionary2[j].definition[k]);
}
}
//for (k = 0; k < strlen(&dictionary1->word[word1]))
for (k = 0; k < strlen(dictionary[word1].word); k++) {
strcpy(&dictionary2[word2].word[k], &dictionary[word1].word[k]);
}
for (k = 0; k < strlen(dictionary[word1].definition); k++) {
strcpy(&dictionary2[word2].definition[k], &dictionary[word1].definition[k]);
}
dict2Length++;
break;
}
}
else {
//insert section to prevent back-and-forth cycling
if (bnf == false) {
word2++;
bnf = true;
}
else { //(dictionary[word1].word[i] < dictionary[word2].word[i])
//open up new index by moving everything above up one, insert at word
for (j = dict2Length; j > word2; j--) {
//word
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].word[k], &dictionary2[j].word[k]);
}
//definition
for (k = 0; k <= strlen(&dictionary2->word[0]); k++) {
strcpy(&dictionary2[j+1].definition[k], &dictionary2[j].definition[k]);
}
}
for (k = 0; k < strlen(dictionary[word1].word); k++) {
strcpy(&dictionary2[word2].word[k], &dictionary[word1].word[k]);
}
for (k = 0; k < strlen(dictionary[word1].definition); k++) {
strcpy(&dictionary2[word2].definition[k], &dictionary[word1].definition[k]);
}
dict2Length++;
break;
}
}
}
word1++;
}
word2 = 0;
for (i = 0; i < dict2Length; i++) {
printf ("%s\n", dictionary2[i].word);
}
}
int main (void) {
struct entry dictionary[100] =
{{"aerie", "a high nest"},
{"abyss", "a bottomless pit"},
{"ahoy", "a nautical call of greeting"},
{"addle", "to become confused"},
{"aardvark", "a burrowing African mammal"},
{"agar", "a jelly made of seaweed"},
{"acumen", "mentally sharp; keen"},
{"aigrette", "an ornamental cluster of feathers"},
{"affix", "to attach"},
{"ajar", "partially opened"}};
dictionarySort(dictionary);
}
分割故障是内存问题,但它不是以*耗尽*的内存问题。您的程序尝试访问未分配给它的内存时出现问题。通常这是由于坏的数组索引,未初始化的或悬挂的指针或类似的。 –
你有权访问某种调试器吗?程序冻结的地方应该告诉你问题的确切位置在哪里。 –
你可以使用'qsort'。 – BLUEPIXY