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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:49:54+00:00 2026-05-21T06:49:54+00:00

This is the edited code: I have a two dimensional stack, like #define push(s,ele)

  • 0

This is the edited code:

I have a two dimensional stack, like

#define push(s,ele) s.list[++(s.last)]=ele

typedef struct vp {
    short int v1,v2;
}VPTYPE;  

typedef struct VPLIST{
    int last;
    VPPTR *list;
}VPLISTTYPE,*VPLISTPTR ;    

VPLISTPTR v1v2;  
v1v2=(VPLISTPTR)malloc(sizeof(VPLISTTYPE)*nof);  
a=0;
while(a<100)
{  //allocation part
   for(i=0;i< nof;i++)  
   {  
      v1v2[i].list=(VPPTR *)malloc(20*(sizeof(VPPTR)));  

     for(i2=0;i2< 10;i2++) //please note that I am not filling the array completely, in the actual code the value 10 is dependent on certain factors, which I am omitting for the sake of simplicty 
     {  
         v=(VPTYPE *)malloc(sizeof(VPTYPE));  
         push(v1v2[i],v); 
         v1v2[i]->v1=1;v1v2[i]->v2=2;
     }
   }
   // some algorithm goes on here which accesses these values in the stack

   // free memory part
  for(i=0;i< nof;i++)  
   {  
      for(i2=0;i2<= (v1v2[i2].last);i2++)
     {  
         free(v1v2[i2].list[i]); 
     }
   }

   a++;
}

When I free the memory this way there is memory leakage. Please let me know where I am going wrong.

Thank you very much.

  • 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-21T06:49:54+00:00Added an answer on May 21, 2026 at 6:49 am
    • You are not initializing the allocated memory in the code you show. In general, you get non-zero garbage allocated by malloc(). If you need zeroed memory, use calloc().

    • There’s also the problem identified by JCooper‘s answer.

    • There’s also the problem identified by Muggen‘s comment.

    • You are freeing the items on the stack, but not the stacks as a whole. That should be done inside the ‘for (i2 = 0; ...)‘ loop but after the ‘for (k2 = 0; ...)‘ loop.

    Collectively, these add up to a minor catastrophe.


    After the code edit…

    • The type VPPTR is not defined, but is presumably meant to be ‘typedef VPTYPE *VPPTR;.
    • In the struct VPLIST, you have a pointer to a VPPTR – one more reason to distrust such pointer typedefs. You almost certainly intended to have a simple VPPTR there. However, the other code does assume you need an array of pointers to pointers, so it is self-consistent (up to a point).
    • This problem propagates into the memory allocation code.
    • In your free memory loop, in the call to free(), you have reversed the roles of i and i2:

      free(v1v2[i2].list[i]);  // Yours
      free(v1v2[i].list[i2]);  // Mine
      
    • Your assignments in the allocation loop (v1v2[i]->v1=1;v1v2[i]->v2=2;) are bogus.

    The following code compiles clean and runs clean:

    $ cc -Wall -Wextra -g -O3 -std=c99 x.c -o x
    $ valgrind ./x
    ==16593== Memcheck, a memory error detector.
    ==16593== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
    ==16593== Using LibVEX rev 1658, a library for dynamic binary translation.
    ==16593== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
    ==16593== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
    ==16593== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
    ==16593== For more details, rerun with: -v
    ==16593== 
    ==16593== 
    ==16593== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from 1)
    ==16593== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==16593== malloc/free: 2,201 allocs, 2,201 frees, 40,032 bytes allocated.
    ==16593== For counts of detected errors, rerun with: -v
    ==16593== All heap blocks were freed -- no leaks are possible.
    $
    

    Working Code

    #include <stdlib.h>
    #include <stdio.h>
    
    #define push(s, ele) ((s).list[((s).last)++] = (ele))
    
    typedef struct vp
    {
        short v1;
        short v2;
    } VPTYPE;
    
    typedef struct VPLIST
    {
        int     last;
        VPTYPE **list;
    } VPLISTTYPE;
    
    enum { nof = 2 };
    
    int main(void)
    {
        VPLISTTYPE *v1v2 = (VPLISTTYPE *)malloc(sizeof(*v1v2) * nof);
    
        for (int i = 0; i < nof; i++)
            v1v2[i].last = 0;
    
        for (int a = 0; a < 100; a++)
        {
            //allocation part
            for (int i = 0; i < nof; i++)
            {
                v1v2[i].list = (VPTYPE **)malloc(20 * sizeof(*v1v2[i].list));
    
                for (int i2 = 0; i2 < 10; i2++)
                {
                    VPTYPE *v = (VPTYPE *)malloc(sizeof(*v));
                    v->v1 = 1;
                    v->v2 = 2;
                    push(v1v2[i], v);
                }
            }
    
            // free memory part
            for (int i = 0; i < nof; i++)
            {
                for (int i2 = 0; i2 < (v1v2[i].last); i2++)
                {
                    free(v1v2[i].list[i2]);
                }
                free(v1v2[i].list);
                v1v2[i].list = 0;
                v1v2[i].last = 0;
            }
        }
        free(v1v2);
        return 0;
    }
    

    Simpler Working Code

    This code uses one less level of indirection – and one less level of memory allocation, therefore – and compiles and runs equally cleanly.

    #include <stdlib.h>
    #include <stdio.h>
    
    #define push(s, ele) ((s).list[((s).last)++] = (ele))
    
    typedef struct vp
    {
        short v1;
        short v2;
    } VPTYPE;
    
    typedef struct VPLIST
    {
        int     last;
        VPTYPE *list;
    } VPLISTTYPE;
    
    enum { nof = 2 };
    
    int main(void)
    {
        VPLISTTYPE *v1v2 = (VPLISTTYPE *)malloc(sizeof(*v1v2) * nof);
    
        for (int i = 0; i < nof; i++)
            v1v2[i].last = 0;
    
        for (int a = 0; a < 100; a++)
        {
            //allocation part
            for (int i = 0; i < nof; i++)
            {
                v1v2[i].list = (VPTYPE *)malloc(20 * sizeof(*v1v2[i].list));
    
                for (int i2 = 0; i2 < 10; i2++)
                {
                    VPTYPE v;
                    v.v1 = 1;
                    v.v2 = 2;
                    push(v1v2[i], v);
                }
            }
    
            // free memory part
            for (int i = 0; i < nof; i++)
            {
                free(v1v2[i].list);
                v1v2[i].list = 0;
                v1v2[i].last = 0;
            }
        }
        free(v1v2);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .
I have two HTML files on my www folder using the PhoneGap framework. The
I have two dev servers, one is PHP version 5.3.1 which is on my
This an C# WPF application with SQL CE as DataSource: I have a DataTable
I have the following code structure: <input type=checkbox name=some_name_1 value=some_val /> <input type=text name=input_name_1
(Probably just knowledge of the Unix library is enough to answer this, so please
First this IS a Java question so forgive this first C#-related explanation... I've most
I have been looking at porting a cuda library to fortran. PGI and EM
I need a value of Someclass based on the key. And the key can

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.