Replaced malloc+memset with calloc
This commit is contained in:
26
src/encode.c
26
src/encode.c
@ -63,17 +63,14 @@ byte_t* encode_seq_on_2_bits(const char* seq, int32_t length)
|
||||
|
||||
length_b = ceil((double) length / (double) 4.0);
|
||||
|
||||
seq_b = (byte_t*) malloc(length_b * sizeof(byte_t));
|
||||
seq_b = (byte_t*) calloc(length_b, sizeof(byte_t));
|
||||
if (seq_b == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obi_set_errno(OBI_MALLOC_ERROR);
|
||||
obidebug(1, "\nError allocating memory for an encoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize all the bits to 0
|
||||
memset(seq_b, 0, length_b);
|
||||
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
// Shift of 2 to make place for new nucleotide
|
||||
@ -99,7 +96,7 @@ byte_t* encode_seq_on_2_bits(const char* seq, int32_t length)
|
||||
seq_b[i/4] |= NUC_T_2b;
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_ENCODE_ERROR); // TODO
|
||||
obi_set_errno(OBI_ENCODE_ERROR);
|
||||
obidebug(1, "\nInvalid nucleotide base when encoding (not [atgcATGC])");
|
||||
return NULL;
|
||||
}
|
||||
@ -125,7 +122,7 @@ char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq = (char*) malloc((length_seq+1) * sizeof(char));
|
||||
if (seq == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obi_set_errno(OBI_MALLOC_ERROR);
|
||||
obidebug(1, "\nError allocating memory for a decoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
@ -151,7 +148,7 @@ char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq[i] = 't';
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_DECODE_ERROR); // TODO
|
||||
obi_set_errno(OBI_DECODE_ERROR);
|
||||
obidebug(1, "\nInvalid nucleotide base when decoding");
|
||||
return NULL;
|
||||
}
|
||||
@ -172,17 +169,14 @@ byte_t* encode_seq_on_4_bits(const char* seq, int32_t length)
|
||||
|
||||
length_b = ceil((double) length / (double) 2.0);
|
||||
|
||||
seq_b = (byte_t*) malloc(length_b * sizeof(byte_t));
|
||||
seq_b = (byte_t*) calloc(length_b, sizeof(byte_t));
|
||||
if (seq_b == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obi_set_errno(OBI_MALLOC_ERROR);
|
||||
obidebug(1, "\nError allocating memory for an encoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Initialize all the bits to 0
|
||||
memset(seq_b, 0, length_b);
|
||||
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
// Shift of 4 to make place for new nucleotide
|
||||
@ -252,7 +246,7 @@ byte_t* encode_seq_on_4_bits(const char* seq, int32_t length)
|
||||
seq_b[i/2] |= NUC_N_4b;
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_ENCODE_ERROR); // TODO
|
||||
obi_set_errno(OBI_ENCODE_ERROR);
|
||||
obidebug(1, "\nInvalid nucleotide base when encoding (not IUPAC)");
|
||||
return NULL;
|
||||
}
|
||||
@ -278,7 +272,7 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq = (char*) malloc((length_seq+1) * sizeof(char));
|
||||
if (seq == NULL)
|
||||
{
|
||||
obi_set_errno(OBI_MALLOC_ERROR); // TODO
|
||||
obi_set_errno(OBI_MALLOC_ERROR);
|
||||
obidebug(1, "\nError allocating memory for a decoded DNA sequence");
|
||||
return NULL;
|
||||
}
|
||||
@ -337,7 +331,7 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
|
||||
seq[i] = 'n';
|
||||
break;
|
||||
default:
|
||||
obi_set_errno(OBI_DECODE_ERROR); // TODO
|
||||
obi_set_errno(OBI_DECODE_ERROR);
|
||||
obidebug(1, "\nInvalid nucleotide base when decoding");
|
||||
return NULL;
|
||||
}
|
||||
|
Reference in New Issue
Block a user