M
mast2as
Hi everyone,
I am trying to implement some specs which specify that an array of
parameter is passed to a function as a pointer to an array terminated
by a NULL chatacter. That seemed fairly easy to implement. I had a
special Malloc function that would allocated the number of needed
bytes for the objects i needed to store + 1 additional byte to save
the NULL character
/*!
* \name Malloc
* \brief Allocate memory for a primitive variable
* \param size is the size of the parameter array (number of
elements)
* \return Pointer to the first byte of allocated memory
*
* Because we can't parse an array of values without knowing its size,
we
* have to terminate this array with a special character, so we stop
accessing
* its elements when we reach it. This is a standard C technique but
also
* how the parameter list is described in the ri spec (the list is
terminated
* by the special token RI_NULL).
*
*/
template<typename T>
void * PrimitiveVariable::Malloc( size_t size )
{
printf( "allocating\n" );
void * buffer;
buffer = (void*)malloc( sizeof( T ) * size + 1 );
memset( buffer, RI_NULL, sizeof( T ) * size + 1 );
return buffer;
}
Then I would set each element of the array with the desired values.
When that is done I pass the array as void* to a function.
RtFloat *vertexArray =
(RtFloat*)PrimitiveVariable::Malloc<RtFloat>( 4 );
for ( int pt = 0; pt < 4; pt++ )
{
vertexArray[pt] = 1.0f + (float)pt / 10.0f ;
//printf( "vertexArray %f\n", vertexArray[pt] );
}
Test( (void*)vertexArray) );
where Test is the following function
void Test( float* buffer )
{
size_t pt = 0;
char *c = (char*)buffer;
for ( int pt =0; pt < sizeof( float ) * 4 + 1; pt++ )
{
if ( *( c + pt ) == RI_NULL )
printf( "RI_NULL %d ", pt );
}
}
The problem with this is approach is that some bytes of the array are
actually set to 0 depending of the value of each individual element
(for example if it is a list of integers all set to 0 all the bytes
are set to 0 which is the same as NULL). So i was hoping I could loop
over the value until I would find a NULL character that would
indicated that i had reached the end of the array, but because some
bytes of the array might be set to 0, this loop potentially stops
before it actually reaches its last stored object.
This seems to be a very common technique in C/C++ so I am sure some
one as a good solution for this problem.
Thanks a lot -mark
I am trying to implement some specs which specify that an array of
parameter is passed to a function as a pointer to an array terminated
by a NULL chatacter. That seemed fairly easy to implement. I had a
special Malloc function that would allocated the number of needed
bytes for the objects i needed to store + 1 additional byte to save
the NULL character
/*!
* \name Malloc
* \brief Allocate memory for a primitive variable
* \param size is the size of the parameter array (number of
elements)
* \return Pointer to the first byte of allocated memory
*
* Because we can't parse an array of values without knowing its size,
we
* have to terminate this array with a special character, so we stop
accessing
* its elements when we reach it. This is a standard C technique but
also
* how the parameter list is described in the ri spec (the list is
terminated
* by the special token RI_NULL).
*
*/
template<typename T>
void * PrimitiveVariable::Malloc( size_t size )
{
printf( "allocating\n" );
void * buffer;
buffer = (void*)malloc( sizeof( T ) * size + 1 );
memset( buffer, RI_NULL, sizeof( T ) * size + 1 );
return buffer;
}
Then I would set each element of the array with the desired values.
When that is done I pass the array as void* to a function.
RtFloat *vertexArray =
(RtFloat*)PrimitiveVariable::Malloc<RtFloat>( 4 );
for ( int pt = 0; pt < 4; pt++ )
{
vertexArray[pt] = 1.0f + (float)pt / 10.0f ;
//printf( "vertexArray %f\n", vertexArray[pt] );
}
Test( (void*)vertexArray) );
where Test is the following function
void Test( float* buffer )
{
size_t pt = 0;
char *c = (char*)buffer;
for ( int pt =0; pt < sizeof( float ) * 4 + 1; pt++ )
{
if ( *( c + pt ) == RI_NULL )
printf( "RI_NULL %d ", pt );
}
}
The problem with this is approach is that some bytes of the array are
actually set to 0 depending of the value of each individual element
(for example if it is a list of integers all set to 0 all the bytes
are set to 0 which is the same as NULL). So i was hoping I could loop
over the value until I would find a NULL character that would
indicated that i had reached the end of the array, but because some
bytes of the array might be set to 0, this loop potentially stops
before it actually reaches its last stored object.
This seems to be a very common technique in C/C++ so I am sure some
one as a good solution for this problem.
Thanks a lot -mark