Solved

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

Posted on 1998-08-11
34
203 Views
Last Modified: 2010-04-02
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
Comment
Question by:learn
  • 17
  • 8
  • 7
  • +1
34 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1169959
EXPERTS  please don't answer this.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169960
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169961
>> 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
 

Author Comment

by:learn
ID: 1169962
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169963
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
 

Author Comment

by:learn
ID: 1169964
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169965
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169966
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169967
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169968
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169969
Agreed, but I'll trade 500,000 lines of readable code for 1 or 2 lines that might need changes anyday.
0
 
LVL 3

Expert Comment

by:xyu
ID: 1169970
I don't understand what is so unreadabel in size_t or wchar_t ???
0
 
LVL 1

Expert Comment

by:Musashi
ID: 1169971
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169972
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169973
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169974
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169975
I don't like abreviations.. except globally known like
NT - Neanderthal Technology :)

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:Musashi
ID: 1169976
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169977
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
 

Author Comment

by:learn
ID: 1169978
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169979
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
 

Author Comment

by:learn
ID: 1169980
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
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
ID: 1169981
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
 

Author Comment

by:learn
ID: 1169982
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169983
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
 

Author Comment

by:learn
ID: 1169984
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169985
>> 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
 

Author Comment

by:learn
ID: 1169986
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169987
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169988
nietod... if answer will be size_t  :) You have to share some pointe with me (i guess) :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169989
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
 
LVL 3

Expert Comment

by:xyu
ID: 1169990
nietod .. :) ok
I want :)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169991
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
 

Author Comment

by:learn
ID: 1169992
sorry, I will ask you again. At the moment, things stoped ;-(

Cheers.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now