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

  • SEARCH
  • Home
  • 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 6549543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:06:20+00:00 2026-05-25T12:06:20+00:00

For the following sample of code there is memory leak. How can we prevent

  • 0

For the following sample of code there is memory leak. How can we prevent memory leak in the following case:

            1 #include <stdio.h>
            2 #include <stdlib.h>
            3 
            4 typedef struct sample_help {
            5 int *value;
            6 void **pointers;
            7 }*sample,sample_node;
            8 
            9 
            10 sample main(){
            11 sample  ABC=NULL; 
            12 sample  XYZ=NULL;
            13 sample  kanchi = NULL;
            14 
            15 ABC = malloc(sizeof(sample_node));
            16 XYZ = malloc(sizeof(sample_node));
            17 ABC->pointers = malloc(5*sizeof(void *));
            18 XYZ->pointers = malloc(5*sizeof(void *));
            19 ABC->value = malloc(5*sizeof(int));
            20 XYZ->value = malloc(5*sizeof(int));
            21 
            22 ABC->value[0] = 10;
            23 ABC->value[1] = 20;
            24 
            25 XYZ->pointers[0] = ABC;
            26 kanchi = XYZ->pointers[0];
            27 
            28 printf("::::%d\n",XYZ->pointers[0]);
            29 printf("kanchi1:::::%d\n",kanchi->value[0]);
            30 
            31 
            32 return XYZ;
            33 }
            34 

Following is the output of valgrind.

==27448== 
==27448== HEAP SUMMARY:
==27448==     in use at exit: 152 bytes in 6 blocks 
==27448==   total heap usage: 6 allocs, 0 frees, 152 bytes allocated
==27448== 
==27448== 152 (16 direct, 136 indirect) bytes in 1 blocks are definitely lost in loss  record 6 of 6
==27448==    at 0x4C244E8: malloc (vg_replace_malloc.c:236)
==27448==    by 0x40056B: main (test2.c:16)
==27448== 
==27448== LEAK SUMMARY:
==27448==    definitely lost: 16 bytes in 1 blocks
==27448==    indirectly lost: 136 bytes in 5 blocks
==27448==      possibly lost: 0 bytes in 0 blocks
==27448==    still reachable: 0 bytes in 0 blocks
==27448==         suppressed: 0 bytes in 0 blocks  
==27448== 
  • 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-25T12:06:21+00:00Added an answer on May 25, 2026 at 12:06 pm

    Your code should really be more like this:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct sample_help {
        int *value;
        void **pointers;
    } *sample, sample_node;
    
    sample foo(void)
    {
        sample  ABC=NULL;
        sample  XYZ=NULL;
        sample  kanchi = NULL;
    
        ABC = malloc(sizeof(sample_node));
        XYZ = malloc(sizeof(sample_node));
        ABC->pointers = malloc(5*sizeof(void *));
        XYZ->pointers = malloc(5*sizeof(void *));
        ABC->value = malloc(5*sizeof(int));
        XYZ->value = malloc(5*sizeof(int));
    
        ABC->value[0] = 10;
        ABC->value[1] = 20;
    
        XYZ->pointers[0] = ABC;
        kanchi = XYZ->pointers[0];
    
        printf("::::%d\n",XYZ->pointers[0]);
        printf("kanchi1:::::%d\n",kanchi->value[0]);
    
        return XYZ;
    }
    
    int main(void)
    {
        // call your function
        sample xyz = foo();
    
        // ... do something with the data structure xyz ...
    
        // free memory allocated by your function
        free(xyz->pointers[0]->value);    // free ABC->value
        free(xyz->pointers[0]->pointers); // free ABC->pointers
        free(xyz->pointers[0]);           // free ABC
        free(xyz->value);                 // free XYZ->value
        free(xyz->pointers);              // free XYZ->pointers
        free(xyz);                        // free XYZ
    
        return 0;
    }
    

    Note that we free the data structure from within main() after we’re done with it.

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

Sidebar

Related Questions

I have the following code: #include <memory> class Foo; typedef std::tr1::shared_ptr<Foo> pFoo_t; class DoSomething
I have the following sample code in a VB.NET console application. It compiles and
I have the following sample code which doesn't seem to want to run. import
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I'm following the sample code in CFNetwork Programming Guide , specifically the section on
Given the following code sample: uint8_t i, in, ni; i = in = 2;
I have the following code sample that im trying to wrap my head around
The goal of the following code sample is to read the contents of $target
In the sample code, the line with the 'error comment' gives the following the
I have downloaded sample code from Apple Center.I also have gone through following question:

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.