An ObjectARX application can dynamically allocate a single result buffer by calling acutNewRb(). The call to acutNewRb() must specify the type of buffer to allocate; acutNewRb() automatically initializes the buffer's restype field to contain the specified type code.
The following sample code fragment allocates a result buffer to contain a three-dimensional point and then initializes the point value:
struct resbuf *head; if ((head=acutNewRb(RT3DPOINT)) == NULL) { acdbFail("Unable to allocate buffer\n"); return BAD; } head->resval.rpoint[X] = 15.0; head->resval.rpoint[Y] = 16.0; head->resval.rpoint[Z] = 11.18;
If the new result buffer is to contain a string, the application must explicitly allocate memory to contain the string:
struct resbuf *head; if ((head=acutNewRb(RTSTR)) == NULL) { acdbFail("Unable to allocate buffer\n"); return BAD; } if ((head->resval.rstring = malloc(14)) == NULL) { acdbFail("Unable to allocate string\n"); return BAD; } strcpy(head->resval.rstring, "Hello, there.");
Memory allocated for strings that are linked to a dynamic list is released when the list is released, so the following call releases all memory allocated in the previous example:
acutRelRb(head);
To release the string without releasing the buffer, call free() and set the string pointer to NULL as shown in the following example:
free(head->resval.rstring); head->resval.rstring = NULL;
Setting resval.rstring to NULL prevents a subsequent call to acutRelRb() from trying to release the string a second time.
If the elements of a list are known beforehand, a quicker way to construct it is to call acutBuildList(), which takes a variable number of argument pairs (with exceptions such as RTLB, RTLE, -3, and others) and returns a pointer to a list of result buffers that contains the specified types and values, linked together in the order in which they were passed to acutBuildList(). This function allocates memory as required and initializes all values. The last argument to acutBuildList() must be a single argument whose value is either zero or RTNONE.
The following sample code fragment constructs a list that consists of three result buffers. These contain a real value, a string, and a point, in that order:
struct resbuf *result; ads_point pt1 = {1.0, 2.0, 5.1}; result = acutBuildList( RTREAL, 3.5, RTSTR, "Hello, there.", RT3DPOINT, pt1, 0 );
If it cannot construct the list, acutBuildList() returns NULL; otherwise, it allocates space to contain the list. This list must be released by a subsequent call to acutRelRb():
if (result != NULL) acutRelRb(result);