Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:40:23+00:00 2026-05-24T00:40:23+00:00

This question is null and void! I was not properly freeing the Students! I

  • 0

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!

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;
}

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)

line 24 is the

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

line.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:40:24+00:00Added an answer on May 24, 2026 at 12:40 am

    There are a few problems here:

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

    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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question and answer shows how to send a file as a byte array
This question is about removing sequences from an array, not duplicates in the strict
I've read a few posts on this, but there's still one thing that's not
While reading answers to this question I noticed that answers ( this for example)
This question is kind of an add-on to this question In C#, a switch
This question comes on the heels of the question asked here . The email
This question would probably apply equally as well to other languages with C-like multi-line
This question is a follow up to my previous question about getting the HTML
(This question is over 6 years old and probably no longer has any relevance.)
This question is the other side of the question asking, How do I calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.