CORBA style was: [omniORB] CORBA::string_alloc(len) problem

David Riddoch djr@uk.research.att.com
Fri, 16 Jul 1999 10:55:53 +0100 (GMT)


On 16 Jul 1999, Bjorn Wennberg wrote:

> I'm just curious about this :-)
> 
> As far as I'm able to understand the behaviour of string_alloc - it returns
> a character pointer not a new String_var class?
> 
> Thus the assignment would be correct without temporary copying the String_var:
> 
> 1. String_var SomeString(CORBA::string_alloc(100));
> 2. String_var SomeString(new char[100]);
> 3. String_var SomeString = CORBA::string_alloc(1000);
> Number 1 and 2 are equal and uses the ctor:
> String_var::String_var(char *p) { _data = p; }
> Number 3 might use either the ctor or the assignment operator, which are equal:
> String_var::operator = (char *p) { if (_data) string_free(_data); _data = p; }

No - the right hand side is a String_var, not a char*, so we use:

String_var::operator = (String_var&)

And thus we *copy* the string rather than consume it.

> Then I can't see what is wrong with doing
> String_var SomeString = string_alloc(100);
> char *ptr = SomeString;
> ptr[0] = 'f';
> ptr[1] = 'o';
> ptr[2] = 'o';
> ptr[3] = 0;
> 
> Please correct me if I'm missing something here :-)
> 
> bjornw>
>