Solved

beginner C - malloc / free / valgrind. Help please

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
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…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

911 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

20 Experts available now in Live!

Get 1:1 Help Now