c-glibc/realloc/无效指针
这是学校的一个项目。 我编写了一个名为file_to_array_d的函数,它将所有字符放在一个数组中,因此可以轻松地操作文件 该程序根据从用户处读取的内容对文件进行一些处理。目前,只有“V”和“R”选项可以工作V'打印出文件的内容 在主循环(main()函数中的循环)的第一次运行中,一切正常。 然而,在第二次运行时,我得到了glibc错误c-glibc/realloc/无效指针,c,pointers,gcc,glibc,realloc,C,Pointers,Gcc,Glibc,Realloc,这是学校的一个项目。 我编写了一个名为file_to_array_d的函数,它将所有字符放在一个数组中,因此可以轻松地操作文件 该程序根据从用户处读取的内容对文件进行一些处理。目前,只有“V”和“R”选项可以工作V'打印出文件的内容 在主循环(main()函数中的循环)的第一次运行中,一切正常。 然而,在第二次运行时,我得到了glibc错误 *** glibc detected *** ./a.out: realloc(): invalid pointer: 0x00007f01ad4397f8
*** glibc detected *** ./a.out: realloc(): invalid pointer: 0x00007f01ad4397f8 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f01ad0ffb96]
/lib/x86_64-linux-gnu/libc.so.6(realloc+0x2de)[0x7f01ad10495e]
./a.out[0x400ec9]
./a.out[0x400d3d]
./a.out[0x4009b1]
./a.out[0x40090c]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f01ad0a276d]
./a.out[0x400819]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:05 3543576 /home/hork/Dropbox/FIIT/PPR/projekt/a.out
00601000-00602000 r--p 00001000 08:05 3543576 /home/hork/Dropbox/FIIT/PPR/projekt/a.out
00602000-00603000 rw-p 00002000 08:05 3543576 /home/hork/Dropbox/FIIT/PPR/projekt/a.out
01318000-01339000 rw-p 00000000 00:00 0 [heap]
7f01ace6b000-7f01ace80000 r-xp 00000000 08:05 4722212 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01ace80000-7f01ad07f000 ---p 00015000 08:05 4722212 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01ad07f000-7f01ad080000 r--p 00014000 08:05 4722212 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01ad080000-7f01ad081000 rw-p 00015000 08:05 4722212 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f01ad081000-7f01ad236000 r-xp 00000000 08:05 4723475 /lib/x86_64-linux-gnu/libc-2.15.so
7f01ad236000-7f01ad435000 ---p 001b5000 08:05 4723475 /lib/x86_64-linux-gnu/libc-2.15.so
7f01ad435000-7f01ad439000 r--p 001b4000 08:05 4723475 /lib/x86_64-linux-gnu/libc-2.15.so
7f01ad439000-7f01ad43b000 rw-p 001b8000 08:05 4723475 /lib/x86_64-linux-gnu/libc-2.15.so
7f01ad43b000-7f01ad440000 rw-p 00000000 00:00 0
7f01ad440000-7f01ad462000 r-xp 00000000 08:05 4723489 /lib/x86_64-linux-gnu/ld-2.15.so
7f01ad63f000-7f01ad642000 rw-p 00000000 00:00 0
7f01ad65c000-7f01ad662000 rw-p 00000000 00:00 0
7f01ad662000-7f01ad663000 r--p 00022000 08:05 4723489 /lib/x86_64-linux-gnu/ld-2.15.so
7f01ad663000-7f01ad665000 rw-p 00023000 08:05 4723489 /lib/x86_64-linux-gnu/ld-2.15.so
7fffe5f17000-7fffe5f38000 rw-p 00000000 00:00 0 [stack]
7fffe5fff000-7fffe6000000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
我使用的是Ubuntu12.04,代码是用gcc编译的
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
这是完整的源代码,注意有些注释是用斯洛伐克语写的,但重要的是用英语写的
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <execinfo.h>
#define DEBUG
#define DETAILED_DEBUG
#define FILE_RELATIVE_PATH "ucet.txt"
void dump(char **p, int n);
char ** output_file();
char ** file_to_array_d(FILE * fd, long * rows);
void print_trace ();
void * xrealloc (void *ptr, size_t size);
void fatal(char *message);
void biggest_credit();
int main(int argc, char const *argv[])
{
char argument;
char ** p = NULL;
double * suma = NULL;
int open = 0;
while( (argument = getchar()) != 'K')
{
if (argument == 'V') // vypise na obrazovku obsah suboru
{
output_file();
open = 1;
}
else if (argument == 'n') // aktualizuje polia
{
//number_of_records = parse_file(cena, pocet_dni);
}
else if (argument == 'l') // najlacnejsia cena
{
//calculate_lowest_price(cena, pocet_dni, number_of_records);
}
else if (argument == 'h') // histogram
{
//histogram(cena, pocet_dni, number_of_records);
}
else if (argument == 'R') // najvyssia cena za posledny rok
{
if (open)
biggest_credit();
}
}
return 0;
}
char ** output_file()
{
FILE * f = fopen(FILE_RELATIVE_PATH, "r");
if (f == 0)
{
printf("Neotvoreny subor\n");
return 0;
}
long size = 0;
char ** p = NULL;
p = file_to_array_d(f, &size);
//dump(p, size);
printf("Size: %li\n", size );
int i, r = 0;
for (i = 0, r = 0; r < (int)size; i++, r++)
{
switch(i)
{
case 0:
printf("transakcia: %s\n", p[r]);
break;
case 1:
printf("kredit/debet: %s\n", p[r]);
break;
case 2:
printf("cislo uctu kam/odkial idu peniaze: %s\n", p[r]);
break;
case 3:
printf("suma: %s\n", p[r]);
break;
case 4:
printf("datum: %s\n", p[r]);
break;
case 5:
printf("\n");
i = -1;
break;
}
}
fclose(f);
return p;
}
void biggest_credit()
{
FILE * f = fopen(FILE_RELATIVE_PATH, "r");
if (f == 0)
{
return;
}
long size = 0;
char ** p = file_to_array_d(f, &size);
double max = 0;
int max_index;
int i;
// suma je 4. riadok, takze index 3 je prva suma
for (i = 3; i < size; i += 6)
{
if ( (double)atof(p[i]) > max && atoi(p[i-2]) == 1 )
{
max = (double)atof(p[i]);
max_index = i;
}
}
if (max)
{
printf("%s\n", p[max_index]);
}
}
/*
* Parameters:
* FILE * - pointer to the file handle
* long * - pointer to adress, where the number of rows is written
*
* Returs:
* char * - pointer to the start of array containing file
*
* Info:
*
*/
char ** file_to_array_d(FILE * fd, long * rows)
{
char c; // current char being read from file
int current_row = 0; // starting from first row
int current_pos = 0;
// whole file will be stored in p[][]
// allocate first row
#ifdef DEBUG
printf("calling (char**) malloc( %i );\n", 1 * (int)sizeof(char *) );
#endif
char **p = (char **) malloc(1 * sizeof(char *));
// until EOF is reached
for(current_pos = 0; ; current_pos++)
{
if (EOF == fscanf(fd, "%c", &c))
{
if (p[current_row] == 0)
{
// un-allocate space
p = (char**) xrealloc(p, ((current_row) * sizeof(char *)) );
}
break;
}
#ifdef DETAILED_DEBUG
printf("row: %2i pos: %2i\n",current_row, current_pos );
printf(" read char: '%c'\n\n", c);
#endif
// if char was read and it is not EOF
// allocate new space for char
#ifdef DEBUG
printf("calling (char*) xrealloc(p[%i ], %i);\n",current_row, current_pos+1);
#endif
p[current_row] = (char*) xrealloc(p[current_row], current_pos+1);
if (c != '\n')
{
// place read char there
p[current_row][current_pos] = c;
}
else // if new line
{
// place end of string there
p[current_row][current_pos] = '\0';
#ifdef DATAILED_DEBUG
printf("\tp[%i] = '%s' \n", current_row, p[current_row]);
#endif
// allocate and increment new row
current_row++;
#ifdef DEBUG
printf("calling (char**) xrealloc(p, %i);\n", (current_row + 1) * (int)sizeof(char *) );
#endif
p = (char**) xrealloc(p, ((current_row + 1) * sizeof(char *)) );
// set to -1, it will be zero in the next run of loop
current_pos = -1;
#ifdef DEBUG
int i;
for (i = 0; i < current_row; ++i)
{
printf("p[%2i] is at: %p size: %4li contains: %s\n",i, &p[i], sizeof(*p[i]) ,p[i] );
}
printf("\n");
#endif
}
}
// if EOF is in the position: '\n EOF'
// right way to end file
if( p[current_row] == 0)
{
#ifdef DEBUG
printf("%s\n", "Subor je spravne ukonceny n.1 :)");
#endif
*rows = current_row;
}
// if EOF is right behind line
// wrong way to end file
else
{
#ifdef DEBUG
printf("%s\n", "Subor je nespravne ukonceny !! (ale to nevadi)");
#endif
*rows = current_row+1;
}
#ifdef DEBUG
printf("Number of lines: %i\n", current_row);
#endif
return p;
}
void * xrealloc (void *ptr, size_t size)
{
register void *value = realloc (ptr, size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
}
void dump(char **p, int n)
{
int i, s;
printf("-- DUMP --\n");
for(i = 0; i < n; i++)
{
for(s = 0; p[i][s] != '\0'; s++)
printf("%c", p[i][s]);
printf("\n");
}
printf("-- END OF DUMP --\n");
}
// A function to display an error message and then exit
void fatal(char *message)
{
char error_message[100];
strcpy(error_message, "[!!] Fatal Error ");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
#包括
#包括
#包括
#包括
#定义调试
#定义详细的调试
#定义文件相对路径“ucet.txt”
无效转储(字符**p,整数n);
字符**输出_文件();
字符**文件到数组(文件*fd,长*行);
无效打印跟踪();
void*xrealloc(void*ptr,尺寸\u t尺寸);
无效致命(字符*消息);
作废最大的信用证;
int main(int argc,char const*argv[]
{
字符参数;
char**p=NULL;
double*suma=NULL;
int open=0;
而((argument=getchar())!='K')
{
if(参数=='V')//vypise na obrazovku obsah suboru
{
输出_文件();
开放=1;
}
else if(参数=='n')//aktualizuje polia
{
//记录数=解析文件(cena、pocet\U dni);
}
else if(参数=='l')//najlacnejsia cena
{
//计算最低价格(cena、pocet dni、记录数量);
}
else if(参数=='h')//直方图
{
//直方图(cena、pocet_dni、记录数量);
}
else if(参数=='R')//najvyssia cena za posledny rok
{
如果(打开)
最大信用;
}
}
返回0;
}
字符**输出_文件()
{
FILE*f=fopen(文件相对路径,“r”);
如果(f==0)
{
printf(“Neotvoreny subor\n”);
返回0;
}
长尺寸=0;
char**p=NULL;
p=文件到数组的大小(f和大小);
//转储(p,大小);
printf(“大小:%li\n”,大小);
int i,r=0;
对于(i=0,r=0;r<(int)大小;i++,r++)
{
开关(一)
{
案例0:
printf(“跨大西洋:%s\n”,p[r]);
打破
案例1:
printf(“kredit/debet:%s\n”,p[r]);
打破
案例2:
printf(“cislo uctu kam/odkial idu peniaze:%s\n”,p[r]);
打破
案例3:
printf(“suma:%s\n”,p[r]);
打破
案例4:
printf(“基准:%s\n”,p[r]);
打破
案例5:
printf(“\n”);
i=-1;
打破
}
}
fclose(f);
返回p;
}
作废信用证()
{
FILE*f=fopen(文件相对路径,“r”);
如果(f==0)
{
返回;
}
长尺寸=0;
char**p=文件到数组的大小(f和大小);
双最大值=0;
int max_指数;
int i;
//suma je 4.riadok,takze指数3 je prva suma
对于(i=3;imax和&atoi(p[i-2])==1)
{
max=(双)atof(p[i]);
最大指数=i;
}
}
如果(最大值)
{
printf(“%s\n”,p[max_index]);
}
}
/*
*参数:
*FILE*-指向文件句柄的指针
*long*-指向地址的指针,其中写入了行数
*
*报告:
*char*-指向包含文件的数组开头的指针
*
*信息:
*
*/
字符**文件到数组(文件*fd,长*行)
{
char c;//正在从文件中读取的当前字符
int current_row=0;//从第一行开始
int current_pos=0;
//整个文件将存储在p[][]
//分配第一行
#ifdef调试
printf(“调用(字符**)malloc(%i);\n”,1*(int)sizeof(字符*);
#恩迪夫
char**p=(char**)malloc(1*sizeof(char*));
//直到达到EOF
对于(当前位置=0;当前位置++)
{
如果(EOF==fscanf(fd、%c、&c))
{
if(p[当前_行]==0)
{
//取消分配空间
p=(char**)xrealloc(p,((当前行)*sizeof(char*));
}
打破
}
#ifdef详细调试
printf(“行:%2i位置:%2i\n”,当前行,当前位置);
printf(“读取字符:'%c'\n\n',c);
#恩迪夫
//如果读取了char,但它不是EOF
//为char分配新空间
#ifdef调试
printf(“调用(char*)xrealloc(p[%i],%i);\n”,当前行,当前位置+1);
#恩迪夫
p[当前行]=(字符*)xrealloc(p[当前行],当前位置+1);
如果(c!='\n')
{
//把读字符放在那里
p[当前行][当前位置]=c;
}
else//if新行
{
//把绳子的末端放在那个里
p[当前行][当前位置]='\0';
#ifdef数据文件调试
printf(“\tp[%i]=“%s”\n”,当前行,p[当前行];
#恩迪夫
//分配并增加新行
当前_行++;
#ifdef调试
printf(“调用(char**)xrealloc(p,%i);\n”,(当前行+1)*(int)sizeof(char*);
#恩迪夫
p=(char**)xrealloc(p,((当前行+1)*sizeof(char*));
//如果设置为-1,则在下一次循环运行中将为零
当前位置=-1;
#ifdef调试
int i;
对于(i=0;i<当前_行;++i)
{
printf(“p[%2i]位于:%p大小:%4li包含:%s\n”、i、&p[i]、sizeof(*p[i])、p[i]);
}
printf(“\n”);
#恩迪夫
}