Solved

beginner C - malloc / free / valgrind. Help please

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error I/O in copying in linux redhat 12 93
VMWare Fusion 6 On Mac - Need To Increase Disk Size 17 75
What is atomic operation? 6 64
linux redhat 7.2 10 88
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

809 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