Double list memory problem....Help: i am going mad!!!

  • Thread starter D. Frangiskatos
  • Start date
D

D. Frangiskatos

Ok, anybody who can help please do so. This is a Doubly
Linked List that is been used to store data in the form of
a queue. The strange thing is that it queues up to 961
entries. The the program will throw out a Debug Assertion
Failed, at line 346 of dgbheap.c , expression
_CrtCheckMemory().

The funny thing is that is not node data related. Meaning
a node might hold 1.6Kb or 65Kb or 100Kb and still it will
fail on the 961 new node. I do know what i have overlooked
but please if you can spot the reason for this error post
a reply.
Ps: i know that the error message is associated with
trying to free memory that its already been freed.

here is how the a new node is been added (the program
fails at packet_node *new_rec_ptr;)



................
head_ptr = NULL;

current_ptr = head_ptr;

packet_node *new_rec_ptr; // Declare temporary
pointer for the new node.


new_rec_ptr = new packet_node; // Allocate memory
for a new node and


// initialize
pointer to point to it.

char
long_buf[20];

/* store
the packet's label:

convert the long pkt id into a string and "label"
the packet*/
strcpy
(new_rec_ptr->packet_id,_ultoa( pkt_id, long_buf, 10 ));

/* store
the packet's size in order not to have to recalculate it*/

new_rec_ptr->packet_sz=BytesRecv;

/* now
store the entire packet headers+data=everything*/
memcpy
(new_rec_ptr->packet_cont,RecvBuffer, BytesRecv);

/*insert
into the list (id, bytes received and entire packet)*/
insert_node
(new_rec_ptr);

current_ptr = head_ptr;
.....................
............
.......


////////////////D.L.L ///////////////////////////
/*
****Doubly linked list for the TCP Packet analyzer

Start date 19-06-03

Ongoing revision ()
*/
// A better version of the initial doubly-linked list.


// compiler directives/includes
#include<fstream.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>

const int id_len=10;
const int p_len=1650; // Ethernet packet only 1500 Bytes
but

// lets give it a bigger value for test
purposes

// global structures and variables
struct packet_node
{

char packet_id[id_len];
long packet_sz; // we are not gonna have a
packet longer than 1500 in real life
char packet_cont[p_len];

packet_node *next;
packet_node *previous;
};

packet_node *head_ptr;
packet_node *current_ptr;


// function prototypes
void handle_choice(int choice);
void add_packet();
void insert_node(packet_node *new_rec_ptr);
packet_node *position_insertion_point(char pk_id[20]);
void make_node_new_head(packet_node *new_rec_ptr);
void add_node_to_end(packet_node *new_rec_ptr);
void move_current_to_end();
void display_list();
void delete_packet();
void delete_head_of_list();
void delete_end_of_list(packet_node *previous_ptr);
void delete_from_middle_of_list(packet_node *previous_ptr);
//int verify_delete();
void delete_node(packet_node *previous_ptr);
void delete_list();
void search_by_pk_id();
void display_next_packet();
void display_previous_packet();
void load_list_from_file();
void write_list_to_file();

// main function
/*int main()
{
int choice;

head_ptr = NULL;

//packet_node *new_rec_ptr;
//new_rec_ptr->packet_id
//insert_node(new_rec_ptr);


current_ptr = head_ptr;

do
{
cout << "\nPACKET LIST\n";
cout << "0 - Exit program\n";
cout << "1 - Add packet\n";
cout << "2 - Display all packets\n";
cout << "3 - Search for packet by packet id\n";
cout << "4 - Delete packet\n";
cout << "5 - Display next packet\n";
cout << "6 - Display previous packet\n";
cout << "Enter choice: ";
cin >> choice;
handle_choice(choice);
} while(choice != 0);
return 0;
} // end of main function
*/
// function to direct program flow based on user's choice
void handle_choice(int choice)
{
switch(choice)
{
case 0:
write_list_to_file();
if(head_ptr != NULL)
{ delete_list(); }
break;
case 1:
add_packet(); // add a packet to the linked list
break;
case 2:
display_list(); // display all packets in linked
list
break;
case 3:
search_by_pk_id(); // search for packet by pk_id
break;
case 4:
delete_packet(); // search for packet by pk_id
break;
case 5:
display_next_packet();
break;
case 6:
display_previous_packet();
break;
default :
cout << "\nInvalid choice\n";
break;
}
}

