Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


text buffer problem

This topic is 5393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
Good point. The error occurs in the lines that delete nodes:

delete prev;
delete head;

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.

Share this post

Link to post
Share on other sites
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)

Share this post

Link to post
Share on other sites
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:
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.
class Text
String* head,* tail;

void addString(char * string)
String * New=new String(string);
if (head==NULL)
else {

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]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!