473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

using string as a character buffer in unix system calls

I have gotten in the habit of using strings to manage character buffers
that I pass in to unix system calls.

For example, suppose I want to create a character buffer to use with
the "write" system call.

string buf(1024);
int fd;
write(fd,(void *)(&buf[0]),buf.size());

It recently occured to me that the standard C++ library doesn't
necessarily guarantee that a vector<char> or string will be implemented
in terms of a single c array of char, so that by taking creating a
pointer to the first element of the string or vector and passing it
into a function expecting a c array of char I could be making my
programs non-portable.

Does anyone know if the standard makes any guarantees one way or the
other?

Does anyone find my practice a bad idea?

Chris

Jul 23 '05 #1
4 3414
ch******************@yahoo.com wrote:
I have gotten in the habit of using strings to manage character buffers
that I pass in to unix system calls.

For example, suppose I want to create a character buffer to use with
the "write" system call.

string buf(1024);
int fd;
write(fd,(void *)(&buf[0]),buf.size());
Ugh! Should most likely be

write(fd, buf.c_str(), buf.size());


It recently occured to me that the standard C++ library doesn't
necessarily guarantee that a vector<char> or string will be implemented
in terms of a single c array of char, so that by taking creating a
pointer to the first element of the string or vector and passing it
into a function expecting a c array of char I could be making my
programs non-portable.
Not really. If you use &somevector[0], you're most likely fine because
in the next standard the contiguousness of the vector storage will be
required (and all current implementations already do that). For a string
you should use 'c_str()' anyway.
Does anyone know if the standard makes any guarantees one way or the
other?
c_str() returns a pointer to the first character of an array, and that
pointer remains valid until a call to a non-const member function. What
you shouldn't attempt doing is _reading_ into that area.
Does anyone find my practice a bad idea?


No. Just do it right. And don't _read_ into a string. For that you
should use a vector or a plain array.

V
Jul 23 '05 #2
Victor:

Thanks for the quick reply!

I goofed in my example. I should have declared my buf like this:

vector<char> buf(1024);

It is interesting that the next standard will require contiguousness of
storage in vectors. I wonder what reasons pushed that into being an
explicit issue?

Your c_str() point was well taken. Also the one about not reading into
a string through the address of its first element.

Chris Marshall

Jul 23 '05 #3
ch******************@yahoo.com wrote:
[..]
It is interesting that the next standard will require contiguousness of
storage in vectors. I wonder what reasons pushed that into being an
explicit issue?


I think it's the same reason that drove you to use &v[0] to read into or
write from the vector's storage. People have too much legacy stuff that
needs an array. A vector is a good replacement for the C++ arrays _if_
you allow using its storage for direct access through a pointer to the
first element. Turned out to satisfy other requirements vector could not
have non-contiguous storage, so it was decided to legalize that drug.
At least that's how I remember it. You can find out better if you ask
in comp.std.c++ were they talk Standard document.

V
Jul 23 '05 #4
Me
Victor Bazarov wrote:
string buf(1024);
int fd;
write(fd,(void *)(&buf[0]),buf.size());


Ugh! Should most likely be

write(fd, buf.c_str(), buf.size());


I think it's better to use buf.data() here instead.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
2
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
2
by: Alpha | last post by:
Hi, I'm able to make connection to a server using socket connection. However, when I send a command string the server just ignores it. All command string needs to start with "0xF9" at Byte 0. ...
7
by: Malcolm | last post by:
This is a program to convert a text file to a C string. It is offered as a service to the comp.lang.c community. Originally I thought it would be a five minute job to program. In fact there are...
33
by: sklett | last post by:
I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this: "California" "_california" Here is the (ugly) solution I cam up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.