
January 27th, 2013, 03:55 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Time spent in forums: 56 m 9 sec
Reputation Power: 0
|
|
|
Memory & arrays - Memory Leak help
Hey, I'm having issues trying to fix my memory leak.. Im using Visual2010, and im only getting one leak.
---" \visual studio 2010\projects\ass0_freill1\ass0_freill1\ass0_freil l1.c(110) : {59} normal block at 0x00644338, 0 bytes long."
I've been working at this for quite some time now.. and i was wondering if any of you could point out my mistakes.
Thanks!
Code:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
char* firstname;
char* lastname;
int id;
float mark;
}StudentRecord;
StudentRecord* g_pRecords;
int g_numRecords = 0;
char* g_FileName;
FILE* g_pf;
typedef enum{FALSE,TRUE}BOOL;
void OpenFile(void);
void ListRecords(void);
void AddRecord(void);
void DeleteRecord(void);
void SaveRecords(void);
int main()
{
BOOL running = TRUE;
char response;
OpenFile();
while(running)
{
printf(
"1. List records in memory\n"
"2. Add a new record\n"
"3. Delete last record\n"
"4. Quit and Save records to file\n"
);
fflush(stdin);
scanf("%c",&response);
switch(response)
{
case '1':
ListRecords();
break;
case '2':
AddRecord();
break;
case '3':
DeleteRecord();
break;
case '4':
running = FALSE;
SaveRecords();
break;
default: printf("Invalid Choice\n");
}
}
return 0;
}
void OpenFile(void){
int n_count = 0;
int n_id = 0;
char c_filename[30];
float f_mark = 0.0;
char c_fName[32];
char c_lName[32];
printf("OPEN FILE\nPlease enter the name of the file to open: ");
fflush(stdin);
scanf("%s",c_filename);
g_FileName = (char *)malloc(((int)(strlen(c_filename)+1)));
strcpy(g_FileName, c_filename);
g_pRecords = (StudentRecord *)malloc(g_numRecords * sizeof(StudentRecord));
if((g_pf = fopen(g_FileName, "r" )) != NULL && fgetc(g_pf) != EOF){
rewind(g_pf);
while(fscanf(g_pf, "%32s %32s %d %f\n", c_fName, c_lName, &n_id, &f_mark) != EOF){
g_pRecords[n_count].firstname = (char *)malloc(((int)strlen(c_fName)+1));
strcpy(g_pRecords[n_count].firstname, c_fName);
g_pRecords[n_count].lastname = (char *)malloc(((int)strlen(c_lName)+1));
strcpy(g_pRecords[n_count].lastname, c_lName);
g_pRecords[n_count].id = n_id;
g_pRecords[n_count].mark = f_mark;
n_count++;
}
g_numRecords = n_count;
printf("Records Added: %d\n", g_numRecords);
}else{
g_numRecords = 0;
printf("Empty File\n");
}
fclose(g_pf);
if(g_numRecords == 0)
free(g_pRecords);
}
void ListRecords(void){
int n_Records = 0;
printf("LIST RECORDS IN MEMORY\n");
if(g_numRecords >= 0){
for(n_Records = 0; n_Records < g_numRecords; n_Records++){
printf("Record #%d\n", n_Records+1);
printf("First name = %s\n", g_pRecords[n_Records].firstname);
printf("Last Name = %s\n", g_pRecords[n_Records].lastname);
printf("ID = %d\n", g_pRecords[n_Records].id);
printf("Mark = %5.2f\n", g_pRecords[n_Records].mark);
printf("\n");
}
}else
printf("No records\n");
}
void AddRecord(void){
int n_count = 0;
char c_fName[32];
char c_lName[32];
for(n_count = 0; n_count <= g_numRecords; n_count++){
if(n_count == g_numRecords){
printf("Please enter First Name: ");
fflush(stdin);
scanf("%s", c_fName, (int)(strlen(c_fName)+1), stdin);
g_pRecords[n_count].firstname = (char *)malloc((int)(strlen(c_fName)+1));
strcpy(g_pRecords[n_count].firstname, c_fName);
printf("Please enter Last Name: ");
fflush(stdin);
scanf("%s", c_lName, (int)(strlen(c_lName)+1), stdin);
g_pRecords[n_count].lastname = (char *)malloc((int)(strlen(c_lName)+1));
strcpy(g_pRecords[n_count].lastname, c_lName);
printf("Please enter ID: ");
fflush(stdin);
scanf("%d", &g_pRecords[n_count].id);
printf("Please enter mark: ");
fflush(stdin);
scanf("%f", &g_pRecords[n_count].mark);
}
}
g_numRecords++;
printf("Number of Records = %d\n", g_numRecords,
"ADDED A NEW RECORD TO MEMORY OK");
}
void DeleteRecord(void){
int n_count = 0;
StudentRecord *pt_tmpRecords;
pt_tmpRecords = (StudentRecord *)malloc(sizeof(StudentRecord)*g_numRecords);
for(n_count = 0; n_count < g_numRecords; n_count++){
pt_tmpRecords[n_count].firstname = (char *)malloc((int)(strlen(g_pRecords[n_count].firstname)+1));
strcpy(pt_tmpRecords[n_count].firstname , g_pRecords[n_count].firstname);
pt_tmpRecords[n_count].lastname = (char *)malloc((int)(strlen(g_pRecords[n_count].lastname )+1));
strcpy(pt_tmpRecords[n_count].lastname, g_pRecords[n_count].lastname);
pt_tmpRecords[n_count].id = g_pRecords[n_count].id;
pt_tmpRecords[n_count].mark = g_pRecords[n_count].mark;
}
for(n_count = 0; n_count < g_numRecords; n_count++){
free(g_pRecords[n_count].firstname);
free(g_pRecords[n_count].lastname);
}
g_numRecords--;
for(n_count = 0; n_count < g_numRecords; n_count++){
g_pRecords[n_count].firstname = (char *)malloc((int)(strlen(pt_tmpRecords[n_count].firstname)+1));
strcpy(g_pRecords[n_count].firstname , pt_tmpRecords[n_count].firstname);
g_pRecords[n_count].lastname = (char *)malloc((int)(strlen(pt_tmpRecords[n_count].lastname)+1));
strcpy(g_pRecords[n_count].lastname, pt_tmpRecords[n_count].lastname);
g_pRecords[n_count].id = pt_tmpRecords[n_count].id;
g_pRecords[n_count].mark = pt_tmpRecords[n_count].mark;
}
for(n_count = 0; n_count < g_numRecords+1; n_count++){
free(pt_tmpRecords[n_count].firstname);
free(pt_tmpRecords[n_count].lastname);
}
free(pt_tmpRecords);
printf("\nDELETED LAST RECORD IN MEMORY OK\n");
}
void SaveRecords(void){
int n_count = 0;
if((g_pf = fopen(g_FileName, "w+" )) != NULL){
for(n_count = 0; n_count < g_numRecords; n_count++){
fprintf(g_pf, "%s %s %d %5.2f\n",
g_pRecords[n_count].firstname,
g_pRecords[n_count].lastname,
g_pRecords[n_count].id,
g_pRecords[n_count].mark);
}
}
fclose(g_pf);
g_numRecords--;
while(g_numRecords >= 0){
free(g_pRecords[g_numRecords].firstname);
free(g_pRecords[g_numRecords].lastname);
g_numRecords--;
}
free(g_FileName);
_CrtDumpMemoryLeaks();
exit(EXIT_SUCCESS);
}
|