• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Only use length, no '\0' in strings.

Hi Experts:

In my own String class, I tried to use only string length, not add '\0' at the end.

1. What I should do for
   cout<<string1.GetString
without the '\0'?
2. Use cout.write(const singed char*, int n)? Unfortunately, instead 'int', I use 'unsigned short' for the length.
0
learn
Asked:
learn
  • 17
  • 8
  • 7
  • +1
1 Solution
 
nietodCommented:
EXPERTS  please don't answer this.

0
 
nietodCommented:
Learn, don't you have a question on this already?  If so, next time ask there and save yourself 50 points.    Just because a question is answered, doesn't mean it is done with.  If you need additional help or clarification on a subject, post comments to the question until yoiu get it totally resolved.
0
 
nietodCommented:
>> Unfortunately, instead 'int', I use 'unsigned short' for the length.

You just need to convert the "unsigned short" to an "int" like

cout.write(stringptr*,(int) n);

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
learnAuthor Commented:
Hi nietod :-)

I will check where I got the answer.....

I think a number of unsigned short may be too big to convert to the number of int........????
0
 
nietodCommented:
In terms of bits, short may not be bigger than int and is ussually smaller. However, if the two are the same size in bits, then a short can be up to twice as large (in value) as an int.  However, is this really likely to be a problem?  In a 16 bit compiler short will go up to 64k and int will only go up to 32K (unless ints are 32 bits).  Are you going to have strings that exceed 32k?  If so, there are ways around this, but I really doubt it is worth worrying about.  
0
 
learnAuthor Commented:
Hi nietod

I just think all cases ;-) Is this possible that some files without return at all in a paragraph or even in the whole file? In those cases, a string may become very long....

OK, I may use int and forget unsigned short...but I will waste the negative part of the int type:-)
0
 
nietodCommented:
Int is always at least as many bits a short.  So you could use unsigned int instead of unsigned short.  That is guaranteed to store at least as much and most likeley much more.
Now you still need to convert from unsigned int to int and could run into problems there.  If you want to be really safe about it you could look to see if the unsigned int is too large to store in an int, if so, you would write to string using two write() operations with shorter lengths whose sum is the unsigned int's value.  But that probalby really isn't necessary.  I would just convert to an int and assume the value was converted okay

0
 
xyuCommented:
hey guys ... what about standard definition of size_t???
Microsoft anounced on MSDN online, that in new version of Win64 they thinking to make sizeof(void*) bigger than sizeof (unsigned long )!!!! that will be nice... :( but size_t by the standard draft must hold any memory size allowed by system (actually by new and alloc operations)... think about it :)
0
 
nietodCommented:
That's a good point.  (Although a string that needs a 64 bit length is not going to fit on my hard disk much less in RAM.)

Since size_t is ugly, I use a typedef.  I have all my ints in my program declared using a type that is typedefed to int currently.  I can just change that single typedef to change all their sizes if needed.
0
 
xyuCommented:
nietod... Your point is clear... and its sounds for me like that...
"..If system is going to be changed i'll change my definition"...
but You don't need to change anything if You use standard defs. from the beginning :) so Your code is already compatible not possibly compatible or easy to make compatible :)
0
 
nietodCommented:
Agreed, but I'll trade 500,000 lines of readable code for 1 or 2 lines that might need changes anyday.
0
 
xyuCommented:
I don't understand what is so unreadabel in size_t or wchar_t ???
0
 
MusashiCommented:
Nietod, I'm always curious about the conventions of others; what are all of your int's typedef'd to that is readable compared to size_t (which, BTW, I also consider ugly)?
0
 
nietodCommented:
Well first of all I don't use "_"

My convention use 3 letter abreviations.  The first letter of the abreviation is capitalized to make it easier to pick out the abbreviations.    The abbreviations are standardized so that once you learn an abreviation you know that it will be used in all cases.  This leads to names like

OpnFil - open file.
ClsFil  - Close file.
OpnDbfFil - Open database file.
ClsDbfFil - Close database file.
OpnWnd - Open window.
ClsWnd - Close window.

0
 
xyuCommented:
nietod...
oin my code it should look like that

OpenFile
CloseFile
OpenDatabaseFile
CloseDatabaseFile
OpenWindow
CloseWindow

so nobody need to learn any... and code seaks for itself :)

0
 
nietodCommented:
A short look throught the MS guides brings up these little dities.

AVIMakeStreamFromClipboard
AVIStreamNearestKeyFrameTime

I don't have time for that.  Of course MS uses abreviations too.

