- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码。编辑:调用者包含在底部。
该函数读取数据文件,确定有多少行和列,然后将数据存储到 data_array 中。
int getdata(double* *data_array, int* *data_labels, int argc, char *argv[], int *items, int *attr)
{
// filename variables
char *filename; // pointer to a string that will contain the name of the training data file.
char *result = NULL; // used with strtok() to extract each feature value given a line of delimited features.
FILE *fp; // pointer to FILE, we can use this with fgets to access each line
char line[10000]; // array of 1000 chars for storing the raw data for one observation
char delims[4]; // an array of common delimiters for data files
delims[0]='\t';
delims[1]=' ';
delims[2]=',';
delims[3]='\0';
int i, j;
// check that we have the correct number of command line arguments
if (argc < 2)
{
printf("2usage: progname filename\n");
return -1;
}
if (argc < 4)
{
printf("3usage: progname filename num_labels k(nn)\n");
return -1;
}
if (atoi(argv[2]) < 1)
{
printf("num_labels must be a positive integer.\n");
return -1;
}
if (atof(argv[2]) - atoi(argv[2]) > 0)
{
printf("num_labels must be an integer.\n");
return -1;
}
if (atoi(argv[3]) < 1)
{
printf("k must be a positive integer.\n");
return -1;
}
if (atof(argv[3]) - atoi(argv[3]) > 0)
{
printf("k must be an integer.\n");
return -1;
}
// try to open the file
filename = argv[1];
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("could not open file: %s\n", filename);
printf("note: the filename should be the second command line argument, after the .exe file");
return -1;
}
printf("reading file: %s\n", filename);
// GET NUMBER OF ROWS AND COLUMNS OF INPUT FILE
fgets(line, 10000, fp);
result = strtok(line,delims);
while(result != NULL)
{
(*items)++;
result = strtok(NULL, delims);
}
(*attr)++;
while(fgets(line,sizeof(line),fp) != NULL)
(*attr)++;
printf("num items: %d\n", *items);
printf("num attributes: %d\n", *attr);
// NOW THAT YOU HAVE FILE SIZE, ALLOCATE MEMORY TO STORE DATA
*data_array = malloc(*items* *attr*sizeof(double));
//*data_labels = malloc(*items*sizeof(int));
printf("data array size = %d\n\n",*items* *attr);
i=0;
j=0;
fclose(fp);
fp = fopen(filename, "r");
// SAVE DATA INTO DATA_ARRAY
while (fgets(line, 10000, fp) != NULL)
{
// we break line into tokens using our delimeters list declared at the beginning of the function
result = strtok(line,delims);
//printf("%d\n",i);
while(result != NULL) {
(*data_array)[i++] = atof(result);
//printf("%f\n",(*data_array)[i-1]);
result = strtok(NULL, delims);
}
//(*data_labels)[j++] = (int)((*data_array)[--i]);
}
// close the file
fclose(fp);
return 0;
}
int main(int argc, char *argv[])
{
double *data_array = NULL;
int *data_labels = NULL;
int items = 0;
int attr = 0;
if (getdata(&data_array, &data_labels, argc, argv, &items, &attr) == -1)
{
printf("reading input data failed");
return 0;
}
printf("PROCESS COMPLETED WITHOUT SEGFAULT");
#ifdef CROSSVAL
// v-fold cross-validation
if (crossval(data_array, atoi(argv[2]), items, attr, closest_center, centers) == -1)
{
printf("k-means operation failed");
return 0;
}
#else
// coords holds coordinates of one new point
int i;
double coords[attr];
for(i = 0; i<attr;i++)
{
coords[i] = atof(argv[i+4]);
}
/*
// calling knn function
if (knn(data_array, data_labels, atoi(argv[2]), atoi(argv[3]), coords, items, attr) == -1) // use a heap!
{
printf("knn operation failed");
return 0;
}
*/
#endif
free(data_array);
return 0;
}
奇怪的是这个函数在出现段错误之前输出正确。给定一个包含 2 列和 272 行的数据文件,我得到“num items: 2, num attribute: 272, data array size = 544”
。但是,如果我注释掉 "printf("num attribute: ...
"行之后和最后一个 fclose(fp)
行之前的所有内容,它仍然会出现段错误。它很好地完成了打印语句,然后出现了段错误,即使它后面没有重要的代码。太困惑了。
最佳答案
在 for
循环中调用 getdata()
后,程序出现段错误。
您不会检查程序是否有足够的参数,也不会读取 attr + 4
参数,无论它们是否存在。
这也是当您删除 (*attr)++
时不会出现段错误的原因。因为这样,attr
仍为 0
并且您不会尝试读取不存在的参数。
关于c - 输入文件时出现 SegFault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971220/
所以我需要一个简单的分配器来分配(有时使用清零)并随后从映射内存池中释放 4K block 。然而,在实现这个之后,在测试时我发现在释放一两个 block 之后,如果我尝试分配一个 block ,程序
我的任务是用 C 编写一个程序。该程序应该能够检查参数并创建与我提供的参数一样大的数组。我必须用随机数填充数组。到目前为止工作正常。稍后我的任务是使用指针对数组进行排序。第一件事是我不太明白指针是如何
我对 C 很陌生(仍然)所以如果我误解了一些基本的东西,请耐心等待 我有一个简单的程序,它应该将文件作为字符串读取,然后将该字符串拆分成行 - 将结果存储到 n 个字符串数组中。但是,当我运行以下代码
我在工作中使用的应用程序之一遇到了一个奇怪且烦人的问题。该应用程序是用 C++ 编写的,当应用程序终止(主函数返回或调用 exit)时,它会因段错误而崩溃。段错误似乎是由 basic_string 类
我使用 python swig 包装的 C++ 库。在它的 __init__.py 文件中,它 sets在导入包含实现代码的共享对象文件之前,使用 dlopen 标志 RTLD_GLOBAL。 这会导
我在这里遇到了段错误。我很困惑。请帮帮我。 f1 和 y 都是结构体节点的指针。我想把 y 的左转 f1 右转。 #include #include struct node{
我有一个在公共(public)结构中声明的数组,如下所示: uint16_t *registers; 在一个函数中,我正在检索一个字符字符串(存储在缓冲区中,请参阅下面的代码),其中包含以逗号分隔的数
我正在用 C 实现二叉搜索树。下面的代码工作正常,只是当我尝试从树中删除子树时得到 SEGFAULT: 源代码: #include #include struct node { int dat
struct vehicle *add_vehicle(struct vehicle *v){ struct vehicle *newcar = (struct vehicle*)malloc
我正在使用链接列表实现符号表,代码工作正常,但代码中存在内存泄漏, 我有以下结构 struct node { char* pcKey; void* pvValue; struct node
我正在尝试将字符串复制到数组并打印它。它适用于第一个 for 循环,但第二次出现 seg 错误。 main (int argc, char *argv[]){ int argcIndex; cha
自从我用 C 编写代码已经一年了,但我不明白为什么会出现段错误 // Assume all imports are made int printAgain(double** array, int si
这是我的代码。编辑:调用者包含在底部。 该函数读取数据文件,确定有多少行和列,然后将数据存储到 data_array 中。 int getdata(double* *data_array, int*
我认为有两组代码是等效的,但一组会导致段错误,而另一组则不会。我真的很困惑为什么会这样...... 我想创建一个查找函数 此代码确实有效: MyPair *> dummy(x, NULL);
希望有人能提供帮助。我可以毫无错误地编译,我没有发现任何语法错误,但是当我运行它时,它崩溃了。在启动时调试段错误。全面披露,这是作业。我不是要找人来编写这个代码,只是看看我的问题和我现有的代码,也许会
我正在尝试在OpenMP中并行化相当大的for-loop。大约有20%的时间运行正常,但其余时间会因各种段错误而崩溃,例如: *** glibc detected *** ./execute: dou
我有一个模板类 ISingleton class ISingleton { public: static T* getInstance() { lock_guard g
我正在为使用 LibSVM 的 Android 构建 NDK 应用程序。我在 XCode 中为我的 mac 构建了一个等价物(都是 C++) 我发现 Mac 可以高速准确地处理我给它的非常大的特征向量
我在 ARM linux 平台上有一个由简单代码引起的非常奇怪的崩溃。问题是它很少重现(一天一次),另一个问题是它在实际上无法重现的地方崩溃。 让我们从 C++ 代码开始。线程函数执行此操作:
我有这段代码 int main() { int *b = new int(8); cout<<" &b = "<
我是一名优秀的程序员,十分优秀!