strings longer than 255 
Author Message
 strings longer than 255

Hi,
I have been working on a program for a while which (among other things)
reads in a file, a line at a time, using fgets.   I have a #define
for the length of the string, and use that name everywhere I would
use the length of the string.  My program has been working fine for
a few months, but recently a file it had to process had a line longer
than my #define'd length (which was 200) so I made it 300.   Then
I started noticing weird behavior and getting erroneous results.  Playing
around with the number, I came to the conclusion that any number <= 254
works, but any number >= 255 causes erroneous results. (well, usually a
segmentation fault, with wierd pointers like 0000:0245 in malloc'd variables)  
The program works fine under unix, I only see the problem under Windows 3.1.  
Before I bothered to post the code, I thought I would see if this struck
a familiar chord with anyone that might have any ideas.  I am using
Microsoft Visual C/C++ 1.0, a Large memory model, with no optimizations and
full debug.   I have run it under Bounds Checker, but it doesn't seem to
properly pinpoint the problem....it's flaky behavior there too.

Melissa Abato

#include <stddisclaimer.h>



Sat, 31 Aug 1996 03:53:18 GMT  
 strings longer than 255

|I have been working on a program for a while which (among other things)
|reads in a file, a line at a time, using fgets.   I have a #define
|for the length of the string, and use that name everywhere I would
|use the length of the string.  My program has been working fine for
|a few months, but recently a file it had to process had a line longer
|than my #define'd length (which was 200) so I made it 300.   Then
|I started noticing weird behavior and getting erroneous results.  Playing
|around with the number, I came to the conclusion that any number <= 254
|works, but any number >= 255 causes erroneous results. (well, usually a
|segmentation fault, with wierd pointers like 0000:0245 in malloc'd variables)  

Hrmmm ... applying some numerology and some occasional rainy Sunday
afternoon cabbala tells me that it's got something to do with the
number 255 ;-) Quite suspect, I might say, because this particular
number happens to be the largest number that can be stored in an
unsigned char (at least, that's what my dear Watson told me, after
some quick thinking.) Are you, mayhap, indexing that line with a
variable of type (unsigned) char?

Sorry, I can't say much more about your problem without seeing at
least some of the relevant code ...

kind regards,


----------------------------------------------------------------------------
Hebban olla vogala nestas beginnan hinase hic enda thu
        -- First Dutch sentence



Sun, 01 Sep 1996 16:58:19 GMT  
 strings longer than 255

: Hi,
: I have been working on a program for a while which (among other things)
: reads in a file, a line at a time, using fgets.   I have a #define
: for the length of the string, and use that name everywhere I would
: use the length of the string.  My program has been working fine for
: a few months, but recently a file it had to process had a line longer
: than my #define'd length (which was 200) so I made it 300.   Then
: I started noticing weird behavior and getting erroneous results.  Playing
: around with the number, I came to the conclusion that any number <= 254
: works, but any number >= 255 causes erroneous results. (well, usually a
: segmentation fault, with wierd pointers like 0000:0245 in malloc'd variables)  
: The program works fine under unix, I only see the problem under Windows 3.1.  
: Before I bothered to post the code, I thought I would see if this struck
: a familiar chord with anyone that might have any ideas.  I am using
: Microsoft Visual C/C++ 1.0, a Large memory model, with no optimizations and
: full debug.   I have run it under Bounds Checker, but it doesn't seem to
: properly pinpoint the problem....it's flaky behavior there too.

The problem is obviously something around your pointer arithmetic. The
outstanding value of 255, which is incidentially the largest value of
unsigned char on most machines (including DOS) seems to indicate that

* either your program fiddles around with char's, at best including
  typecasts, to compute some address,

* or your compiler internally works with char's.

However, as it runs well under UN*X, I suspect that this is a compiler
error (or, even worse, a library error). I would recommend to read the
library and compiler manuals thoroughly.

In case you are able to rewrite your program, simply reject lines longer
than a certain length (1024 will suffice for most files existing). You
must then, though, use fgetc() and do some indexing and pointer-updating.

Yours sincerely

        HW



Mon, 02 Sep 1996 21:11:50 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Is there way to call SNMP API with 255.255.255.255

2. Is there way to call SNMP API with 255.255.255.255

3. Is there way to call SNMP API with 255.255.255.255

4. Can not display text longer than 255 characters in the CListCtrl (VC++ 6.0)

5. Problem with SQL string larger than 255 items!

6. Problem with SQL string larger than 255 items!

7. Debuggins strings > 255 characters

8. convert string to long, and long to string

9. char size (was long long long long long int)

10. format string for long long

11. data type to store numbers 0-255.

12. Simpleton: Can someone tell me the difference between char* and char x[255]

 

 
Powered by phpBB® Forum Software