GetSysColor()
GetSysColorBrush(0
GetSysModalWindow()
GetSysDirectory() -- Nope! GetSystemDirectory()
0
 
xyuCommented:
I don't like abreviations.. except globally known like
NT - Neanderthal Technology :)

0
 
MusashiCommented:
xyu, you insult Neanderthal's -- fortunately we have thick skins ;)
I  wonder what the abbreviation "xyu" stands for?  Hmmmn.

nietod, so is "Int" your int typedef?
You are prolific, so I assume that you are a touch typist, so that would have nothing to do with your preference for shortened names.  
BYW, why do you choose to not use MFC?
0
 
nietodCommented:
All names provided by my framework begin with a "Q" or "q"  (the difference is significant).  So my ints are "QInt".

I'm a touch typo-est.   I think short names are easier to read.  

My class framework is far superior to MFC, at least in the areas it covers, it is no where neear as extensive as MFC.
0
 
learnAuthor Commented:
Hi Nietod

After you, experts, discussed the interesting things, can you give me the answer? I am a bad student and can not remember where you posted the answer:) I have also checked all the Q&As but got no such answer :(

Thank you.
0
 
nietodCommented:
Sorry, I thought you had your answer.  you have choices.  you could:

1. use "int" for the length instead of "unsigned short".  
2. use "size_t" for the length instead of "unsigned short"
3.  convert from unsigned short to an int when you write by doing
cout.write(stringptr,(int) strlength);
where the "(int)" converts the "unsigned short" that I called "stringlength"  to an "int".
0
 
learnAuthor Commented:
Hi Nietod

Before I decide to use "int", can yor tell me something about "size_t"? Can you also put stuff into "answer" instead of "comment"?
0
 
nietodCommented:
Well, If you want to spend 50 points, I'm not going to refuse.

size_t is an integeral type  (like char, short, int, etc)  i.e. it stores an integer number.  However it is garanted to be big enough to store the difference between any two addresses (pointers) on your computer.  That is, it is big enough that it can express the memory size of any item.  Does that make sense.  sizeof() returns the size of a structure or class as a size_t because only a size guaranted to be big enough to hodl the size of any class or structure.   A huge class might be bigger than the largest value that can be expressed in an int.  Similarly a huge array could be so large that an int can't hold its size (this is more like your case) but a size_t will be large enough to hold it.
0
 
learnAuthor Commented:
Hi Nietod

I am reading your answer at the moment, thank you.

Actually, the points is not 50. It is 70 now. I increase the points each time I think I should pay more than the initial value....Perhaps you didn't notice..you got extra points two or three times from me :-)
0
 
nietodCommented:
Thanks.  But if you do that, I hope you are also giving A's.  The points we received are "multiplied" by the grade.  A = 4, b = 3 etc.  One client on two occasions increased the points and then gave me a low grade.  In a sense it is a waste of his points because he could have given me the same number of points with a good grade without spending more of his own points.
0
 
learnAuthor Commented:
Hi Nietod

I remember I graded all your answers as A since I found yours are helpfull.

size_t looks not like one of the Int types. Why not use "resize int" or INT...(it is not asking you). So I can do
     const size_t asize = 9999999999;
     String s[asize];
if my computer has got 9999MB RAM.
0
 
nietodCommented:
>> size_t looks not like one of the Int types
What do you mean?  In fact, I'm having trouble following all of that comment...  


0
 
learnAuthor Commented:
Hi Nietod

???????
I must misunderstood! I thought size_t is one of the int types (the others are int, short, long...) and we can not do
   const int i = 999999999;
but can do
   const size_t i = 999999999;  //:-)

So...I didn't understand :-(
0
 
nietodCommented:
size_t can be used the way you suggested, that is

const size_t i = 9999999;

That is, it can be used just like an int or short etc.  However, it is not automatically defined.  It is defined in the stddef.h include file.  (Where there is probably a typedef that says "typedef int size_t"  Thus size_t is really probably just an int, but that could change.)  Include that file and you can then use size_t.
0
 
xyuCommented:
nietod... if answer will be size_t  :) You have to share some pointe with me (i guess) :)
0
 
nietodCommented:
Well, that part was admitedly your idea--I don't use size_t myself.

If you want, I'll ask a 35 pt (half of 70) for you.
0
 
xyuCommented:
nietod .. :) ok
I want :)
0
 
nietodCommented:
So what's happening here?  You were e-mailing me stuff and that seems to have stopped.  I don't remember where things were.
0
 
learnAuthor Commented:
sorry, I will ask you again. At the moment, things stoped ;-(

Cheers.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 17
  • 8
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now