gpt4 book ai didi

c - 我的 C 程序中存在内存泄漏

转载 作者:行者123 更新时间:2023-11-30 14:58:39 25 4
gpt4 key购买 nike

以下代码可以正常工作,但出现内存泄漏。该程序使用链表和动态存储功能。该程序不将 .txt 文件作为命令行参数。

示例文件:

Jacob Miller 832323244 21
Tom Darren 8931701830 19
Paul Mitchell 329797231 20
Here the output from Valgrind

==5742== HEAP SUMMARY:
==5742== in use at exit: 324 bytes in 9 blocks
==5742== total heap usage: 11 allocs, 2 frees, 908 bytes allocated
==5742==
==5742== 52 bytes in 1 blocks are definitely lost in loss record 2 of 4
==5742== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==5742== by 0x400A2C: student_record_allocate (new.c:106)
==5742== by 0x4007EB: main (new.c:46)
==5742==
==5742== 272 (16 direct, 256 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==5742== at 0x4A0610C: malloc (vg_replace_malloc.c:195)
==5742== by 0x400A1E: student_record_allocate (new.c:104)
==5742== by 0x4008D1: parseFile (new.c:74)
==5742== by 0x400803: main (new.c:47)
==5742==
==5742== LEAK SUMMARY:
==5742== definitely lost: 68 bytes in 2 blocks
==5742== indirectly lost: 256 bytes in 7 blocks
==5742== possibly lost: 0 bytes in 0 blocks
==5742== still reachable: 0 bytes in 0 blocks
==5742== suppressed: 0 bytes in 0 blocks
==5742==
==5742== For counts of detected and suppressed errors, rerun with: -v
==5742== ERROR SUMMARY: 66 errors from 21 contexts (suppressed: 4 from 4)

There is some problem here code snippet.

    void parseFile(char* filename, struct student_record_node** head)
{
FILE *fp;
student_record_node *sn;

fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Can't Open file %s\n", filename);
exit(EXIT_FAILURE);
}

while(!feof(fp)) {
sn = student_record_allocate();

fscanf(fp, "%20s", sn->record_->first_name_);
fscanf(fp, "%20s", sn->record_->last_name_);
fscanf(fp, "%d", &(sn->record_->student_id_));
fscanf(fp, "%d", &(sn->record_->student_age_));

appendNode(*head, sn);
}

fclose(fp);
}

void printNode(struct student_record_node* node)
{
printf("\nstruct student_record_node:\n");
if (node) {
printf("\tstudent first name: %s\n",node->record_->first_name_);
printf("\tstudent second name: %s\n",node->record_->last_name_);
printf("\tstudent id: %d\n", node->record_->student_id_);
printf("\tstudent age: %d", node->record_->student_age_);

}
}

struct student_record_node* student_record_allocate()
{
student_record_node *newNode=NULL;
student_record *newRecord=NULL;

newNode = (student_record_node *)malloc(sizeof(student_record_node));
newRecord = (student_record *)malloc(sizeof(student_record));
newNode->record_ = newRecord;
newNode->next_ = NULL;

return newNode;
free(newNode->record_);
}

{
free(node->record_);
free(node->next_);

}

我正在通过此方法释放上述内容

    void freeNodeList(struct student_record_node* head)
{
student_record_node *tmp;
while(tmp != NULL) {
tmp=head;
head=head->next_;
free(tmp);
}
head=NULL;
}

Here is my full code

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student_record
{
int student_id_;
int student_age_;
char first_name_[21];
char last_name_[21];
}s;

struct student_record_node
{
struct student_record* record_;
struct student_record_node* next_;
};
typedef struct student_record student_record;
typedef struct student_record_node student_record_node;

