![](/img/trans.png)
[英]I'm trying to create code for an assignment and I keep getting a memory error that I can't figure out
[英]I'm trying to read a file into an arrray and keep getting this error
请帮帮我。 我正在尝试将文件读入数组并返回文件中的最大数量。 我不断收到分段错误错误,我知道这可能是因为我处理数组的方式,但我可以找到确切的问题,问题出在函数 ReadFileInotArray 中。 功能结束后,我收到如下所示的错误。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
void PrintArray(int ArrayToPrint[], int SizeAP)
{
int i;
printf("{");
for (i=0; i< SizeAP; i++)
printf("%d,", ArrayToPrint[i]);
printf("\b}\n");
}
void swap(int *SwapA, int *SwapB)
{
int temp = *SwapA;
*SwapA = *SwapB;
*SwapB = temp;
}
int partition (int A[], int low, int high)
{
int i, j = 0;
#if QSM
int middle = (high+low)/2;
swap(&A[middle], &A[high]);
#elif QSRND
int random = (rand() % (high-low+1)) + low;
swap(&A[random], &A[high]);
#endif
int pivot = A[high];
i= (low - 1);
for (j = low; j < high; j++)
{
if (A[j] < pivot)
{
i++;
swap(&A[i], &A[j]);
}
}
swap(&A[i+ 1], &A[high]);
return (i+ 1);
}
void QuickSort(int A[], int low, int high)
{
if (low < high)
{
int ndx= partition(A, low, high);
QuickSort(A, low, ndx-1);
QuickSort(A, ndx+ 1, high);
}
}
void ReadFileIntoArray(int argc, char *argv[], int A[],int size)
{
char line[200];
int j=0;
if(argv[1]==NULL)
{
printf("File must be provided on command line...exiting\n");
exit(0);
}
FILE *fp = fopen(argv[1],"r");
if(fp==NULL)
{
printf("Exiting: no such file in directory\n");
exit(0);
}
while(fgets(line,200,fp)!=NULL)
{
size++;
}
fseek(fp,0,0);
A=malloc(sizeof(int)*size);
while(j<size && fgets(line,200,fp)!=NULL )
{
A[j]=atoi(line);
printf("%d\n",A[j]);
j++;
}
printf("%d",size);
PrintArray(A,size);
}
int main(int argc ,char *argv[])
{
clock_t start,end;
int i,control,count=0,size;
int *array;
if(argv[2]==NULL)
{
printf("Number of runs not specified on the command line...defaluting to 10");
control=10;
}
else
control=atoi(argv[2]);
for(i=1; i<=control; i++)
{
ReadFileIntoArray(argc,argv,array,size);
#ifdef PRINTARRAY
PrintArray(array, size);
#endif
printf("ok");
start = clock();
QuickSort(array, 0, size-1);
end = clock();
printf("\nRun %d complete: %ld tics",i ,end-start);
count+=end-start;
#ifdef PRINTARRAY
PrintArray(array,size);
#endif
free(array);
}
printf("\nThe average run time for %s runs is %d\n",argv[2],count/i);
printf("\nProcessed %d records\n",size);
return 0;
}
这是我在 VM 中运行时得到的输出。
student@maverick:/media/sf_VM_folder/CSE_3318_code@ gcc Code3_1001472179.c
student@maverick:/media/sf_VM_folder/CSE_3318_code$ ./a.out TestFile.txt 5
472179
93424
313939
263358
154966
93659
221593
284590
439245
154572
10{472179,93424,313939,263358,154966,93659,221593,284590,439245,154572}
ok
munmap_chunk(): invalid pointer
Aborted (core dumped)
你还没有粘贴完整的代码,但我假设这是发生了什么。 您刚刚直接从 main 传入array
。 您在ReadFileIntoArray()
对A
修改不会反映在main()
并且由于您甚至还没有初始化array
,因此在main()
访问它是 Undefined Bevahoiour 并且可能会崩溃。 您需要传入&array
并在ReadFileIntoArray()
接受一个int** A
PS 您如何使用size
也存在错误,因为在ReadFileIntoArray()
对size
所做的更改不会反映在main()
,您需要传入&size
并在ReadFileIntoArray()
接受int* size
。
编辑:你正在做我假设的事情。 当你调用QuickSort(array, 0, size-1);
, array
没有任何值并指向某个不确定的值(或可能为NULL
)。
我将分享一个小例子来解释什么是错的。
void readIntoArray(int array[], int size) {
printf("%s: Initial values %p - %d\n", __FUNCTION__, array, size);
//code to read the vale of size from file
size = 5;
array = malloc(sizeof(int) * size);
printf("%s: End values %p - %d\n", __FUNCTION__, array, size);
}
int main() {
int * array;
int size;
printf("%s: Initial values %p - %d\n", __FUNCTION__, array, size);
readIntoArray(array, size);
printf("%s: End values %p - %d\n", __FUNCTION__, array, size);
}
在某些编译器上,这可能会产生如下输出:
main: Initial values (nil) - 32766
readIntoArray: Initial values (nil) - 32766
readIntoArray: End values 0x2435020 - 5
main: End values (nil) - 32766
注意这里发生了什么。 即使您将readIntoArray()
array
更改为指向某些malloc
的内存,这也不会反映在main()
。 size
也是如此。 这是 C 中一个非常基本的概念,其中参数按值传递给函数并被复制。 要在readIntoArray
完成更改,您需要通过使用&
运算符传递它们的地址来通过引用传递它们,如下所示:
void readIntoArray(int **array, int* size) {
printf("%s: Initial values %p - %d\n", __FUNCTION__, *array, *size);
//code to read the vale of size from file
*size = 5;
*array = malloc(sizeof(int) * *size);
printf("%s: End values %p - %d\n", __FUNCTION__, *array, *size);
}
int main() {
int * array;
int size;
printf("%s: Initial values %p - %d\n", __FUNCTION__, array, size);
readIntoArray(&array, &size);
printf("%s: End values %p - %d\n", __FUNCTION__, array, size);
}
产生输出:
main: Initial values (nil) - 32766
readIntoArray: Initial values (nil) - 32766
readIntoArray: End values 0x1217020 - 5
main: End values 0x1217020 - 5
PS 这些只是示例,在处理指针时,最好先检查它们是否为NULL
然后再像*array
、 *size
那样取消引用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.