// function to add packet to the linked list
void add_packet()
{
packet_node *new_rec_ptr; // Declare temporary pointer
for the new node.

new_rec_ptr = new packet_node; // Allocate memory for a
new node and
// initialize pointer to
point to it.

cin.ignore(80,'\n');
cout << "\nEnter a new packet.\n";
cout << "Packet id: ";
cin.get(new_rec_ptr->packet_id,20);
cin.ignore(80,'\n');
//cout << "First Name: ";
//cin.get(new_rec_ptr->packet_sz,15);
// cin.ignore(80,'\n');
cout << "Packet contents: ";
cin.get(new_rec_ptr->packet_cont,15);
cin.ignore(80,'\n');

insert_node(new_rec_ptr);
}

// Function to insert new node into correct position in
list.
void insert_node(packet_node *new_rec_ptr)
{
packet_node *before_ptr;
packet_node *after_ptr;

if(head_ptr == NULL)
{ // If no nodes exist,
make the node
new_rec_ptr->next = NULL; // the head.
new_rec_ptr->previous = NULL;
head_ptr = new_rec_ptr;
}
else
{
if(strcmp(new_rec_ptr->packet_id, head_ptr-
packet_id) < 0)
{ // If new packet
comes before head,
make_node_new_head(new_rec_ptr); // make it the
new head.
}
else // Else,
determine where the new node
{ // should be
inserted.
current_ptr = position_insertion_point(new_rec_ptr-
packet_id);
before_ptr = current_ptr; // Use pointers to
keep track of nodes
after_ptr = current_ptr->next; // on each side of
the insertion point.

if(after_ptr == NULL) // If after_ptr is NULL, the
node needs to be
{ // added to the end of the
list.
add_node_to_end(new_rec_ptr);
}
else // Else add the node between
the nodes pointed to
{ // by before_ptr and
after_ptr.
before_ptr->next = new_rec_ptr;
new_rec_ptr->next = after_ptr;
new_rec_ptr->previous = before_ptr;
after_ptr->previous = new_rec_ptr;
}
}
}
}

// Function that positions current_ptr at the node before
the position
// where the new node should be inserted.
packet_node *position_insertion_point(char pk_id[20])
{
char temp_name[20];
packet_node *temp_ptr;
int tempint;

if(head_ptr->next != NULL) // If more than one node
exists, search the
{ // list for the correct
insertion point.
current_ptr = head_ptr;
temp_ptr = current_ptr->next;
strcpy(temp_name, temp_ptr->packet_id);
// Loop until the correct insertion point is found
tempint = strcmp(pk_id,temp_name);
while((tempint > 0) && (current_ptr->next !=NULL))
{
current_ptr = temp_ptr;

// check to see if current node is the last node
if(current_ptr->next != NULL)
{
temp_ptr = current_ptr->next;
strcpy(temp_name, temp_ptr->packet_id);
tempint = strcmp(pk_id,temp_name);
}
}
}
else // If only one node exists in the list, current_ptr
is the same
{ // as head_ptr. New node will be added to the end
of the list.
current_ptr = head_ptr;
}
return(current_ptr);
}

// Function that makes the node pointed to by new_rec_ptr
the new
// head of the linked list. It handles the special case of
inserting at
// the front of the list.
void make_node_new_head(packet_node *new_rec_ptr)
{
packet_node *temp_ptr;

temp_ptr = head_ptr;
temp_ptr->previous = new_rec_ptr;
new_rec_ptr->next = temp_ptr;
new_rec_ptr->previous = NULL;
head_ptr = new_rec_ptr;
}

// Function that adds a node to the end of the linked
list. It handles
// the special case of inserting at the end of the list.
void add_node_to_end(packet_node *new_rec_ptr)
{
new_rec_ptr->next = NULL; // Set next node pointer of
new node to NULL.

move_current_to_end(); // Make sure current_ptr
is at end of list.
current_ptr->next = new_rec_ptr; // Place new node at
the end of the list.
new_rec_ptr->previous = current_ptr;
}

// Function that moves current_ptr to end of the linked
list.
void move_current_to_end()
{
current_ptr = head_ptr; // Move current_ptr to head of
the list.

while(current_ptr->next != NULL)
{ // Traverse list until
NULL is reached.
current_ptr = current_ptr->next;
}
}

