CORBA style was: [omniORB] CORBA::string_alloc(len) problem
David Riddoch
djr@uk.research.att.com
Fri, 16 Jul 1999 10:00:45 +0100 (GMT)
On 16 Jul 1999, Bjorn Wennberg wrote:
> Back to the example of accessing the elements of the String_var;
> What is wrong with doing:
> String_var SomeString = CORBA::string_alloc(100);
The above line is the problem. The c++ spec says that this is equiv to
String_var SomeString;
{
String_var _tmp(CORBA::string_alloc(100));
SomeString = _tmp;
}
Thus we attempt to copy a string which is uninitialised. In the case of
omniORB it will have been initialised to the empty string -- but this just
means we will end up with a buffer of length 1 instead of 101 as intended.
Now most compilers optimise away _tmp and the assignment to give what
you'd expect, but a few (eg. on VAX) don't.
Better would be the following line, and then the rest of your example is
okay.
String_var SomeString(CORBA::string_alloc(100));
> char *ptr = SomeString; // uses the operator char *() to extract the raw _data ptr
> // but does not copy the string
> ptr[0] = 'c';
> ptr[1] = '+''
> ptr[2] = '+';
> ptr[3] = 0;
David