首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双倍免费/腐败?

双倍免费/腐败?
EN

Stack Overflow用户
提问于 2013-09-16 23:51:28
回答 2查看 355关注 0票数 1
代码语言:javascript
复制
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++;

}

这是我用于addRow的代码,如果我是malloc所有数据的话。我不明白为什么我会犯双倍的免费/腐败错误。在我在这里标记的区域,我相信我是malloc-在行结构中的所有信息实例,这些行是唯一做malloc/free的。

我只想在结束节目的时候适当地养成这个习惯。

全员课程:

代码语言:javascript
复制
#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");
    }
}

程序需要以EOF结尾的整数输入。特别是在换行符前有相同数目的整数。

更新:我使用gdb来分析空闲的第一部分--它只在第二次迭代中失败,使用for(i = 0; i < list.len; i++)for(i = list.len - 1; i > 0 ; i--)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-17 00:23:07

另一件事是,我没有看到对list.size的更新(在调整head大小时应该更新它)

票数 2
EN

Stack Overflow用户

发布于 2013-09-17 00:06:23

“我只是想在终止程序时适当地养成自由的习惯。”

正确的处理方法是释放一个非空指针,然后将指针设置为空。

例如:

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18839202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档