// Function that displays entire linked list.
void display_list()
{
// char name[36]; // used to combine names into one array

current_ptr = head_ptr; // Move current_ptr to head of
list.
if(current_ptr != NULL)
{
cout <<"packets......................"<< endl;

do
{

//cout << setw(12) << current_ptr->packet_cont <<
endl;
long indexer=0;
/*print each one of the array field*/
cout<< "Received packet start"<<endl;
//printf("Packet ID is:\n",current_ptr-
packet_id);
cout<<"Received packet ID: "<<current_ptr-
packet_id<<endl; for(indexer=0;indexer<current_ptr-
packet_sz;indexer++)
{
printf("%c", current_ptr-
packet_cont[indexer]);
}
cout<< "Received packet end"<<endl;
current_ptr = current_ptr->next; // set current_ptr
to point to next node
} while(current_ptr != NULL); // loop until end of list
}
else
{
cout << "\nNO packets TO DISPLAY\n";
}
current_ptr = head_ptr;
}

// Function that deletes individual nodes from the linked
list.
void delete_packet()
{
char search_string[20];
packet_node *previous_ptr;

previous_ptr = NULL; // initialize previous_ptr to NULL
current_ptr = head_ptr; // Move current_ptr to head of
list
// to begin search.

while((current_ptr != NULL) &&
(strcmp(current_ptr->packet_id, search_string) !
= 0))
{
previous_ptr = current_ptr;
current_ptr = current_ptr->next;
}

if(current_ptr != NULL) // If current_ptr is not NULL,
then match was
{ // found.
cout << "\npacket FOUND\n";
cout said:
packet_id << endl;
cout << current_ptr->packet_cont << endl;
delete_node(previous_ptr);

/* if(verify_delete())
{
delete_node(previous_ptr);
cout << "\PACKET DELETED\n";
}
else
{
cout << "\nPACKET NOT DELETED\n";
}
*/
}
else
{
cout << "\nNO MATCH FOUND. NO PACKET DELETED.\n";
}
}
///////////////////////////////////////////////////////////
//////////////////////////////////////

// Function to ask user to verify intention to delete the
node.
/*int verify_delete()
{
char YesNo;

cout << "\nDo you wish to delete this packet? (Y/N) ";
cin >> YesNo;
if((YesNo == 'Y') || (YesNo == 'y'))
{
return(1);
}
else
{
return(0);
}
}
*/
///////////////////////////////////////////////////////////
/////////////////////////////

// Function that deletes node pointed to by current_ptr.
void delete_node(packet_node *previous_ptr)
{
if(current_ptr == head_ptr)
{
delete_head_of_list();
}
else
{
if(current_ptr->next == NULL)
{
delete_end_of_list(previous_ptr);
}
else
{
delete_from_middle_of_list(previous_ptr);
}
}
}

//Function that deletes the head of the list.
void delete_head_of_list()
{
current_ptr = head_ptr;
if(head_ptr->next != NULL)
{
head_ptr = current_ptr->next;
head_ptr->previous = NULL;
}
else
{
head_ptr = NULL;
}
delete current_ptr;
}

// Function that deletes the last node of the linked list.
void delete_end_of_list(packet_node *previous_ptr)
{
delete current_ptr;
previous_ptr->next = NULL;
current_ptr = head_ptr; // Set current_ptr to head to
give it a value.
}

// Function that deletes a node from the middle of the
list.
void delete_from_middle_of_list(packet_node *previous_ptr)
{
previous_ptr->next = current_ptr->next;
current_ptr->next->previous = previous_ptr;
delete current_ptr;
current_ptr = head_ptr;
}

// Function that frees the memory used by the linked list.
void delete_list()
{
packet_node *temp_ptr; // pointer used for temporary
storage

current_ptr = head_ptr; // Move current_ptr to head of
the list.

do // Traverse list until the end is reached.
{
temp_ptr = current_ptr->next; // Set temporary
pointer to point

// to
the remainder of the list.
delete current_ptr; // Delete current node.
current_ptr = temp_ptr; // Set current_ptr to next
node after the
} while(temp_ptr != NULL); // deleted one.
}

// Function that searches the linked list for the first
occurrence of a given
// packet and displays the packet to the screen.
void search_by_pk_id()
{
char search_string[20]; // Character array for packet
id to search for.

current_ptr = head_ptr; // Move current_ptr to head of
list
// to begin search.
cin.ignore(80,'\n');
cout << "\nEnter the packet's id for which you want to
search: ";
cin.get(search_string,20);
cin.ignore(80,'\n');

while((current_ptr != NULL) &&
(strcmp(current_ptr->packet_id, search_string) !
= 0))
{
current_ptr = current_ptr->next;
}

if(current_ptr != NULL) // If current_ptr is not NULL,
then match was
{ // found.
cout << "\nPACKET FOUND\n";
cout << current_ptr->packet_sz << ' '
<< current_ptr->packet_id << endl;
cout << current_ptr->packet_cont << endl;
}
else
{
cout << "\nNO MATCH FOUND\n";
}
}

