Sunday, November 29, 2009

Assignment 2: IO_TextEdit progress

Constructors & Destructors:
___________________________________
I began to work on these first, encountering little problems along the way. Simply following the format of the already complete IO_Menu and IO_Checklist classes. The first problem I encountered was initializing a 2d dynamic:

Initially I thought the following would suffice:
_editstr= new char[_maxLines][_IO_TEXT_ALLOCATION_LINE_SIZE];

But, after an error, and a little reading online I used the following instead:
_editstr = new char*[maxLines];
for(int i = 0; i < _maxLines; i++)
_editstr[i] = new char[_IO_TEXT_ALLOCATION_LINE_SIZE];


Set(const void *str) Function:
___________________________________
"Splits the newline separated data from the "str" argument into "series of stings" that are created dynamically to be edited by the io_edit function in the edit method. "

Now this function really cause more then a few headaches.
Constant access violation errors and crashes, I spend most of my time messing with the pointers and reading online for solutions.

I began with the strtok() function to split the string into tokens, but soon realized that strtok also modifies the string given as well as returning the tokens, this caused problems. The only way to use this function would then to create a copy of str.

I abandoned strtok for strncpy. Unfortunately I kept having problems casting the values for this to work. After a while of tweaking, I decided to go with the most simplistic way.

At the end, I winded up copying letter by letter. Still playing with pointers,but at last it works. The sad thing is, I was thinking of doing this from the beginning, but wanted to try out strtok instead...

Edit::*data()
___________________________________
After overcoming the last function, thinking I can finish this without breaking a sweat, i realize this is another headache waiting to happen.

For the first part of the function, in the event of a dynamically allocated object, the function runs smoothly. But the second scenario of a non-dynamic object there were many problems.

After some reading, I find that the problem is that conversion target for the string is a string literal - meaning it is pointing to a read only portion of memory. Strcpy is supposed to work over using '=' to copy over character by character. But I still seem to be crashing - encountering access violation errors. It only seems to work if I were to reallocate memory for the conversion target.

For all I know, I might be miss-reading the function requirements, though it specifically says I am not to reallocate memory for the conversion target.

No comments:

Post a Comment