BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String to Char, how?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
b
Guest





PostPosted: Wed Jul 30, 2003 1:31 pm    Post subject: String to Char, how? Reply with quote



if i have a
String ciao = "Hey guys";

char bye[20];

now i want put ciao in bye, how?

how can, futhermore, understand the lenght of ciao? At this lenght i must
add another caracter for '' character or it is just inserted?


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Wed Jul 30, 2003 4:55 pm    Post subject: Re: String to Char, how? Reply with quote




"Jonathan Arnold" <jdarnold (AT) buddydog (DOT) org> wrote


Quote:
strcpy( bye, ciao.c_str() );

That is very dangerous. You should never use strcpy(). Use strncpy()
instead. Using strcpy(), if ciao ever happens to be larger than 20
characters, you will overrun your bye buffer and your program will have
undefined behavior from that time onwards as you have now modified the
memory located after the bye buffer.

Quote:
using the Length() method. A better way of copying the string:

char* bye = new char[ciao.Length()+1];
strcpy( bye, ciao.c_str() );

That, on the other hand, is safer for using strcpy() with. Although you
have to be careful now about the 'new' failing if the memory cannot be
allocated, particularly if ciao is large. Also do not forget to free bye
when you are done with it:

delete[] bye;


Gambit




Back to top
Duane Hebert
Guest





PostPosted: Thu Jul 31, 2003 12:17 pm    Post subject: Re: String to Char, how? Reply with quote



Quote:
So strncpy isn't any better if you don't know the sizes somehow.

But you do know the size whenever using strncpy(), that is the primary
reason people choose to use it in the first place. And if they don't know
the size, then they can't call strncpy() in the first place anyway, so
they
re-write their code so that they will know the size when needed.

The thing about strncpy is that you may end up with an unterminated
string. If the source string is longer than the target, strncpy will copy
n characters to the target but will not append the ''.

You would need to create a target char array big enough to hold
the source + a null in either case and with strncpy make sure
to append the null.

I assume this is all because some part of ciao needs to be passed
to a function requiring a char*. Why not just substring it to another
String or string and pass it with the c_str()?






Back to top
Yahia El-Qasem
Guest





PostPosted: Thu Jul 31, 2003 12:22 pm    Post subject: Re: String to Char, how? Reply with quote

Why not do the following :

AnsiString X = "blabla...";

char *bye = NULL;
bye = new char [ X.Length () + 1 ];
if ( bye )
{
memset ( bye, 0x00, X.Length () + 1 );

strncpy ( bye, X.c_str(), X.Length () );
// not X.Length () + 1 because this way my 0x00 at the is still there
whatever happens while copying...
}

This way "bye" is always under any circumstances terminated and there will
never be any sort of buffer overrun whatsoever...

Just my 2 cents...

Yahia



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Jul 31, 2003 4:54 pm    Post subject: Re: String to Char, how? Reply with quote


"Duane Hebert" <spoo (AT) flarn (DOT) com> wrote


Quote:
The thing about strncpy is that you may end
up with an unterminated string.

Yes, if the end of the destination buffer is reached before the null
terminator is encountered. Which is why your buffers should always be
declared to contain extra room for the null terminator. When calling
strncpy(), pass it a length value that is at least 1 character less than the
overall size of the destination buffer, and make sure that the extra room is
intialized ot set to '' before the buffer is used after the copying.

Quote:
I assume this is all because some part of ciao needs to be
passed to a function requiring a char*. Why not just
substring it to another String or string and pass it with the
c_str()?

That is only when passing the AnsiString to functions taking char*
parameters, or for otherwise other reasons where accessing the raw data of
the AnsiString is desired. But there are certainly other cases where
copying the AnsiString into a char[] array/buffer is desired instead. One
possibility is when copying the AnsiString into a structure that declared
fixed-sized buffers for any strings it contains. That is one common
practice in development your own custom API, especially when DLLs are
concerned, for example.


Gambit




Back to top
Jonathan Arnold
Guest





PostPosted: Thu Jul 31, 2003 6:18 pm    Post subject: Re: String to Char, how? Reply with quote

Remy Lebeau (TeamB) wrote:
Quote:
"Jonathan Arnold" <jdarnold (AT) buddydog (DOT) org> wrote in message
news:3f282507$1 (AT) newsgroups (DOT) borland.com...
Well, yes, that's true, but using strncpy isn't
going to help you all that much either if you
aren't careful there.
It is a lot safer than using strcpy() though, as strcpy() copies an
arbitrary amount of data from the source until a null terminator is
encountered. It does no validation of either the source buffer size of the
destination buffer size at all, or even that the memory being copied from is
valid. Whereas strncpy() forces you to specify the actual size of the

I guess we'll just have to agree to disagree then. I don't see strncpy
as being any more inherently safer than strcpy. Neither does any sort
of size validation, and both require you to know in some way the size of
the destination. And strncpy adds in all the extra stuff about adding in
the terminating '', which is just as likely, in my mind, to get forgotten
as overwriting it.

Mind you, I'm not saying that strcpy is safe. I'm saying that both strcpy
and strncpy are inherently dangerous things to use, and there is almost
no excuse in this day and age to use either.

--
Jonathan Arnold C/C++/CBuilder Keen Advice:
http://www.keen.com/categories/categorylist_expand.asp?sid=5156620
Comprehensive C++Builder link site:
http://www.buddydog.org/C++Builder/c++builder.html



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.