Copying Strings

It is often necessary to copy one string to another. Assume, for example, that there are two string descriptors s_src and s_dst, and that s_dst contains garbage. It would almost suffice to simply copy the contents of s_src into s_dst. The reason this is not quite correct is that both descriptors would then contain a pointer to the same string. This aliasing can cause some strange effects, or even cause IDL to crash if one of the two descriptors is freed and the string from the other is accessed.

IDL_StrDup() takes care of this problem by allocating memory for a second copy of the string, and replacing the string pointer in the descriptor with a pointer to the fresh copy. Naturally, if the string descriptor is for a null string, nothing is done.

void IDL_StrDup(IDL_STRING *str, IDL_MEMINT n) 

where:

str

Pointer to one or more IDL_STRING descriptors which need their strings duplicated.

n

The number of descriptors.

The proper way to copy a string is:

s_dst = s_src;            /* Copy the descriptor */ 
IDL_StrDup(&s_dst, 1L);   /* Duplicate the string */