Re: Corrupted NSMutableString
Re: Corrupted NSMutableString
- Subject: Re: Corrupted NSMutableString
- From: Cameron Hayne <email@hidden>
- Date: Fri, 01 Aug 2003 21:38:57 -0400
On 28/7/03 4:08 PM, "Matthew" <email@hidden> wrote:
>
I'm working on a simple program that reads in data from a binary file,
>
processes the information, and copies it to a plain text file (for
>
processing in MATLAB). I can read all of the data in fine. I can even
>
print it without a problem. I'm reading the data and appending it to a
>
NSMutableString so that I can use writeToFile to save the new text
>
file. For some strange reason, the NSMutableString gets corrupted. Only
>
the last two lines are important at the moment. The printf works fine,
>
but the appendFormat randomly chokes. (I've checked the values by hand
>
using a hex editor.)
>
>
short int * zeroCalibrations[*numOfChannels];
>
short int * gainCalibrations[*numOfChannels];
>
short int trial[*numOfChannels];
I know this isn't what you are asking about - but what are those arrays
doing getting declared with what appears to be a variable?
This is not valid C code (or Obj-C code) - you must declare arrays with
sizes that are known at compile time. I recall that gcc might allow this
sort of thing - but it should not.
If you want dynamically sized arrays, you should allocate them. Or use
NSArray, etc.
>
...
>
>
NSMutableString * matlabFile = [[NSMutableString alloc]
>
initWithString:@""];
>
>
... various loops ...
>
>
[dataFile getBytes:trial range:NSMakeRange(*headerLength + cellLength +
>
(q * ((*numOfChannels * 2) * *numOfSampPointsPerOb)) + (r *
>
(*numOfChannels * 2)), 2 * *numOfChannels)];
That is a complicated range expression. It would be better to put it into
separate variables, which you then could print out to check that you are not
asking for more data than will fit in the array 'trial'.
And I think you are treading on thin ice by using [NSData getBytes] to read
short integers. You seem perhaps to be relying on the byte order and that
sizeof(short int) is 2 ? Better not to build such dependencies into your
code.
>
>
... more loops ...
>
>
printf("%.3f", (trial[s] - (float)*zeroCalibrations[s]) * 400 /
>
*gainCalibrations[s]);
>
>
[matlabFile appendFormat:@"%.3f ", (trial[s] -
>
(float)*zeroCalibrations[s]) * 400 / *gainCalibrations[s]];
>
>
>
>
(Please excuse the use of C type arrays, but they worked quite well for
>
reading a string of short integers...)
>
>
After appending the 2472143rd float (-10.342), to the end of matlabFile
>
the string gets erased and the 2472144th element (-17.289) becomes the
>
first item. If I just put a blank space after the %.3f, then the first
>
string that I try to write comes out ok, but the the second string gets
>
truncated at a different spot.
>
>
At this moment, I'm at a loss. I've stepped through the file while
>
checking all the variables. Everything goes fine until the string gets
>
erased. The string should end up being about 30MB, but gets cut down to
>
10.9MB. The following strings get processed without a problem. If I
>
make a simple change to what get appended, then the location of the
>
problem moves.
It seems likely that you have some problem relating to not allocating enough
space for your arrays and hence overwriting something.
And anyway, why accumulate this all into one string if all you are going to
do is write it to a file? Easier to read some data, format it, and write it
out to the file before going on to read the next piece of data.
--
Cameron Hayne (email@hidden)
Hayne of Tintagel
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.