void parseFile(char* filename, struct student_record_node** head);
void printNode(struct student_record_node* node);
struct student_record_node* student_record_allocate();
void student_record_node_deallocate(struct student_record_node* node);
void sortByAge(struct student_record_node** recordsHead);
void sortById(struct student_record_node** recordsHead);
void swap(struct student_record_node** node1, struct student_record_node** node2);
void freeNodeList(struct student_record_node* head);
void appendNode(struct student_record_node* head, struct student_record_node* newNode);
void printNodeList(struct student_record_node* head);
int main(int argc, char **argv)
{
student_record_node *head;
if (argc != 2) {
fprintf(stderr, "Error: expected input file");
exit(EXIT_FAILURE);
}
/* student_record *newRecord=NULL;
recordsHead->record_ = newRecord;
recordsHead->next_ = NULL;
*/
head = student_record_allocate();
parseFile(argv[1], &head);
printf("Before sorting...");
printNodeList(head);
printf("Sorting by age...");
sortByAge(&head);
printNodeList(head);
printf("Sorting by id...");
sortById(&head);
printNodeList(head);
freeNodeList(head);
free(head);

return 0;
}

void parseFile(char* filename, struct student_record_node** head)
{
FILE *fp;
student_record_node *sn;

fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Can't Open file %s\n", filename);
exit(EXIT_FAILURE);
}

while(!feof(fp)) {
sn = student_record_allocate();

fscanf(fp, "%20s", sn->record_->first_name_);
fscanf(fp, "%20s", sn->record_->last_name_);
fscanf(fp, "%d", &(sn->record_->student_id_));
fscanf(fp, "%d", &(sn->record_->student_age_));

appendNode(*head, sn);
}

fclose(fp);
}

void printNode(struct student_record_node* node)
{
printf("\nstruct student_record_node:\n");
if (node) {
printf("\tstudent first name: %s\n",node->record_->first_name_);
printf("\tstudent second name: %s\n",node->record_->last_name_);
printf("\tstudent id: %d\n", node->record_->student_id_);
printf("\tstudent age: %d", node->record_->student_age_);

}
}

struct student_record_node* student_record_allocate()
{
student_record_node *newNode=NULL;
student_record *newRecord=NULL;

newNode = (student_record_node *)malloc(sizeof(student_record_node));
newRecord = (student_record *)malloc(sizeof(student_record));
newNode->record_ = newRecord;
newNode->next_ = NULL;

return newNode;
free(newNode->record_);
}

{
free(node->record_);
free(node->next_);

}

void sortByAge(struct student_record_node **recordsHead)
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;

do
{
swapped = 0;
ptr1 = *recordsHead;

while (ptr1->next_ != lptr)
{
if (ptr1->record_->student_age_ > ptr1->next_->record_->student_age_)
{
swap(&ptr1, &ptr1->next_);
swapped = 1;
}
ptr1 = ptr1->next_;
}
lptr = ptr1;
}
while (swapped);
}

void sortById(struct student_record_node** recordsHead)
{
int swapped, i;
struct student_record_node *ptr1;
struct student_record_node *lptr = NULL;

do
{
swapped = 0;
ptr1 = *recordsHead;

while (ptr1->next_ != lptr)
{
if (ptr1->record_->student_id_ > ptr1->next_->record_->student_id_)
{
swap(&ptr1, &ptr1->next_);
swapped = 1;
}
ptr1 = ptr1->next_;
}
lptr = ptr1;
}
while (swapped);
}

void swap(struct student_record_node** node1, struct student_record_node** node2)
{
student_record_node *tmp, *n1, *n2;

n1 = *node1;
n2 = *node2;
tmp->record_= n1->record_;
n1->record_= n2->record_;
n2->record_ = tmp->record_;

}

void freeNodeList(struct student_record_node* head)
{
student_record_node *tmp;
while(tmp != NULL) {
tmp=head;
head=head->next_;
free(tmp);
}
head=NULL;
}

void appendNode(struct student_record_node* head, struct
student_record_node* newNode)
{
student_record_node *prev, *next;

if(head == NULL) {
head = newNode;
return;
}

next = head;
while(next != NULL) {
prev = next;
next = next->next_;
}
prev->next_ = newNode;
}

void printNodeList(struct student_record_node* head)
{
student_record_node *tmp = head;

while(tmp != NULL) {
tmp = tmp->next_;
printNode(tmp);
}

}

最佳答案

此代码不会初始化tmp:

void freeNodeList(struct student_record_node* head) {
student_record_node *tmp;
while(tmp != NULL) {
tmp=head;
head=head->next_;
free(tmp);
}
head=NULL;
}

因此,在读取未初始化的变量时,您会出现未定义的行为。

您可能打算使用 head != NULL 作为 while 条件。

关于c - 我的 C 程序中存在内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43243456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com