Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Memory Leaks in my C Program.
Index -> Programming, C -> C Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Natsu




PostPosted: Mon Oct 15, 2012 11:21 am   Post subject: Memory Leaks in my C Program.

I got a simple program that reads and write's from text files. It works perfectly but apparently has memory leaks (with feedback from my prof). I am using this to check for mem leaks

code:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

_CrtDumpMemoryLeaks();


Every time I re-run it, I get diff amount of mem leaks on diff lines. Can I get help to make sure this program is mem leak free? I checked all my malloc's and free's and it seems to be all correct. I just need to make sure its mem-leak free, cuz the mem leak checker always gives me diff results.



Ass0.c
 Description:

Download
 Filename:  Ass0.c
 Filesize:  10.99 KB
 Downloaded:  1159 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Mon Oct 15, 2012 6:50 pm   Post subject: RE:Memory Leaks in my C Program.

Try using valgrind to tell you what you're losing. There should be plenty of guides to using it with your IDE/platform of choice.
Natsu




PostPosted: Mon Oct 15, 2012 6:51 pm   Post subject: RE:Memory Leaks in my C Program.

I want to valgrind's website and I cant find the tool to download to check my mem leaks?

Isent there a way to check just by looking at the code?
md




PostPosted: Mon Oct 15, 2012 6:52 pm   Post subject: RE:Memory Leaks in my C Program.

Also, hungarian notation is the work of the devil.
Natsu




PostPosted: Mon Oct 15, 2012 6:54 pm   Post subject: RE:Memory Leaks in my C Program.

I am not understanding.
Dreadnought




PostPosted: Mon Oct 15, 2012 7:02 pm   Post subject: Re: Memory Leaks in my C Program.

Here's a quick list of calls to malloc and free.

malloc:
C:
g_FileName = (char *) malloc (Filelength);
student = (pStudentRecord) malloc(sizeof(**g_ppRecords));
temp = (pStudentRecord*) malloc(sizeof(pStudentRecord)*g_numRecords);
fname = (char *) malloc(strlen(stri)+1);
student->firstName = (char *) malloc(strlen(fname)+1);
lname = (char *) malloc(strlen(stri)+1);
student->lastName = (char *) malloc(strlen(lname)+1);
lname = (char *) malloc(strlen(stri)+1);
student = (pStudentRecord) malloc(sizeof(**g_ppRecords));
temp = (pStudentRecord*) malloc(sizeof(pStudentRecord)*g_numRecords);
student->firstName = (char*) malloc(sizeof(char)*strlen(fname)+1);
student->lastName = (char*) malloc(sizeof(char)*strlen(lname)+1);
temp = (pStudentRecord*) malloc(sizeof(pStudentRecord)*g_numRecords);


free:
C:
free(g_ppRecords);
free(g_FileName);
free(g_ppRecords);
free(g_ppRecords);


Try to make sure that for every call to malloc there is a call to free that frees that memory (this doesn't necessarily mean that you need to have the same number of calls).

Also, just in case you didn't know.
C:
struct {
    int * ptr;
} *exampleA, *exampleB;

exampleA->ptr = malloc(sizeof(int));
exampleB->ptr = malloc(sizeof(int));

... // some code

free(exampleA); // causes a memory leak since the integer at ptr was not freed

free(exampleB->ptr);
free(exampleB); //does not cause a memory leak
Natsu




PostPosted: Mon Oct 15, 2012 7:15 pm   Post subject: RE:Memory Leaks in my C Program.

Whenever I add more free's to match the # of malloc's sometimes the mem leaks increase and sometimes they decrease. Its kinda weird.
Dreadnought




PostPosted: Mon Oct 15, 2012 7:35 pm   Post subject: Re: Memory Leaks in my C Program.

Are you always using the same file? Does your program modify the file in some way when you run it?
Sponsor
Sponsor
Sponsor
sponsor
Natsu




PostPosted: Mon Oct 15, 2012 9:04 pm   Post subject: RE:Memory Leaks in my C Program.

Yes I am using the same file.
bl0ckeduser




PostPosted: Mon Oct 15, 2012 9:14 pm   Post subject: Re: Memory Leaks in my C Program.

Hi. Several things are wrong with your code. Here are a few.
I hope this helps.

code:

strcat(g_FileName,"\0");


code:

strcat(fname,"\0");


It doesn't work that way. In a nutshell, your string has to be null-terminated to start with for strcat to find the end of it where it appends the other string from.
You should seriously learn or relearn how C strings work before attempting to use routines like these.

code:

if(mystring=="")


You can't compare strings directly like that in C
See e.g. http://codepad.org/K9auBejj for an example of what happens when you do that.
See http://c-faq.com/charstring/stringeq.html for an explanation.

You copy the local char Filename [50] to a global char*, which you
then use only in the routine where char Filename [50] exists. You then
proceed to free the char* before using it as an argument to fopen.
This makes absolutely no sense.

Your code needs to be more structured, like your teachers'.
It's good to plan out ahead of time what your program will look like.

Here are some resources:
- http://c-faq.com/
- the comp.lang.c newsgroup
- the book "The C Programming Language"
- ##c channel on freenode IRC
- http://publications.gbdirect.co.uk/c_book/
QuantumPhysics




PostPosted: Mon Oct 15, 2012 9:30 pm   Post subject: RE:Memory Leaks in my C Program.

Hey md, thanks a lot for suggesting the valgrind. I was also looking for something similar. Heh, lucky I read this post.
Natsu




PostPosted: Tue Oct 16, 2012 5:30 pm   Post subject: RE:Memory Leaks in my C Program.

The problem I am trying to fix now is to remove all mem leaks, other then that my prof said m code is perfect.
Insectoid




PostPosted: Tue Oct 16, 2012 5:47 pm   Post subject: RE:Memory Leaks in my C Program.

Syntactically, maybe, but not logically.
Natsu




PostPosted: Tue Oct 16, 2012 7:15 pm   Post subject: RE:Memory Leaks in my C Program.

Yes I agree, now I am looking for why how come I cant free everything correctly. Visual Studio always gives me diff amount of mem leaks error with that tool checker. I was wondering if anyone can take al ook into it to see it?
Insectoid




PostPosted: Tue Oct 16, 2012 7:32 pm   Post subject: RE:Memory Leaks in my C Program.

If you free the wrong thing, or free things in the wrong order, you can cause more memory leaks. See Dreadnought's example.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: