Solved

beginner C - malloc / free / valgrind. Help please

Posted on 2010-11-10
2
759 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
After running Ubuntu some time, you will be asked to download updates for fixing bugs and security updates. All the packages you download replace the previous ones, except for the kernel, also called "linux-image". This is due to the fact that w…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

831 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