// Function that allows the user to browse through packets
by displaying
// the next packet in the linked list.
void display_next_packet()
{
if(current_ptr != NULL) // check to make sure packets
exist
{
if(current_ptr->next != NULL) // If
current_ptr is not
{ // already at
the end of the
current_ptr = current_ptr->next; // list, move
current_ptr
cout << current_ptr->packet_sz << ' ' // forward in
the list and
<< current_ptr->packet_id << endl; // display
the data in the next
cout << current_ptr->packet_cont << endl; // node.
}
else // Otherwise,
display message
{ // to alert
user that the last
cout << "\nLAST packet\n"; // packet has
been reached and
cout << current_ptr->packet_sz << ' ' // display the
last packet
<< current_ptr->packet_id << endl; // again.
cout << current_ptr->packet_cont << endl;
}
}
else
{
cout << "\nNO packets\n";
}
}

// Function that allows the user to browse through packets
by displaying
// the previous packet in the linked list.
void display_previous_packet()
{
if(current_ptr != NULL) // check to make sure packets
exist
{
if(current_ptr->previous != NULL) // If
current_ptr is not
{ // already at
the beginning of
current_ptr = current_ptr->previous; // the list,
move current_ptr
cout << current_ptr->packet_sz << ' ' // backward in
the list and
<< current_ptr->packet_id << endl; // display
the data in the
cout << current_ptr->packet_cont << endl; // previous
node.
}
else // Otherwise,
display message
{ // to alert
user that the first
cout << "\nPacket\n"; // packet has been
reached and
cout << current_ptr->packet_sz << ' ' // display the
first packet
<< current_ptr->packet_id << endl; // again.
cout << current_ptr->packet_cont << endl;
}
}
else
{
cout << "\nNO packets\n";
}
}

/* Function to load the linked list from the data file.*/
void load_list_from_file()
{
packet_node *new_rec_ptr;
new_rec_ptr->packet_id;
insert_node(new_rec_ptr);

ifstream infile;

infile.open("PACKETS.DAT",ios::in); // open file for
input

if (infile) // If no error occurred while opening file
{ // input the data from the file.
do
{
new_rec_ptr = new packet_node;
infile.get(new_rec_ptr->packet_id,id_len);
infile.ignore(80,'\n');
char
long_buff[20];

/*convert
the long pkt id into a string and "label" the packet*/
//strcpy
(new_rec_ptr->packet_id,_ultoa( long_buff, 10 ));

infile.get(_ultoa(new_rec_ptr->packet_sz,
long_buff, 10 ),20);
infile.ignore(80,'\n');
infile.get(new_rec_ptr->packet_cont, p_len);
infile.ignore(80,'\n');

if(strcmp(new_rec_ptr->packet_id, "END OF
FILE") != 0)
{
insert_node(new_rec_ptr);
}
else
{
delete new_rec_ptr;
new_rec_ptr=NULL;
}
} while(new_rec_ptr!=NULL && strcmp(new_rec_ptr-
packet_id,"END OF FILE") != 0);
infile.close();
}
else // If error occurred, display message.
{
cout << "No usable data file located. List is
empty.\n";
}
}

/* Function to write linked list data to the data file.*/
void write_list_to_file()
{
ofstream outfile;

outfile.open("Packets.dat",ios::blush:ut); // open file for
output

if (outfile)
{
current_ptr = head_ptr;
if(head_ptr != NULL)
{
do // Traverse list until the end is reached.
{
outfile << current_ptr->packet_id << endl;
outfile << current_ptr->packet_sz << endl;

long indexer=0; //for every packet declare new
indexer

for(indexer=0;indexer<current_ptr-
packet_sz;indexer++)
{
//printf("%c", current_ptr-
packet_cont[indexer]); outfile<<current_ptr-
packet_cont[indexer];
} //end of the loop of going
through the packet's contents
outfile<<endl; //after the
packet throw a blank line in the file

current_ptr = current_ptr-
} while(current_ptr != NULL);
}
outfile << "END OF FILE" << endl;
outfile.close();
}
else
{
cout << "Error opening file.\n";
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top