text buffer problem
I''m creating a text buffer for use in an opengl project, and it works flawlessly except for one thing - I''m unable to delete nodes in the linkedlist without getting errors. If I take the "delete" lines out, the program runs flawlessly. Here''s a copy of the include file, can anybody figure out where the error''s coming from?
#ifndef _TEXTCLASS_H_
#define _TEXTCLASS_H_
const int STRLIFE = 1000;
const int MAXSTR = 11;
struct String
{
String* next;
char* Text;
String(char *text)
{
Text = new char;
strcpy(Text, text);
}
};
class Text
{
public:
String* head;
String* current;
String* prev;
int counter;
Text()
{
head = NULL;
counter = 0;
}
~Text()
{
current = head;
while(current != NULL)
{
prev = current;
current = current->next;
delete prev;
}
}
int numStrings()
{
current = head;
for(int c=0; current!=NULL; c++)
current = current->next;
return c;
}
void chopHead()
{
if(head!=NULL)
{
current = head->next;
delete head;
head = current;
}
}
void addString(char *string)
{
String *New;
New = new String(string);
New->next = NULL;
if(head == NULL)
{
head = New;
}
else
{
current = head;
while(current!=NULL)
{
prev = current;
current=current->next;
}
prev->next = New;
}
if(numStrings()>=MAXSTR)
chopHead();
}
char *getString(int index)
{
current = head;
for(int c=0; cnext;
}
if(c==index)
return current->Text;
else
return "FAILURE";
}
void Progress()
{
counter++;
if(counter>=STRLIFE)
{
counter = 0;
chopHead();
}
}
};
#endif
You should post the errors you''re getting as well
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Good point. The error occurs in the lines that delete nodes:
delete prev;
delete head;
etc...etc...
The error reads as follows:
The instruction at [memory address] referenced memory at [memory address]. The memory could not be "written".
Also, if you think you can help and this is simply too illegible, give me your email and I''ll send you the file instead.
delete prev;
delete head;
etc...etc...
The error reads as follows:
The instruction at [memory address] referenced memory at [memory address]. The memory could not be "written".
Also, if you think you can help and this is simply too illegible, give me your email and I''ll send you the file instead.
No need for an email
. Just specify what the memory addresses are as well - they''re sometimes kinda important (like if the second one is 0 then you''re trying to delete a NULL pointer)

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Deleting a null pointer is a no-op, so that can''t be the problem.
// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
~nz
// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
~neoztar "Any lock can be picked with a big enough hammer"my website | opengl extensions | try here firstguru of the week | msdn library | c++ faq lite
Your String class only allocates one byte for each string. This would only work for zero length strings as each string has a zero terminator. What you really want is:
Text = new char[strlen(text)+1];
Most likely this is the source of your errors.
You don't have a destructor for the String class. Thus the memory allocated for Text is never freed. You need:
String::~String()
{
delete [] Text;
}
Suggested Improvements:
1) In your Text class, add a counter that increments by 1 for every String you add, decrements by 1 for each delete. Then you don't have to count the number of strings in numStrings() - you would simply return the counter value.
2) Since you always append to the end of the list, have a pointer that points to the LAST item added. Then you don't have to traverse the list to the end of it.
ie:
class Text
{
public:
String* head,* tail;
....
void addString(char * string)
{
String * New=new String(string);
New->next=NULL;
if (head==NULL)
head=tail=New;
else {
tail->next=New;
}
tail=New;
}
Oh.. just a suggestion: Temporaries such as
String * current;
String * prev;
are better defined in the routines you use them in and not as part of the class definition. Right now, they occupy space in the Text class yet the values don't need to be stored for the whole life of the Text class. They only need to exist in the scope of the routines using them. This saves on memory. This is not really a huge issue in this case since the number of occurances of the Text class would be rather small anyways.
[edited by - JotDot on October 15, 2003 8:44:04 AM]
Text = new char[strlen(text)+1];
Most likely this is the source of your errors.
You don't have a destructor for the String class. Thus the memory allocated for Text is never freed. You need:
String::~String()
{
delete [] Text;
}
Suggested Improvements:
1) In your Text class, add a counter that increments by 1 for every String you add, decrements by 1 for each delete. Then you don't have to count the number of strings in numStrings() - you would simply return the counter value.
2) Since you always append to the end of the list, have a pointer that points to the LAST item added. Then you don't have to traverse the list to the end of it.
ie:
class Text
{
public:
String* head,* tail;
....
void addString(char * string)
{
String * New=new String(string);
New->next=NULL;
if (head==NULL)
head=tail=New;
else {
tail->next=New;
}
tail=New;
}
Oh.. just a suggestion: Temporaries such as
String * current;
String * prev;
are better defined in the routines you use them in and not as part of the class definition. Right now, they occupy space in the Text class yet the values don't need to be stored for the whole life of the Text class. They only need to exist in the scope of the routines using them. This saves on memory. This is not really a huge issue in this case since the number of occurances of the Text class would be rather small anyways.
[edited by - JotDot on October 15, 2003 8:44:04 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement