Sign in to follow this  
DMINATOR

C and _CrtIsValidHeapPointer error

Recommended Posts

Well I was trying to solve my problems for atleast 2 weeks, but haven't made a single step to fixing it, I made about 3 versions of my structered links, and tryed to made them simplier all the time. But my lack of understanding the way my structures work , granted me with error filled program. Well the program is not easy for me to debug, for any one other trying to get the idea how it works it would be even harder. Here is the basic idea : I receve the text from server like this
Quote:
:wineasy2.se.quakenet.org 001 DMINASTAH :Welcome to the Internet Relay Network, DMINASTAH :wineasy2.se.quakenet.org 002 DMINASTAH :Your host is wineasy2.se.quakenet.org, running version u2.10.11.04+asuka(1.0.6a) :wineasy2.se.quakenet.org 003 DMINASTAH :This server was created Thu Apr 29 2004 at 19:13:41 CEST :wineasy2.se.quakenet.org 004 DMINASTAH wineasy2.se.quakenet.org u2.10.11.04+asuka(1.0.6a) dioswkgxXInR biklmnopstvrDcCNu bklov :wineasy2.se.quakenet.org 005 DMINASTAH WHOX WALLCHOPS WALLVOICES USERIP CPRIVMSG CNOTICE SILENCE=15 MODES=6 MAXCHANNELS=20 MAXBANS=45 NICKLEN=15 TOPICLEN=250 AWAYLEN=160 KICKLEN=250 :are supported by this server :wineasy2.se.quakenet.org 005 DMINASTAH CHANTYPES=#& PREFIX=(ov)@+ CHANMODES=b,k,l,imnpstrDcCNu CASEMAPPING=rfc1459 NETWORK=QuakeNet :are supported by this server :wineasy2.se.quakenet.org 251 DMINASTAH :There are 62837 users and 140995 invisible on 41 servers :wineasy2.se.quakenet.org 252 DMINASTAH 77 :operator(s) online :wineasy2.se.quakenet.org 253 DMINASTAH 71 :unknown connection(s) :wineasy2.se.quakenet.org 254 DMINASTAH 190970 :channels formed :wineasy2.se.quakenet.org 255 DMINASTAH :I have 8736 clients and 1 servers :wineasy2.se.quakenet.org NOTICE DMINASTAH :Highest connection count: 9005 (9004 clients) :wineasy2.se.quakenet.org 375 DMINASTAH :- wineasy2.se.quakenet.org Message of the Day - :wineasy2.se.quakenet.org 372 DMINASTAH :- 2004-10-16 16:45 :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [ wineasy.se.quakenet.org ] **************************************** :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- Welcome to the QuakeNet IRC Network! :wineasy2.se.quakenet.org 372 DMINASTAH :- _ :wineasy2.se.quakenet.org 372 DMINASTAH :- _ __(_)___ ___ ____ ________ __ :wineasy2.se.quakenet.org 372 DMINASTAH :- | | /| / / / __ \/ _ \/ __ `/ ___/ / / / :wineasy2.se.quakenet.org 372 DMINASTAH :- | |/ |/ / / / / / __/ /_/ (__ ) /_/ / :wineasy2.se.quakenet.org 372 DMINASTAH :- |__/|__/_/_/ /_/\___/\__,_/____/\__, / :wineasy2.se.quakenet.org 372 DMINASTAH :- s e . q u a k e n e t . o r g /____/ :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- You are connected to wineasy.se.quakenet.org. :wineasy2.se.quakenet.org 372 DMINASTAH :- Available ports are 6667, 6668, 6669, 6670 and 7000. :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- This server is located at WinEasy AB, Stockholm. :wineasy2.se.quakenet.org 372 DMINASTAH :- :wineasy2.se.quakenet.org 372 DMINASTAH :- ** [Local IRC operators ] *********************************************
What i want' to do is to store each line of text in an easy and fast accesible way like that
Quote:
line[0] - :wineasy2.se.quakenet.org 001 DMINASTAH :Welcome to the Internet Relay Network, DMINASTAH line[1] - :wineasy2.se.quakenet.org 002 DMINASTAH :Your host is wineasy2.se.quakenet.org, running version u2.10.11.04+asuka(1.0.6a) ...
And to be able to acces each word from a line by doing something like
Quote:
line[0][0] - :wineasy2.se.quakenet.org line[0][1] - 001 line[0][2] - DMINASTAH
That's why I did try to use structres to create basic fifo list I have following structures: //This basicly points to first line in line list //also it points to last line to easily add new line to the end typedef struct LINEHEAD { //number of lines int count; LINE* head; LINE* last; }; //here is line of words //it stores 2d array of words //and also points to next line typedef struct LINE { //number of words int count; //array of words char** words; LINE* next; }; well everything seems not really hard but my code gets really messy :( here is main.c
#include <stdio.h>
#include <crtdbg.h>

