Jump to content

Check this out if you are interested in how memory works :)


disturbed

Recommended Posts

...this is a little C++ program I worked on some time ago - it simulates a typical memory leak that could happen if a program is not written with respect to good memory management

What it does, is it creates a linked list of 10,000 nodes in it, containing some data (like name, phone number, email address...) - it prints out the record number (0-10,000) and a generated record id

In the first instance of the program: Project2 - Leak - the program itself is missing a destructor - which is supposed to be part of the memory management - it basically destroys each node after it is processed - as nodes are added into memory - it leaks - your computer runs out of physical memory and the program starts using the oh famous page file - the program might crash ur computer if you have your page file set up too low - no worries, it doesnt harm it (just make sure you have everything saved before you run it)

In the 2nd instace of the program: Project1 - No Leak - the program uses the correct memory management, processes each linked list node (contact record) and it deletes it - the memory is not leaked and everything is fine :)

Source code: for those who are interested:

/* Warning:    Close all other applications before debugging this program.

   You may have to restart your computer after running the program.    */

#include <iostream>

#include <string.h>

#include <time.h>

using namespace std;

// forward declaration

int insert(void);

void deletion(void);

// global variables

int IDgenerator = 0;

class contact {

private:

char name[30];

char phone[20];

char email[30];

contact *next;

int userId;

public:

contact() {}

contact(char* name, char* phone, char* email){

strcpy(this->name, name);

strcpy(this->phone, phone);

strcpy(this->email, email);

userId = IDgenerator++;

this->next = NULL;

}

contact* getNext() { return next; }

void setNext(contact* nx) { next = nx; }

                                //This is the destructor method - uncoment it and it collects the

                                //unused memory - leak is fixed

//~contact(void)

//{

// if(getNext() != NULL )

// delete getNext();

//}

} *head = NULL; // declare head pointer as global

int main() { 

int i = 0, j = 0;

clock_t start =  clock( );

while (i<10000) {

for (j = 0 ; j<1000; j++) {

insert(); // insert 1000 records

}

deletion();

i++;

cout << i << " - " << IDgenerator << endl;

//deletion();

}

clock_t end =  clock( );

cout << "time use = " << (end - start) / CLOCKS_PER_SEC << "seconds"<<endl;

return 0;

}

int insert() {

contact *node;

char sname[30] = "John", sphone[20] = "1234 567", semail[30] = "[email protected]";

node = new contact(sname, sphone, semail);

if (node == NULL) {

cout << "ERROR - Could not allocate memory !" << endl;

return -1;

}

node->setNext(head);

head = node;

return 0;

}

void deletion() {

delete head;

head = NULL;

}

For those of you who would like to run the program - feel free :) I can assure you it wont harm ur system in any way - if you will run the memory leak program, make sure you are not running anything important (because your computer might crash if you have don't have the suffiecient memory)

http://www.1hitkill.com/dfiStuff/Project2-Leak.exe

http://www.1hitkill.com/dfiStuff/Project1-NoLeak.exe

Link to comment
Share on other sites

to shug - i have an explenation of what the programs do in my 1st post

to FellowEarth - the only difference in code is that you have to uncomment this

      //~contact(void)

      //{

      //   if(getNext() != NULL )

      //      delete getNext();

      //}

so basically just remove the // from those 4 lines and you have a working code for the one that does not leak memory

the line delete getNext(); is actually recursive - c++ automatically calls the destructor method again every time it sees the word delete - and it will keep executing that method untill it reaches NULL - which would mean it reached the end of the linked list

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...