Use calloc instead of malloc for memory allocations in CPU CTC

上级 5891883e
...@@ -56,7 +56,7 @@ void create_contiguous_input_lengths( PyArrayObject * input_lengths_arr, ...@@ -56,7 +56,7 @@ void create_contiguous_input_lengths( PyArrayObject * input_lengths_arr,
{ {
npy_int num_elements = PyArray_DIMS( input_lengths_arr )[0]; npy_int num_elements = PyArray_DIMS( input_lengths_arr )[0];
*input_lengths = (int *) malloc( num_elements * sizeof(int) ); *input_lengths = (int *) calloc( num_elements, sizeof(int) );
if ( NULL == (*input_lengths) ) if ( NULL == (*input_lengths) )
return; return;
...@@ -73,11 +73,11 @@ void create_flat_labels( PyArrayObject * label_matrix, int ** flat_labels, ...@@ -73,11 +73,11 @@ void create_flat_labels( PyArrayObject * label_matrix, int ** flat_labels,
npy_int rows = PyArray_DIMS( label_matrix )[0]; npy_int rows = PyArray_DIMS( label_matrix )[0];
npy_int cols = PyArray_DIMS( label_matrix )[1]; npy_int cols = PyArray_DIMS( label_matrix )[1];
*flat_labels = (int *) malloc( rows * cols * sizeof(int) ); *flat_labels = (int *) calloc( rows * cols, sizeof(int) );
if ( NULL == (*flat_labels) ) if ( NULL == (*flat_labels) )
return; return;
*label_lengths = (int *) malloc( rows * sizeof(int) ); *label_lengths = (int *) calloc( rows, sizeof(int) );
if ( NULL == (*label_lengths) ) if ( NULL == (*label_lengths) )
{ {
free( *flat_labels ); free( *flat_labels );
...@@ -127,6 +127,9 @@ int APPLY_SPECIFIC(ctc_cost_cpu)(PyArrayObject * in_activations, ...@@ -127,6 +127,9 @@ int APPLY_SPECIFIC(ctc_cost_cpu)(PyArrayObject * in_activations,
if ( NULL == context->input_lengths ) if ( NULL == context->input_lengths )
{ {
// Destroy previous CTC context before returning exception
ctc_context_destroy( context );
PyErr_Format( PyExc_MemoryError, PyErr_Format( PyExc_MemoryError,
"ConnectionistTemporalClassification: Could not allocate memory for input lengths" ); "ConnectionistTemporalClassification: Could not allocate memory for input lengths" );
return 1; return 1;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论