#include "fifostr.h"

#define LINE1 "test string number 1\n test2 string2 number2 2\n"
//test lines
LINEHEAD* test;


int main()
{

	CreateLines(&test);

	AddLines(&test,LINE1,strlen(LINE1) + 1);

	ShowLines(&test,0);
	ShowLines(&test,1);
	ShowLines(&test,2);

	

	FreeLines(&test);

	//testing memory leaks
	if(_CrtDumpMemoryLeaks() == 0)
	printf(" MEMORY OK\n");
	else
		printf(" MEMORY LEAKS FOUND\n");
	return 0;
}




here is fifostr.c
#include <stdio.h>
#include "fifostr.h"

/*
typedef struct LINE LINE;
typedef struct LINEHEAD LINEHEAD;

typedef struct LINEHEAD
{
	//number of lines
   int count;

   LINE* head;
   LINE* last;
};

//here is line of words
typedef struct LINE
{

   //number of words
   int count;

   //array of words
   char** words;

   LINE* next;

};
*/


//special struct used when adding new strings
typedef struct CHARLIST
{
	//pointer to an array of chars
    char* word;

	//next word
	struct CHARLIST *next;
};


struct CHARLIST *crTemp;
struct CHARLIST *crLast;     //a handy pointer to last element
int crCount;                //counts numbe of words we added

void CreateLines(LINEHEAD **input)
{
   LINEHEAD *temp;

   //allocating memory for temp value
   temp = (LINEHEAD*)malloc(sizeof(LINEHEAD));

   temp->count = 0;
   temp->last = NULL;
   temp->head = temp->last;

   //creating temp variable
   crTemp = (struct CHARLIST*)malloc(sizeof(struct CHARLIST));

   crTemp->next = NULL;
   crTemp->word = NULL;
   crLast = crTemp;
   crCount = 0;

   *input = temp;
}

void FreeLines(LINEHEAD **input)
{
   int i = 0;
   LINE *temp;
   LINE *swap; //used for temp storing of pointer


    //first we check for null
   // and then use temp value;
   if(*input == NULL)
   return;

   //now using our head to go through lines
   temp = (*input)->head;

   //and let's free the input struct
   free(*input);

   while(temp != NULL)
   {
       //now we free each word individually 
	   for(i = 0; i < temp->count;i++)
	   {
          if(temp->words[i] != NULL)
		  free(temp->words[i]);
	   }
       //now free the whole words array
	   free(temp->words);


	   //now let's update our position to next in line
       swap = temp->next;
	   free(temp);
	   temp = swap;
   }
   //now once we are done
   //let's clear all the remaining info

 //  free(crLast);
//   free(crTemp);
   
   temp = NULL;
   swap = NULL;
}


