[Dev] null terms for strings

Ed Hill dev@trilug.org
07 May 2002 13:39:02 -0600


On Tue, 2002-05-07 at 12:30, M. Mueller wrote:
> Here's a little program that captures a problem I had today.  I have a map 
> container, map<string, t_myStruc), that I want to duplicate on another 
> machine.  I pack each entry into datagram and send it via sockets to another 
> machine where each packet is collected and used to build a copy of the map 
> entry by entry.  So, I convert the string key to an array of chars for the 
> journey to the other side.  In the process I got confused about the function 
> of null terminators on char array strings versus string objects.


Hi Mike,

I tested your snippet and got:

[edhill@eddy ~]$ ./a.out
a not equals b
a.c_str() equals b.c_str()
[edhill@eddy ~]$

which I'm sure is your complaint.

Heres a fix:  How about writing the re-assembly part of your code so
that, by default, it automatically strips any trailing nulls?

Its easy and then you don't have to worry about whether you sent the
string with null termination or not.  Of course, its a lousy idea if
your strings ever need to contain imbedded nulls, but that may not be a
problem...

hth,
Ed


-- 
Edward H. Hill III, PhD    |  Email:       ed@eh3.com, ehill@mines.edu
Post-Doctoral Researcher   |  URLs:        http://www.eh3.com
Division of ESE            |   http://wasser.mines.edu/people/edhill.php
Colorado School of Mines   |  Phone:       303-273-3483
Golden, CO  80401          |  Fax:         303-273-3311
Key fingerprint = 5BDE 4DA1 66BE 4F7B BC17  3A0C 932B 7266 1E76 F123