Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

beginner C - malloc / free / valgrind. Help please

Posted on 2010-11-10
2
Medium Priority
?
781 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 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

971 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