void AddLines(LINEHEAD **input,char* string,int length)
{
	int i = 0;
	int j = 0;
	int k = 0;

	char* buffer;
	char c;           //current char reading
	struct CHARLIST *adder;    //new element if needed
	struct CHARLIST *point;    //temporary pointer
	
	buffer = (char*)malloc(sizeof(char) * 1024);
	memset(buffer,0,1024);
	
	
	//we read each line
	//and then just save the pointer
	for(i = 0; i < length;i++)
	{
		c = string[i];
		
		//now we save only these chars wich are needed
		switch(c)
		{
		
		case '\0':
		case ' ':
			    if(j > 0)
				{

				//adding new 
                adder = (struct CHARLIST*)malloc(sizeof(struct CHARLIST));

				adder->next = NULL;

                //now let's point this word to our list of chars
			    crLast->word = buffer;


				//now let's add new line to the next
			    crLast->next = adder;
				
				//now let's re point last element 
				crLast = crLast->next;

				//indicates number of string we read
				crCount++;

				j = 0;

				//and clearing string
				buffer = (char*)malloc(sizeof(char) * 1024);
				memset(buffer,0,1024);
				}
			    break;
		
		case '\n':
                //if we reached this point than we must add lines to our main structure
			 
			    //we know exact number of lines so
			    // we create an array
               
			    //a new line we neex

			    if(j > 0)
				{

				//adding new 
                adder = (struct CHARLIST*)malloc(sizeof(struct CHARLIST));

				adder->next = NULL;
                
				//now let's point this word to our list of chars
			    crLast->word = buffer;

				//now let's add new line to the next
			    crLast->next = adder;
				
				//now let's re point last element 
				crLast = crLast->next;

				free(adder);
				//indicates number of string we read
				crCount++;

				j = 0;

				//and clearing string
				buffer = (char*)malloc(sizeof(char) * 1024);
				memset(buffer,0,1024);

				}
			  
			    (*input)->last = (LINE*)malloc(sizeof(LINE));

				if((*input)->count == 0)
				{
                (*input)->head = (*input)->last;
				}

				 //setting the number of words used
				k = crCount;

				//number of last words
				 (*input)->last->count = k;

				 //creating 2d array
				 (*input)->last->words  = (char**)malloc(k * 1024);

				 //should be numm
                 (*input)->last->next = NULL;
				
				 //let's point our temp value to already saved
                 point = crTemp;

				 //increase linec counter
				 (*input)->count++;

				//now let's point pointers to new location
			    for(k = 0 ; k < crCount; k++)
				{
                   (*input)->last->words[k] = point->word;
                   

				 
				   //let's point to next one
				   point = point->next;

				     //let's free the struct
				   free(crTemp);

				   //and set it to next one
				   crTemp = point;
				}

				//let's free charlist

			    free(point);
				
			    break;
			
		default:
			
			    buffer[j++] = string[i];
			    break;
		}
	}

	free(buffer);
	//now there maybe some problems if the line is finished
	//this should be taked care by \0 symbol
}


void ShowLines(LINEHEAD **input,int pos)
{
   int i = 0;
   LINE *temp;

   if(*input == NULL)
   {
      printf("NULL\n");
      return;
   }

   temp = (*input)->head;

   for( i = 0; i < pos; i++)
   {
       if(temp == NULL)
	   {
          printf("NOT FOUND\n");
	      return;
	   }
	   temp = temp->next;
   }

   if(temp == NULL)
   {
     printf("NOT FOUND\n");
	 return;
   }
   //if we reached here then we have found exact number

   for( i = 0;i< temp->count;i++)
   {
	  if(temp->words[i] != 0)
      printf("%s ",temp->words[i]);
	  else
		  printf("NULL");
   }
   printf("\n");
}




CHARLIST is basic class that i use to store all the lines until '\n' is found and pass the pointers to new line Well if I am trying to add 1 line to the list like "test string number " Then everyithnig is fine, but if I try add a line like "test string number 1\n test2 string2 number2 2\n" I do receve _CrtIsValidHeapPointer error after the first '\n' ,and when trying to allocate memory for new line with malloc adder = (struct CHARLIST*)malloc(sizeof(struct CHARLIST)); Here is where I receve error and I am really confused. I know the code is nasty. But if anyone could provide me with any help how to improve/rewrite my code or use any other technique, I would be really thankfull. Thank's in advance. [Edited by - DMINATOR on December 3, 2004 10:42:50 AM]

Share this post


Link to post
Share on other sites
First, I'd suggest a slightly different storage mechanism:


struct WordList
{
unsigned short NumWords;
char **WordArray;
}

struct LineList
{
unsigned short NumLines;
WordList *LineArray;
}



You will need a few functions to add and remove lines from the arrays, and allocate memory for each item. By simplifying it to just a couple of arrays (basically) you'll cut out some complications with the linked list that aren't really necessary - and it will be much easier to get, say, the third word in line 25 since you won't have to traverse the linked list:

printf("Word: %s\n", LineList.LineArray[25].WordArray[3]);



Also, you can use functions like sscanf() to easily break up a line into words. This will eliminate potential bugs in your line parser code.


Of course, if using C++ is an option, you can switch to using std::string and save yourself a lot of time and headaches.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this