双重自由/腐败? typedef结构{ int*信息; }行; 结构{ 行*头; 内伦; 整数大小; }名单; int main{ list.len=0; list.size=1; list.head=malloc(list.size*sizeof(行)); //……调用addRow(list.len)次的其他一些代码 对于(i=list.len-1;i>0;i--){ 免费(list.head[i].info);/****此处**********/ } 免费(列表头); } void addRow(int*数据){ int i; if(list.len==list.size){ 行*temp=malloc(sizeof(行)*list.size*2); if(temp==NULL){ fprintf(stderr,“错误(回车):(第##行)内存不足。\n”); 返回; } 对于(i=0;i
} 这是我用来添加所有数据的代码。我不明白为什么会出现双重自由/腐败错误。在我在这里标记的区域,我相信我正在为行结构中的所有信息实例malloc-ing,这些行是唯一执行malloc/free的行 我只是想养成一种习惯,在终止程序时适当地进行自由活动 完整程序:双重自由/腐败? typedef结构{ int*信息; }行; 结构{ 行*头; 内伦; 整数大小; }名单; int main{ list.len=0; list.size=1; list.head=malloc(list.size*sizeof(行)); //……调用addRow(list.len)次的其他一些代码 对于(i=list.len-1;i>0;i--){ 免费(list.head[i].info);/****此处**********/ } 免费(列表头); } void addRow(int*数据){ int i; if(list.len==list.size){ 行*temp=malloc(sizeof(行)*list.size*2); if(temp==NULL){ fprintf(stderr,“错误(回车):(第##行)内存不足。\n”); 返回; } 对于(i=0;i,c,malloc,free,C,Malloc,Free,} 这是我用来添加所有数据的代码。我不明白为什么会出现双重自由/腐败错误。在我在这里标记的区域,我相信我正在为行结构中的所有信息实例malloc-ing,这些行是唯一执行malloc/free的行 我只是想养成一种习惯,在终止程序时适当地进行自由活动 完整程序: typedef struct { int *info; } row; struct { row* head; int len; int size; } list; int main{ list
typedef struct {
int *info;
} row;
struct {
row* head;
int len;
int size;
} list;
int main{
list.len = 0;
list.size = 1;
list.head = malloc(list.size * sizeof(row));
//...... some other code that calls addRow (list.len) times
for (i = list.len - 1; i > 0; i--) {
free(list.head[i].info);/*****HERE**********/
}
free(list.head);
}
void addRow(int* data) {
int i;
if (list.len == list.size) {
row *temp = malloc(sizeof(row) * list.size * 2);
if (temp == NULL) {
fprintf(stderr, "Error (enter): (Line ##) Insufficient memory.\n");
return;
}
for (i = 0; i < list.len; i++) {
temp[i] = list.head[i];
}
free(list.head);
list.head = temp;
}
list.head[list.len].info = malloc(sizeof(int) * numCols);
for (i = 0; i < numCols; i++) {
list.head[list.len].info[i] = data[i];
}
list.len++;
#包括
#包括
#包括
类型定义结构{
int*信息;
}行;
结构{
行*头;
内伦;
整数大小;
}名单;
静态int-sortCol,numCols;
int qSortCompare(const void*,const void*);
作废打印列表();
int processInput();
无效无效(字符*,整数);
int main(int n,字符**args){
sortCol=1;
numCols=0;
如果(n>1&&args[1][0]='-'&&args[1][1]=='c'){
sortCol=atoi(args[2]);
}
list.len=0;
list.size=1;
list.head=malloc(list.size*sizeof(行));
processInput();
if(sortCol<1 | | sortCol>numCols){
fprintf(stderr,“错误(回车):(第##行)要排序的列无效。\n”);
返回1;
}
打印列表();
qsort(list.head、list.len、sizeof(row)和qSortCompare);
printf(“\n”);
打印列表();
int i;
printf(“add1:%p\nadd2:%p\n”,list.head[0].info,list.head[1].info);
对于(i=0;itotalN){
如果(总n>0){
numCols=totalN;
addRow(nums);
}否则{
fprintf(标准,
“错误(输入):(第##行)输入的第一行无效。\n”);
}
}
如果(ch!='\n'&&ch!='\0'){
fprintf(标准,
“错误(输入):(行##)输入的行太长。\n”);
//返回0;
}
}
返回1;
}
int qSortCompare(常数无效*c1,常数无效*c2){
行*t1,*t2;
t1=(第*)行c1;
t2=(第*)行c2;
返回t1->info[sortCol-1]-t2->info[sortCol-1];
}
作废打印列表(){
int i,j;
对于(i=0;i
程序需要一个以EOF终止的整数输入。特别是在换行之前使用相同数量的整数
更新:我使用gdb分析免费的第一部分,它只在第二次迭代中失败,使用for(I=0;I
和for(I=list.len-1;I>0;I--)
“我只是想在终止程序时正确地摆脱这种习惯。”
处理这种情况的正确方法是释放一个非NULL指针,然后将指针设置为NULL
例如:
另一件事是,我没有看到list.size的更新(在调整头部大小时应该更新)是我还是您试图在没有init的情况下更改类成员
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
typedef struct {
int *info;
} row;
struct {
row* head;
int len;
int size;
} list;
static int sortCol, numCols;
int qSortCompare(const void*, const void*);
void printList();
int processInput();
void nullify(char*, int);
int main(int n, char **args) {
sortCol = 1;
numCols = 0;
if (n > 1 && args[1][0] == '-' && args[1][1] == 'c') {
sortCol = atoi(args[2]);
}
list.len = 0;
list.size = 1;
list.head = malloc(list.size * sizeof(row));
processInput();
if (sortCol < 1 || sortCol > numCols) {
fprintf(stderr, "Error (enter): (Line ##) Invalid column to sort.\n");
return 1;
}
printList();
qsort(list.head, list.len, sizeof(row), &qSortCompare);
printf("\n");
printList();
int i;
printf("add1:%p\nadd2:%p\n", list.head[0].info, list.head[1].info);
for (i = 0; i < list.len; i++) {
free(list.head[i].info);
}
free(list.head);
return 0;
}
void nullify(char* str, int n) {
int i;
for (i = 0; i < n; i++)
str[i] = '\0';
}
int parseInt(char *str, int index) {
int num = -1;
sscanf(str + index, "%d", &num);
return num;
}
void addRow(int* data) {
int i;
if (list.len == list.size) {
row *temp = malloc(sizeof(row) * list.size * 2);
if (temp == NULL) {
fprintf(stderr, "Error (enter): (Line ##) Insufficient memory.\n");
return;
}
for (i = 0; i < list.len; i++) {
temp[i] = list.head[i];
}
free(list.head);
list.head = temp;
}
list.head[list.len].info = malloc(sizeof(int) * numCols);
if (list.head[list.len].info == NULL) {
fprintf(stderr, "Error (enter): (Line ##) Insufficient memory.\n");
return;
}
for (i = 0; i < numCols; i++) {
list.head[list.len].info[i] = data[i];
}
list.len++;
}
int processInput() {
int i, maxChars = 200, totalN = 0;
int *nums, curNumIndex = 0, onNum, curNum;
numCols = maxChars / 2;
nums = (int*) (malloc(sizeof(int) * numCols));
char str[maxChars], ch;
for (i = 0; i < numCols; i++) {
nums[i] = -1;
}
while (!feof(stdin)) {
nullify(str, maxChars);
fgets(str, maxChars, stdin);
onNum = isdigit(str[0]);
curNumIndex = 0;
for (i = 0; i < maxChars; i++) {
ch = str[i];
if ((!isspace(ch)) && (!isdigit(ch)) && (ch != '\0')) {
fprintf(stderr, "Error 1: (Line ##) Invalid char in input.\n");
//return 0;
}
if (isspace(ch) && onNum) {
curNum = parseInt(str, curNumIndex);
curNumIndex = i;
nums[totalN % numCols] = curNum;
totalN++;
if (totalN % numCols == 0)
addRow(nums);
} else {
onNum = isdigit(str[i]);
}
if (ch == '\n' || ch == '\0')
break;
}
if (numCols > totalN) {
if (totalN > 0) {
numCols = totalN;
addRow(nums);
} else {
fprintf(stderr,
"Error (enter): (Line ##) Invalid first line of input.\n");
}
}
if (ch != '\n' && ch != '\0') {
fprintf(stderr,
"Error (enter): (Line ##) A row from input too long.\n");
//return 0;
}
}
return 1;
}
int qSortCompare(const void *c1, const void *c2) {
row *t1, *t2;
t1 = (row*)c1;
t2 = (row*)c2;
return t1->info[sortCol - 1] - t2->info[sortCol - 1];
}
void printList() {
int i, j;
for (i = 0; i < list.len; i++) {
for (j = 0; j < numCols; j++) {
printf("%10d ", list.head[i].info[j]);
}
printf("\n");
}
}
int* x = malloc (sizeof (int));
if (x != NULL) {
free (x);
x = NULL;
}
/* Misc. Code ... */
/* Now for whatever reason, you want to free x again */
/* This branch is never triggered, because you were smart enough to set x to NULL
* when you freed it the first time...
*/
if (x != NULL) {
free (x);
x = NULL;
}