Solved

beginner C - malloc / free / valgrind. Help please

Posted on 2010-11-10
2
757 Views
Last Modified: 2013-11-15
Can someone give me a hint why valgrind says I'm loosing memory:

==28478== 56 bytes in 7 blocks are definitely lost in loss record 1 of 1
==28478==    at 0x4006F3D: malloc (vg_replace_malloc.c:207)
==28478==    by 0x8048443: insert (part1.c:21)
==28478==    by 0x80484C2: main (part1.c:38)

I know normally you wouldn't have a separate loop just for free() but its an assignment so not my rules! I have equal numbers of malloc & free, so I cant work out where I am going wrong.
Thanks !

 
#include <stdio.h>
#include <stdlib.h>

/* these arrays are just used to give the parameters to 'insert',
   to create the 'people' array */
#define HOW_MANY 7
char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", "Harriet"};
int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24};
static int nextfreeplace = 0;

/* declare your struct for a person here */
typedef struct person {
  char *peopleName;
	int age;
	 }Human;

static void insert (Human *people[], char *name, int age) 
{ 
  people[nextfreeplace] = (Human *)malloc (sizeof(Human));

/* put name and age into the next free place in the array parameter here */	
	people[nextfreeplace]->peopleName = name; 
	people[nextfreeplace]->age = age;
	nextfreeplace ++;  
}

int main(int argc, char **argv) {

  /* declare the people array here */
  Human *people[HOW_MANY];

  int count = 0; 
  for (count = 0; count < HOW_MANY; count ++) {
   insert (people, names[count], ages[count]);	  
   }	 

  /* print the people array here*/
  int index = 0;
	for (index = 0; index < HOW_MANY; index ++)
	{printf("\nPerson number %d is called %s and is %d years old", index, people[index]->peopleName, people[index]->age);  }

int index3 = 0;
for (index3 = 0; index < HOW_MANY; index ++) 
{
free(people[index3]);
}

return 0;
}

Open in new window

0
Comment
Question by:pingulingo
2 Comments
 
LVL 9

Accepted Solution

by:
wellhole earned 500 total points
ID: 34103450
you're using index3 in the loop along with index. That's just not good.
0
 

Author Comment

by:pingulingo
ID: 34103562
whoops typo! has fixed error though. thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now