Mail Archives: djgpp/2004/06/01/08:45:16
X-Authentication-Warning: | delorie.com: mail set sender to djgpp-bounces using -f
|
From: | tonytg13 AT yahoo DOT com (Tony Ganchev)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | "delete" crash
|
Date: | 1 Jun 2004 05:43:26 -0700
|
Organization: | http://groups.google.com
|
Lines: | 573
|
Message-ID: | <a716df2e.0406010443.581cf241@posting.google.com>
|
NNTP-Posting-Host: | 80.72.80.78
|
X-Trace: | posting.google.com 1086093806 1026 127.0.0.1 (1 Jun 2004 12:43:26 GMT)
|
X-Complaints-To: | groups-abuse AT google DOT com
|
NNTP-Posting-Date: | Tue, 1 Jun 2004 12:43:26 +0000 (UTC)
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Guys, I have the following problem:
I have written a class, implementing dynamic linked list with
elements, containing strings. Unfortunately when I use the Remove
method of the GStringList class, my program crashes. I use DJGPP under
WinXP. I suppose the problem is some sort of segmentation although
there is nothing like SIGSEGV signal, the program just stops
execution. I have made sure that all of the elements are created using
"new". If anyone is eager to help me, here is the source
....................__TYPES.H..............................
#ifndef __TYPES_H_
#define __TYPES_H_
// A byte
typedef unsigned char BYTE;
// A word - 2 bytes
typedef unsigned short WORD;
// A double word - 4 bytes
typedef unsigned long DWORD;
#endif
....................__STRING.H.............................
#ifndef __STRING_H_
#define __STRING_H_
#include <string.h>
#include "__types.h"
//****************************************************************************
// GString: a string, representing a line of text in the editor. Has
many
// tools, useful for a text editor
//****************************************************************************
class GString
{
// Properties ********************************************************
char *pString; // Pointer to string in memory
public:
// Construction/Destruction
******************************************
// Constructor
GString() { pString = NULL; }
// Destructor
~GString() { Empty(); }
// Methods ***********************************************************
// Return the string held at the moment
char *Get() const { return pString; }
// Return the length of the string
WORD LengthGet() const { return strlen(pString); }
// Empties the string
void Empty();
// Copies string
GString &operator=(const GString strFrom);
GString &operator=(const char *pFrom);
// Add another string at the end
void StringAdd(const GString &str);
void StringAdd(const char *pStr);
// Concatenate the string with another one
friend GString operator+(const GString &str1, const GString &str2);
friend GString operator+(const GString &str, const char *pStr);
friend GString operator+(const char *pStr, const GString &str);
// Return selected character
char &operator[](const WORD nPos);
// Add character at the end of the string
void Add(const char c);
// Add character in the middle of the string
void Insert(const char c, const WORD nPos);
// Remove character from the string
void Remove(const WORD nPos);
};
//****************************************************************************
//****************************************************************************
// Implementation of GString
//****************************************************************************
//----------------------------------------------------------------------------
// Empties the string
//----------------------------------------------------------------------------
void GString::Empty()
{
if (pString != NULL)
delete [] pString;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Copies string
//----------------------------------------------------------------------------
// pFrom: the string to copy from
//----------------------------------------------------------------------------
// return: Copy of the new string for further assigning
//----------------------------------------------------------------------------
GString &GString::operator=(const char *pFrom)
{
Empty();
pString = new char[strlen(pFrom) + 1];
strcpy(pString, pFrom);
return *this;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Copies string
//----------------------------------------------------------------------------
// strFrom: the string to copy from
//----------------------------------------------------------------------------
// return: Copy of the new string for further assigning
//----------------------------------------------------------------------------
GString &GString::operator=(const GString strFrom)
{
return operator=(strFrom.Get());
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Add another string at the end
//----------------------------------------------------------------------------
// pStr: the string to copy from
//----------------------------------------------------------------------------
void GString::StringAdd(const char *pStr)
{
char *pTemp = new char[LengthGet() + strlen(pStr) + 1];
strcpy(pTemp, pString);
strcat(pTemp, pStr);
Empty();
pString = pTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Add another string at the end
//----------------------------------------------------------------------------
// str: the string to copy from
//----------------------------------------------------------------------------
void GString::StringAdd(const GString &str)
{
StringAdd(str.Get());
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Concatenates the string with another one
//----------------------------------------------------------------------------
// str1:
// str2: the string to copy
//----------------------------------------------------------------------------
// return: Copy of the new string for further assigning
//----------------------------------------------------------------------------
GString operator+(const GString &str1, const GString &str2)
{
GString strTemp;
strTemp = str1;
strTemp.StringAdd(str2);
return strTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Concatenates the string with another one
//----------------------------------------------------------------------------
// str :
// pStr: the string to copy
//----------------------------------------------------------------------------
// return: Copy of the new string for further assigning
//----------------------------------------------------------------------------
GString operator+(const GString &str, const char *pStr)
{
GString strTemp;
strTemp = str;
strTemp.StringAdd(pStr);
return strTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Concatenates the string with another one
//----------------------------------------------------------------------------
// pStr:
// str : the string to copy
//----------------------------------------------------------------------------
// return: Copy of the new string for further assigning
//----------------------------------------------------------------------------
GString operator+(const char *pStr, const GString &str)
{
GString strTemp;
strTemp = pStr;
strTemp.StringAdd(str);
return strTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Return selected character
//----------------------------------------------------------------------------
// nPos: position of the selected character
//----------------------------------------------------------------------------
char &GString::operator[](const WORD nPos)
{
return pString[nPos];
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Add character at the end of the string
//----------------------------------------------------------------------------
// c: character to add
//----------------------------------------------------------------------------
void GString::Add(const char c)
{
char *pTemp = new char[LengthGet() + 2];
strcpy(pTemp, pString);
pTemp[LengthGet()] = c;
pTemp[LengthGet() + 1] = 0;
Empty();
pString = pTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Add character in the middle of the string
//----------------------------------------------------------------------------
// c : character to add
// nPos: position at which to place
//----------------------------------------------------------------------------
void GString::Insert(const char c, const WORD nPos)
{
char *pTemp = new char [LengthGet() + 2];
if (nPos > 0)
strncpy(pTemp, pString, nPos);
pTemp[nPos] = c;
pTemp[nPos + 1] = 0;
strcat(pTemp, (char *)(pString + nPos));
Empty();
pString = pTemp;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Remove character from the string
//----------------------------------------------------------------------------
// nPos: the position to remove the character from
//----------------------------------------------------------------------------
void GString::Remove(const WORD nPos)
{
char *pTemp = new char [LengthGet()];
if (nPos > 0)
strncpy(pTemp, pString, nPos);
pTemp[nPos] = 0;
strcat(pTemp, (char *)(pString + nPos + 1));
Empty();
pString = pTemp;
}
//----------------------------------------------------------------------------
#endif
....................__SLIST.H.............................
#ifndef __SLIST_H_
#define __SLIST_H_
#include <malloc.h>
#include "__types.h"
#include "__string.h"
//****************************************************************************
// GStringNode: Element of the GStringList dynamic linked list
//****************************************************************************
class GStringNode
{
public:
GString strLine;
GStringNode *pNext;
};
//****************************************************************************
//****************************************************************************
// GStringList: A simple dynamic linked list to contain lines of text
//****************************************************************************
class GStringList
{
// Properties ********************************************************
GStringNode *pHead; // Pointer to first element in list
DWORD nCount; // Number of elements in list
public:
// Construction/Destruction
******************************************
// Constructor
GStringList();
// Destructor
~GStringList();
// Methods ***********************************************************
// Return pointer to selected node
GStringNode *Get(const DWORD nPos);
// Return the number of elements
DWORD CountGet() const { return nCount; }
// Return selected string
GString &operator[](const DWORD nPos);
// Add element at the end of the list
void Add(const GString &str);
// Insert element in the middle of the list
void Insert(const GString &str, const DWORD nPos);
// Remove an element
void Remove(const DWORD nPos);
// Remove all elements
void Clear();
};
//****************************************************************************
//****************************************************************************
// Implementation of GStringList
//****************************************************************************
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
GStringList::GStringList()
{
pHead = NULL;
nCount = 0;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------------
GStringList::~GStringList()
{
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Return pointer to selected node
//----------------------------------------------------------------------------
// nPos: position of the node
//----------------------------------------------------------------------------
GStringNode *GStringList::Get(const DWORD nPos)
{
printf("Requested %lu\n", nPos);
DWORD n;
GStringNode *pTemp = pHead;
if (nPos < nCount)
{
for (n = 1; n <= nPos; n++)
pTemp = pTemp->pNext;
return pTemp;
}
return NULL;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Return selected string
//----------------------------------------------------------------------------
// nPos: position of the node
//----------------------------------------------------------------------------
GString &GStringList::operator[](const DWORD nPos)
{
return Get(nPos)->strLine;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Add element at the end of the list
//----------------------------------------------------------------------------
// str: string of the element
//----------------------------------------------------------------------------
void GStringList::Add(const GString &str)
{
GStringNode *pTemp = new GStringNode;
pTemp->strLine = str;
pTemp->pNext = NULL;
if (nCount == 0)
pHead = pTemp;
else
Get(nCount - 1)->pNext = pTemp;
nCount++;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Insert element in the middle of the list
//----------------------------------------------------------------------------
// str : string of the element
// nPos: position of the node
//----------------------------------------------------------------------------
void GStringList::Insert(const GString &str, const DWORD nPos)
{
GStringNode *pTemp = new GStringNode;
pTemp->strLine = str;
if (nPos < nCount)
{
if (nPos == 0)
{
pTemp->pNext = pHead;
pHead = pTemp;
}
else
{
pTemp->pNext = Get(nPos);
Get(nPos - 1)->pNext = pTemp;
}
nCount++;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Remove an element
//----------------------------------------------------------------------------
// nPos: position of the node
//----------------------------------------------------------------------------
void GStringList::Remove(const DWORD nPos)
{
GStringNode *pTemp;
if (nPos < nCount && nCount > 0)
{
if (nCount == 1)
delete pHead;
else if (nPos == 0)
{
printf("------\n");
pTemp = pHead;
printf("......\n");
pHead = pHead->pNext;
printf(";;;;;;\n");
delete pTemp;
printf("//////\n");
}
else if (nPos == nCount - 1)
delete Get(nPos);
else
{
pTemp = Get(nPos + 1);
delete Get(nPos);
Get(nPos - 1)->pNext = pTemp;
}
nCount--;
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Remove all elements
//----------------------------------------------------------------------------
void GStringList::Clear()
{
}
//----------------------------------------------------------------------------
#endif
....................DOSED.CPP.............................
#include <conio.h>
#include <stdio.h>
#include "__string.h"
#include "__slist.h"
int main()
{
GStringList l;
GString s;
clrscr();
s = "Line1";
l.Add(s);
s = "Line2";
l.Insert(s, 0);
s = "Line3";
l.Insert(s, 1);
s = "Line4";
l.Insert(s, 2);
s = "Line5";
l.Insert(s, 3);
l.Remove(0);
printf("0: %s\n", l[0].Get());
printf("1: %s\n", l[1].Get());
printf("2: %s\n", l[2].Get());
printf("3: %s\n", l[3].Get());
return 0;
}
- Raw text -