![]() |
The mapping for strings is provided by corba.h, within the CORBA scope. See IDL name scoping for more information. The user-visible types are CORBA::String and CORBA::String_var. CORBA::String is a typedef name for char*. The CORBA::String_var class performs storage management of a dynamically allocated CORBA::String. The following functions are for dynamic allocation/deallocation of memory to hold a String:
- CORBA::string_alloc
- CORBA::string_free
- CORBA::string_dup
A String_var object behaves as a char*, except that when it is assigned to, or goes out of scope, the memory it points to is automatically freed by CORBA::string_free. When a String_var is constructed or assigned from a char*, the String_var assumes ownership of the string and the caller should no longer access the string directly. (If this is not the desired behavior, as when the char* occupies static storage, the caller can use CORBA::string_dup to copy the char* before assigning it.) When a String_var is constructed or assigned from a const char*, another String_var, or a String element of a struct, union, array, or sequence, an automatic copy of the source string is done. The String_var class provides subscripting operations to access the characters within the embedded string.
C++ compilers do not treat a string literal (such as "abc") as a const char* upon assignment; given both a const and a non-const assignment operator, the compiler will choose the non-const operator. As a result, when assigning a string literal to a String_var, no copy of the string into dynamically allocated memory is made; the pointer "owned" by the String_var will point to memory that cannot be freed. Thus, string literals should not be assigned to a String_var without an explicit cast to const char*.
Some examples using String_var objects are:
// first some supporting functions for the examples char* f1() { return "abc"; } char* f2() { char* s=CORBA::string_alloc(4);strcpy(s,"abc");return s; } // then the examples void main() { CORBA::String_var s1; if (0) s1 = f1();// Wrong!! The pointer cannot be freed and // no copy is done. if (0) s1 = "abc"; // Also wrong, for the same reason. const char* const_string = "abcd"; // *const_string cannot be changed s1 = const_string; // OK. A copy of the string is made because // it is const, and the copy can be freed. CORBA::String_var s3 = f2();// OK. no copy is made, but f2 // returns a string that can be freed CORBA::String_var s4 = CORBA::string_alloc(10); // also OK. no copy s4 = s3; // s4 will use string_free followed by string_dup long l4 = strlen(s4); // l4 will receive 3 long l1 = strlen(s1); // l1 will receive 4 if (l4 >= l1) strcpy(s4,s1); // OK, but only because of the condition. // note that s4's buffer only has size=4. s4 = const_string; // OK. s4 will use string_free followed by // string_dup. The copy is made because String_vars // must reference a buffer that can be modified. } // The s1, s3 and s4 destructors run successfully, freeing their buffers