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 6043289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:52:59+00:00 2026-05-23T06:52:59+00:00

typedef struct Model { int recordId; char *name; }Model; typedef struct ModelArray { //keeps

  • 0
typedef struct Model
{
    int recordId;
    char *name;
}Model;

typedef struct ModelArray
{
    //keeps the size that the array was initially create with. When more elements are needed
    //we use this to add that many more elements
    int originalSize;
    //total number of elements that can be used
    int elements;
    //total number of elements used
    int count;
    //the actual array is stored here
    Model *source;
}ModelArray;

void initModelArray(ModelArray *array, int numberOfElements)
{
    array->originalSize = numberOfElements;
    array->elements = numberOfElements;
    array->count = 0;
    array->source = malloc(sizeof(Model)*numberOfElements);//0 bytes in 3 blocks are definitely lost in loss record 1 of 65
}

void deallocModelArray(ModelArray *array)
{
    if(array == NULL)
        return;

    array->elements = 0;
    array->count = 0;
    free(array->source);
    array->source = NULL;
    free(array);
}

main(int argc, const char * argv[])
{
    ModelArray *models = malloc(sizeof(ModelArray));
    initModelArray(models, 10);
    deallocModelArray(models);
}

What is lost? Code looks fine to me. I’m sure I could say array->source = NULL first but it’s not needed, right?

  • 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-23T06:53:00+00:00Added an answer on May 23, 2026 at 6:53 am

    To deallocate these structures correctly, you need to do the following, in this order:

    free(models->source);
    free(models);
    

    If you do anything else, you’re leaking memory.

    Edit:

    OK, having seen the Model struct, you’re probably leaking the names, or at least valgrind thinks you do because you deallocate the ModelArray structure, which contains a pointer to a Model structure, which contains a char* which you don’t free first.

    So:

    int i;
    for( i=0; i<models->originalSize; i++ ) {
        if( models->source[i]->name != NULL ) {
            free( models->source[i]->name );
        }
    }
    free(models->source);
    free(models);
    

    And it would be a good idea to use calloc() instead of malloc() when allocating models->source in the first place. This will set all the name pointers to 0. Without this, the test for models->source[i]->name being non-NULL above might fail if name happens to contain some garbage (since using uninitialized memory produces undefined behavior.)

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

Sidebar

Related Questions

typedef struct unit_class_struct { char *name; char *last_name; } person; int setName(person *array) {
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
typedef struct child_list {int count; char vo[100]; child_list*next;} child_list; typedef struct parent_list { char
typedef struct { employeeT *employees; int nEmployees; } *payrollT; typedef struct { string name;
typedef struct Value{ int id; char type; char a; } Value; void fooV(Value v){
typedef struct unit_class_struct { char *name; } person; person * setName() { person *
I found that omnicppcomplete does not support typedef-ed struct name . I don't know
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void
Consider the following: typedef struct { int a; int b; int c; int d;

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.