问题描述
我在漫长的code道歉。我有一个简单的问题,但我想我有我的code所以这将是不清楚的地方,我来自哪里。
我得到一个realloc的腐败。我认为,腐败是因为我没有正确地释放。在现实中,我不知道是什么的glibc甚至有传言称或手段。任何一个简单能解释给我吗?
同样遗憾的漫长code。
的#includedraw2.h
#包括draw2a.h
#包括draw2b.h为const char Exec_c [] =Java的罐子Sketchpad.jar无效parseFile(FILE * FP,FILE *草绘){
焦线[MAX_WORD] = {NULL};
字符字[MAX_WORD] = {NULL};
焦炭figureName [MAX_WORD] = {NULL};
结构图* pointsAndname;
长整型countNumberoffigures = 0;
printOutput();
长整型温度= 10;
pointsAndname =的malloc(临时*的sizeof(结构图));
断言(pointsAndname!= NULL);
而(与fgets(行,MAX_WORD! - 1,FP)= NULL){
INT nuRead = sscanf的(行,%S字);
断言(pointsAndname!= NULL);
如果(nuRead大于0){
如果(STRNCMP(字,图,MAX_WORD)== 0){
countNumberoffigures + = 1;
如果(温度== countNumberoffigures - 1){
TEMP =温度* 2;
的printf(pointsAndname是%X \\ n,pointsAndname);
pointsAndname =的realloc(安培; pointsAndname,温度*的sizeof(结构图));
的printf(pointsAndname是%×realloc的\\ n后,pointsAndname);
}
断言(pointsAndname!= NULL);
figureFunction(FP,线条,文字,figureName,countNumberoffigures,pointsAndname + countNumberoffigures);
}
如果(STRNCMP(字,printFigure,MAX_WORD)== 0){// 4)读取命令printFigure,图中的名称
如果(countNumberoffigures == 1){
printFigure(FP,线,countNumberoffigures,pointsAndname + countNumberoffigures);
}其他{
printFigure(FP,线,countNumberoffigures,pointsAndname + countNumberoffigures - 1);
}
}
如果(STRNCMP(字,drawFigure,MAX_WORD)== 0){// 5)读取的命令drawFigure和该图的名称
如果(countNumberoffigures == 1){
drawFigure(草绘,线,countNumberoffigures,pointsAndname + countNumberoffigures);
}其他{
drawFigure(草绘,线,countNumberoffigures,pointsAndname + countNumberoffigures - 1);
}
}
如果(STRNCMP(字,翻译,MAX_WORD)== 0){// 6)读取命令翻译
如果(countNumberoffigures == 1){
翻译(行,草绘,countNumberoffigures,pointsAndname + countNumberoffigures);
}其他{
翻译(行,草绘,countNumberoffigures,pointsAndname + countNumberoffigures - 1);
}
}
如果(STRNCMP(字,子,MAX_WORD)== 0){// 7)读取命令孩子和人物的名字
子(行,字,figureName,草绘);
}
如果(STRNCMP(文字,#,MAX_WORD)== 0){// 8)读的是整条生产线,直到\\ n
的printf(线);
//输出(ANI PO \\ n);
}
如果(STRNCMP(字,结束,MAX_WORD)== 0){
fprintf中(草绘,结束\\ n);
//输出(ANI PO \\ n);
}
如果(STRNCMP(字,旋转,MAX_WORD)== 0){
如果(countNumberoffigures == 1){
旋转(线,草绘,countNumberoffigures,pointsAndname + countNumberoffigures);
}其他{
旋转(线,草绘,countNumberoffigures,pointsAndname + countNumberoffigures - 1);
}
}
}
}
免费(pointsAndname->顶点);
免费(pointsAndname);
}无效processArgument(char参数[]){
FILE *草绘;
FILE * FP;
FP = FOPEN(参数,R);
草绘=的popen(Exec_c,W);
如果(FP == NULL){
的printf(无法打开管道到%s \\ n,参数);
出口(EXIT_FAILURE);
}
如果(草绘== NULL){
的printf(无法打开管道到%s \\ n,参数);
出口(EXIT_FAILURE);
}其他{
parseFile(FP,草绘);
如果(FCLOSE(FP)== EOF){
的printf(无法关闭管道到%s \\ n,参数);
出口(EXIT_FAILURE);
}
如果(pclose函数(草绘)== -1){//如果(pclose函数(草绘)== -1){
fprintf中(标准错误,draw_line错误:无法关闭管道到%s \\ n,Exec_c); // fprintf中(标准错误,,E);
出口(EXIT_FAILURE);
}
}
}INT主(INT ARGC,CHAR *的argv []){
INT I;
如果(的argc 2){
的printf(%S \\ n,评论(0));
出口(EXIT_FAILURE);
}其他{
对于(i = 1; I< ARGC,我++){
processArgument(的argv [I]);
}
}
返回0;
}
的#includedraw2.h
#包括draw2a.h无效printOutput(){
的printf(./ DRAW2开始:); fflush(标准输出);
系统(日期\\ n);
}
/ *送什么都没有孩子画板(在特定行)后* /
无效子(焦线[],CHAR字[],CHAR nameFigure [],FILE *草绘){
sscanf的(行,%s%S字,nameFigure);
fprintf中(草绘%s \\ n,&安培;线[6]);
}/ *我从图线到底线图阅读构建结构。* /
无效figureFunction(FILE * FP,焦线[],CHAR字[],CHAR figureName [],长整型countNumberoffigures,结构图* figureHere){
双startx的,starty;
长整型温度= 10;
figureHere->顶点=的malloc(临时*的sizeof(结构pointxy));
sscanf的(行,%s%S%LF%LF%* S字,figureHere-> figureName,和放大器;运行startx,&安培; starty);
的printf(这是figureHere->在figureFunction内顶点:%S \\ n;顶点,figureHere-&GT);
(*(figureHere->顶点))X = startx的;
(*(figureHere->顶点))。Y = starty;
与fgets(行,MAX_WORD - 1,FP);
INT nuRead = sscanf的(行,%S字);
长INT I = 1;
而(STRNCMP(字,结束,MAX_WORD)!= 0){
如果(STRNCMP(字,#,MAX_WORD)== 0){
的printf(%S,行);
}
如果(STRNCMP(字,画,MAX_WORD)== 0){
//输出(ANI勒珀MAGIA ????? \\ n \\ n);
sscanf的(行,%s%LF%LF,字和放大器;运行startx,&安培; starty);
//的printf(这是我运行startx和starty:%LF%LF \\ n \\ n,运行startx,starty);
figureHere->顶点[I]的.x = figureHere->顶点[I-1] .X + startx的;
figureHere->顶点[I] .Y = figureHere->顶点[I-1] .Y + starty;
我+ = 1;
}
与fgets(行,MAX_WORD - 1,FP);
nuRead = sscanf的(行,%S字);
如果(临时== figureHere-> countPoints){
TEMP =温度* 2;
figureHere->顶点=的realloc(figureHere->顶点,温度*的sizeof(结构pointxy));
}
}
figureHere-> countPoints = I;
}
的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&math.h中GT;
#包括LT&;&ASSERT.H GT;无效parseFile(FILE * FP,FILE *草绘);
无效processArgument(char参数[]);
无效printOutput();#定义MAX_WORD 256
#定义initial_size 17EXTERN为const char参数[];/ *对于非C99库函数的原型* /
FILE * popen这(为const char *,为const char *);
INT函数,pclose(FILE *);
结构pointxy {
双X;
双Y;
};结构图{
焦炭figureName [MAX_WORD]
结构pointxy *顶点;
长整型countPoints;
};
*** glibc的检测*** ./draw2:realloc的():无效的指针:0xbf9c3a28 ***
=======回溯:=========
/lib/libc.so.6(realloc+0x3a8)[0x400cb548]
/lib/libc.so.6(realloc+0x3c)[0x400cb1dc]
./draw2[0x8048ab8]
./draw2[0x8048ec1]
./draw2[0x8048f84]
/lib/libc.so.6(__libc_start_main+0xe0)[0x40072390]
./draw2[0x8048891]
=======存储器映射:========
08048000-0804a000 R-XP 00000000 00:16 160237 /家庭/ dsk07 / ugrad / pkardash / C201 / assignment2 / DRAW2
0804a000-0804b000 RW-P 00002000 00:16 160237 /家庭/ dsk07 / ugrad / pkardash / C201 / assignment2 / DRAW2
0804b000-0804c000 RW-P 00000000 00:00 0
09283000-092a4000 RW-P 00000000 00:00 0堆]
40000000-4001c000 R-XP 00000000 08:01 94922 /lib/ld-2.7.so
4001c000-4001d000 - [R - P 0001b000 08:01 94922 /lib/ld-2.7.so
4001d000-4001e000 RW-P 0001c000 08:01 94922 /lib/ld-2.7.so
4001e000-40022000 RW-P 00000000 00:00 0
40022000-4002c000 R-XP 00000000 08:07 150880 /usr/lib/libgcc_s.so.1
4002c000-4002d000 RW-P 00009000 08:07 150880 /usr/lib/libgcc_s.so.1
40036000-4005a000 R-XP 00000000 08:01 94889 /lib/libm-2.7.so
4005a000-4005b000 - [R - P 00023000 08:01 94889 /lib/libm-2.7.so
4005b000-4005c000 RW-P 00024000 08:01 94889 /lib/libm-2.7.so
4005c000-401a1000 R-XP 00000000 08:01 94899 /lib/libc-2.7.so
401a1000-401a2000 --- p 00145000 08:01 94899 /lib/libc-2.7.so
401a2000-401a4000 - [R - P 00145000 08:01 94899 /lib/libc-2.7.so
401a4000-401a5000 RW-P 00147000 08:01 94899 /lib/libc-2.7.so
401a5000-401a9000 RW-P 00000000 00:00 0
bf9b0000-RW bf9c5000-P 00000000 00:00 0 [堆栈]
ffffe000-fffff000 R-XP 00000000 00:00 0 VDSO]
使:*** [运行]中止
glibc的是告诉你你传递在不能一直通过malloc / realloc的返回地址。这是因为,您将在 pointsAndName
堆栈变量的地址。您需要在价值传递,这是您从的malloc
收什么。此外,当你调用的realloc
,你应该使用一个临时变量。这样一来,如果的realloc
失败,你仍然可以自由的原始值。
结构图* tempPtr = realloc的(pointsAndname,温度*的sizeof(结构图));
如果(tempPtr == NULL)
{
//处理分配错误...
免费(pointsAndname);
}
pointsAndname = tempPtr;
I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from.I get a realloc corruption. I think the corruption is because I am not freeing correctly. In reality I am not sure what glibc even says or means. Can any one briefly explain that to me?Again sorry for the lengthy code.
#include "draw2.h"
#include "draw2a.h"
#include "draw2b.h"
const char Exec_c[] = "java -jar Sketchpad.jar";
void parseFile(FILE * fp, FILE *sketcher){
char line [MAX_WORD] = {"NULL"};
char word [MAX_WORD] = {"NULL"};
char figureName [MAX_WORD] = {"NULL"};
struct figure *pointsAndname;
long int countNumberoffigures = 0;
printOutput();
long int temp = 10;
pointsAndname = malloc(temp * sizeof(struct figure));
assert(pointsAndname != NULL);
while ( fgets(line, MAX_WORD - 1, fp) != NULL ){
int nuRead = sscanf(line, "%s", word);
assert(pointsAndname != NULL);
if ( nuRead > 0 ){
if(strncmp(word, "Figure", MAX_WORD)==0){
countNumberoffigures += 1;
if (temp == countNumberoffigures - 1){
temp = temp * 2;
printf("pointsAndname is %x\n", pointsAndname);
pointsAndname = realloc(&pointsAndname, temp * sizeof(struct figure));
printf("pointsAndname is %x after realloc\n", pointsAndname);
}
assert(pointsAndname != NULL);
figureFunction(fp,line, word, figureName, countNumberoffigures, pointsAndname + countNumberoffigures);
}
if(strncmp(word, "printFigure", MAX_WORD)==0){ //4)read the command printFigure, name of the figure
if (countNumberoffigures == 1){
printFigure(fp, line, countNumberoffigures, pointsAndname + countNumberoffigures);
}else{
printFigure(fp, line, countNumberoffigures, pointsAndname + countNumberoffigures - 1);
}
}
if(strncmp(word, "drawFigure", MAX_WORD)==0){ //5)read the command drawFigure and the name of the figure
if (countNumberoffigures == 1){
drawFigure(sketcher, line, countNumberoffigures, pointsAndname + countNumberoffigures);
}else{
drawFigure(sketcher, line, countNumberoffigures, pointsAndname + countNumberoffigures - 1);
}
}
if(strncmp(word, "translate", MAX_WORD)==0){ //6)read the command translate
if (countNumberoffigures == 1){
translate(line, sketcher, countNumberoffigures, pointsAndname + countNumberoffigures);
}else{
translate(line, sketcher, countNumberoffigures, pointsAndname + countNumberoffigures - 1);
}
}
if(strncmp(word, "child", MAX_WORD)==0){ //7)reads command child and the name of the figure
child(line, word, figureName, sketcher);
}
if(strncmp(word, "#", MAX_WORD)==0){ //8)reads the whole line until the \n
printf(line);
//printf("ani po\n");
}
if(strncmp(word, "end", MAX_WORD)==0){
fprintf (sketcher, "end\n");
//printf("ani po\n");
}
if(strncmp(word, "rotate", MAX_WORD)==0){
if (countNumberoffigures == 1){
rotate(line, sketcher, countNumberoffigures, pointsAndname + countNumberoffigures);
}else{
rotate(line, sketcher, countNumberoffigures, pointsAndname + countNumberoffigures - 1);
}
}
}
}
free(pointsAndname->vertices);
free(pointsAndname);
}
void processArgument(char argument[]){
FILE *sketcher;
FILE *fp;
fp = fopen (argument, "r");
sketcher = popen(Exec_c, "w");
if (fp == NULL){
printf ("Could not open pipe to %s\n", argument);
exit (EXIT_FAILURE);
}
if (sketcher == NULL){
printf ("Could not open pipe to %s\n", argument);
exit (EXIT_FAILURE);
}else{
parseFile(fp, sketcher);
if(fclose(fp)==EOF){
printf("couldn't close pipe to %s.\n", argument);
exit(EXIT_FAILURE);
}
if (pclose(sketcher) == -1){ //if (pclose(sketcher) == -1){
fprintf(stderr, "draw_line error: couldn't close pipe to %s.\n", Exec_c);//fprintf(stderr,"",E);
exit(EXIT_FAILURE);
}
}
}
int main (int argc, char *argv[]){
int i;
if ( argc < 2 ){
printf ("%s\n", "0 comment(s)");
exit(EXIT_FAILURE);
}else{
for (i = 1; i < argc; i++){
processArgument(argv[i]);
}
}
return 0;
}
#include "draw2.h"
#include "draw2a.h"
void printOutput(){
printf("./draw2 started on:");
fflush(stdout);
system("date\n");
}
/*send what ever there is after the child to sketchpad(in that specific line)*/
void child (char line[], char word[], char nameFigure[], FILE * sketcher){
sscanf(line, "%s%s", word, nameFigure);
fprintf (sketcher, "%s\n", &line[6]);
}
/*I construct the struct by reading from the Figure line to the end figure line.*/
void figureFunction (FILE * fp, char line[], char word[], char figureName[], long int countNumberoffigures, struct figure *figureHere){
double startx, starty;
long int temp = 10;
figureHere->vertices = malloc(temp * sizeof(struct pointxy));
sscanf(line, "%s%s%lf%lf%*s", word, figureHere->figureName, &startx, &starty);
printf("This is the figureHere->vertices inside the figureFunction: %s\n", figureHere->vertices);
(*(figureHere->vertices)).x = startx;
(*(figureHere->vertices)).y = starty;
fgets(line, MAX_WORD - 1, fp);
int nuRead = sscanf(line, "%s", word);
long int i = 1;
while (strncmp(word, "End", MAX_WORD)!=0){
if (strncmp(word, "#", MAX_WORD) == 0){
printf("%s",line);
}
if (strncmp(word, "draw", MAX_WORD) == 0){
//printf("lepo ani magia?????\n\n");
sscanf (line, "%s%lf%lf", word, &startx, &starty);
//printf("this is my startx and starty: %lf %lf\n\n",startx, starty);
figureHere->vertices[i].x = figureHere->vertices[i-1].x + startx;
figureHere->vertices[i].y = figureHere->vertices[i-1].y + starty;
i += 1;
}
fgets(line, MAX_WORD - 1, fp);
nuRead = sscanf(line, "%s", word);
if (temp == figureHere->countPoints){
temp = temp * 2;
figureHere->vertices = realloc(figureHere->vertices, temp * sizeof(struct pointxy));
}
}
figureHere->countPoints = i;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
void parseFile(FILE * fp, FILE * sketcher);
void processArgument(char argument[]);
void printOutput();
#define MAX_WORD 256
#define initial_size 17
extern const char argument[];
/* prototypes for non-c99 library functions */
FILE* popen(const char*, const char*);
int pclose(FILE*);
struct pointxy {
double x;
double y;
};
struct figure{
char figureName[MAX_WORD];
struct pointxy *vertices;
long int countPoints;
};
*** glibc detected *** ./draw2: realloc(): invalid pointer: 0xbf9c3a28 ***
======= Backtrace: =========
/lib/libc.so.6(realloc+0x3a8)[0x400cb548]
/lib/libc.so.6(realloc+0x3c)[0x400cb1dc]
./draw2[0x8048ab8]
./draw2[0x8048ec1]
./draw2[0x8048f84]
/lib/libc.so.6(__libc_start_main+0xe0)[0x40072390]
./draw2[0x8048891]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:16 160237 /home/dsk07/ugrad/pkardash/c201/assignment2/draw2
0804a000-0804b000 rw-p 00002000 00:16 160237 /home/dsk07/ugrad/pkardash/c201/assignment2/draw2
0804b000-0804c000 rw-p 00000000 00:00 0
09283000-092a4000 rw-p 00000000 00:00 0 [heap]
40000000-4001c000 r-xp 00000000 08:01 94922 /lib/ld-2.7.so
4001c000-4001d000 r--p 0001b000 08:01 94922 /lib/ld-2.7.so
4001d000-4001e000 rw-p 0001c000 08:01 94922 /lib/ld-2.7.so
4001e000-40022000 rw-p 00000000 00:00 0
40022000-4002c000 r-xp 00000000 08:07 150880 /usr/lib/libgcc_s.so.1
4002c000-4002d000 rw-p 00009000 08:07 150880 /usr/lib/libgcc_s.so.1
40036000-4005a000 r-xp 00000000 08:01 94889 /lib/libm-2.7.so
4005a000-4005b000 r--p 00023000 08:01 94889 /lib/libm-2.7.so
4005b000-4005c000 rw-p 00024000 08:01 94889 /lib/libm-2.7.so
4005c000-401a1000 r-xp 00000000 08:01 94899 /lib/libc-2.7.so
401a1000-401a2000 ---p 00145000 08:01 94899 /lib/libc-2.7.so
401a2000-401a4000 r--p 00145000 08:01 94899 /lib/libc-2.7.so
401a4000-401a5000 rw-p 00147000 08:01 94899 /lib/libc-2.7.so
401a5000-401a9000 rw-p 00000000 00:00 0
bf9b0000-bf9c5000 rw-p 00000000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
make: *** [run] Aborted
glibc is telling you you're passing in an address that couldn't have been returned from malloc/realloc. This is because you're passing in the address of the pointsAndName
stack variable. You need to pass in the value, which is what you received from malloc
. Also, whenever you call realloc
, you should use a temporary variable. That way, if the realloc
fails, you still free the original value.
struct figure *tempPtr = realloc(pointsAndname, temp * sizeof(struct figure));
if(tempPtr == NULL)
{
// Handle allocation error...
free(pointsAndname);
}
pointsAndname = tempPtr;
这篇关于glibc的检测,realloc的():无效的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!