strlen和malloc:C内存泄漏 [英] strlen and malloc: C memory leaks

查看:248
本文介绍了strlen和malloc:C内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是无效的!我没有适当地释放学生!我将尽快接受向我透露这个答案的答案!

This question is null and void! I was not properly freeing the Students! I will accept the answer that revealed this to me as quickly as I am allowed!

我是C语言的新手,正在练习malloc.在很大的范围内,我正在编写一个链接列表库.这个create_student函数是我将用来测试链表库的许多函数之一.问题是...我运行valgrind并调用此函数,它表明第一个malloc导致了若干内存泄漏.就我所知,这一切看起来都很可靠:

I'm new to C and am practicing malloc. In the grand scope, I'm writing a linked list library; this create_student function is among many functions I'm going to use to test my linked list library. The problem is...I run valgrind and call this function and it indicates there are several memory leaks caused by that first malloc. It all looks solid, best to my knowledge:

typedef struct Student
{
        char* first_name; /* This will be malloc'd!*/
    char* last_name; /* This will also be malloc'd */
    int grade;
    long id;
} Student;


Student* create_student(const char* first_name, const char* last_name, int grade, long gtid)
{

        /* First allocate a student on the heap */
        Student *newStudentp = (malloc(sizeof(Student)));


    /* Allocate enough space for the first and last names */
    newStudentp -> last_name = (malloc(strlen(last_name)));
    newStudentp -> first_name = (malloc(strlen(first_name)));



        // AND copy the first and last name to the first and last name fields in the struct   
    strncpy(newStudentp -> first_name, first_name, strlen(first_name));
    strncpy(newStudentp -> last_name, last_name, strlen(last_name));



        /* Set the grade and id */
    newStudentp -> grade = grade;
    newStudentp -> id = id;

        */ 
    return newStudentp;
}

我从valgrind收到的错误消息(有几个)看起来像这样:

My error messages from valgrind (there are several) look like this:

==4285==    9 bytes in 1 blocks are definitely lost in loss record 8 of 8
==4285==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==4285==    by 0x804855B: create_student (test.c:24)
==4285==    by 0x8048748: main (test.c:109)

第24行是

newStudentp -> last_name = (malloc(strlen(last_name))); 

行.

我是否在某种程度上滥用了strlen,从而导致了错误?

Is there some fundamental misuse of strlen by me that is causing an error?

推荐答案

这里有一些问题:

    newStudentp -> last_name = (malloc(strlen(last_name)));
    newStudentp -> first_name = (malloc(strlen(first_name)));

strlen仅给出最大长度,但不包括终止的'\0'.但这也必须存储,因此在两种情况下都应使用strlen(last_name) + 1.

strlen only gives the length up to but not including the terminating '\0'. But that must be stored too, so you should use strlen(last_name) + 1 in both cases.

此外,strncpy()最好使用分配的缓冲区的大小而不是源字符串的大小来完成,因此可以避免写超出数组的高边界.但是,由于您已经使用过malloc(strlen(...) + 1),因此只需在此处使用strcpy().

Also, your strncpy() should better be done using the size of the allocated buffer and not of the source string, so you can avoid writing past the high boundary of the array. But since you already used malloc(strlen(...) + 1), you can simply use strcpy() here.

这篇关于strlen和malloc:C内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