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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:20:57+00:00 2026-06-02T18:20:57+00:00

I am doing C++ program for a real application project. I use valgrind to

  • 0

I am doing C++ program for a real application project.

I use valgrind to do memory leak check.

I got :

160 bytes in 10 blocks are definitely lost in loss record 1 of 2
==14312== at 0x4A0846F: malloc (vg_replace_malloc.c:236)

But, I have called free() to release it.

How to fix it ? Why I have memory leak ?

Any help will be appreciated. !

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
int * GetSomeIDs() ; 
/*-------- GetSomeIDs-----*/

typedef struct {
       char* alias;   /* '\0'-terminated C string */
       int   specific_id;
} aliasID;

int GetNumberOfAliases(void)
{
    return 10;
}

aliasID *GetNextAlias(void)
{
    aliasID *p = (aliasID *) malloc(sizeof(aliasID)) ;
    p->alias = "test alias";
    p->specific_id = rand();
    return p; 
}

int *GetSomeIDs2(aliasID  ***a, int *aIDsize , int *idSize)
{
    int  *ids  = (int *) malloc(sizeof(int) * 8) ;  
    *idSize = 8 ; 
    int length = GetNumberOfAliases();
    *aIDsize = length ;
    int i  ;
    *a = (aliasID**) malloc(sizeof(aliasID*)*length) ;

    for(i = 0 ; i < length ; ++i)
    {
        (*a)[i] = GetNextAlias();
    }

    return  ids; 
}

void callGetSomeIDs()
{
    aliasID  **aD = NULL;
    int mySize = -1; 
    int myidSize = -1; 
    int *pt = NULL;
    pt = GetSomeIDs2(&aD, &mySize, &myidSize); 
    if (!pt || !aD || mySize <= 0 || myidSize <= 0)
    {
        printf("there is a runt time error of GetSomeIDs \n");
        if (!pt && aD)
        {
            printf("there is a runt time error of GetSomeIDs pt is NULL \n");
            free(aD);
        }
    if (!aD && pt)
        {
            printf("there is a runt time error of GetSomeIDs aD is NULL \n");
            free(pt);
        }
        if (!pt && !aD)
        {
            printf("there is a runt time error of GetSomeIDs aD and pt all NULL \n");
        }

        exit(1);
    }
    // print out 8 int
    printf("the followings are integers  \n");
    int i ;
    for(i = 0 ; i < myidSize ; ++i )
    {
        printf("%d " , pt[i]);
    }
    printf("\n");
    // print out alias and ID 
    printf("the followings are alias and ID  \n");
    for (i = 0 ; i <  mySize   ; ++i )
    {
        printf( "alias is  %s " , (*aD[i]).alias) ;   
            printf( "specific_id is  %d", (*aD[i]).specific_id) ;
        printf("\n") ;
    }

    free(pt);
    free(aD);
  }

  int main()
  {

      callGetSomeIDs();

      return 0; 
   }
  • 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-06-02T18:21:00+00:00Added an answer on June 2, 2026 at 6:21 pm

    You free the array of aliasID structures, but not the allocated entries in the array.

    Edit:

    You allocate first an array of aliasID pointers, then for each entry in the array you allocate the actual aliasID. You need to free the aliasID pointers in the array first before you free the array:

    for (int i = 0; i < length_of_array; i++)
        free(aD[i]);
    free(ad);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use mtrace to detect memory leaks in a fortran program. I'm
I am doing my program in cocos2d. I am using NSDate to get the
I am doing a recursive program and I am getting an error about conflicting
I'm doing a small program with sencha touch. I deployed it on my webserver
I am doing a minesweeper program for school, but i keep getting this error
i, I have to run about a 100 tests on my program and doing
This program I'm doing is about a social network, which means there are users
While doing some random experimentation with a factorial program in C, Python and Scheme.
I am basically doing a tutorial but my program seems to have errors that
I'm doing a code testing for a program that calculate the results for a

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.