Replaced malloc+memset with calloc

This commit is contained in:
Celine Mercier
2016-04-21 13:45:39 +02:00
parent 1dc4a3be49
commit c7b8db6a2e

View File

@ -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); 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) 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"); obidebug(1, "\nError allocating memory for an encoded DNA sequence");
return NULL; return NULL;
} }
// Initialize all the bits to 0
memset(seq_b, 0, length_b);
for (i=0; i<length; i++) for (i=0; i<length; i++)
{ {
// Shift of 2 to make place for new nucleotide // 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; seq_b[i/4] |= NUC_T_2b;
break; break;
default: default:
obi_set_errno(OBI_ENCODE_ERROR); // TODO obi_set_errno(OBI_ENCODE_ERROR);
obidebug(1, "\nInvalid nucleotide base when encoding (not [atgcATGC])"); obidebug(1, "\nInvalid nucleotide base when encoding (not [atgcATGC])");
return NULL; 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)); seq = (char*) malloc((length_seq+1) * sizeof(char));
if (seq == NULL) 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"); obidebug(1, "\nError allocating memory for a decoded DNA sequence");
return NULL; return NULL;
} }
@ -151,7 +148,7 @@ char* decode_seq_on_2_bits(byte_t* seq_b, int32_t length_seq)
seq[i] = 't'; seq[i] = 't';
break; break;
default: default:
obi_set_errno(OBI_DECODE_ERROR); // TODO obi_set_errno(OBI_DECODE_ERROR);
obidebug(1, "\nInvalid nucleotide base when decoding"); obidebug(1, "\nInvalid nucleotide base when decoding");
return NULL; 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); 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) 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"); obidebug(1, "\nError allocating memory for an encoded DNA sequence");
return NULL; return NULL;
} }
// Initialize all the bits to 0
memset(seq_b, 0, length_b);
for (i=0; i<length; i++) for (i=0; i<length; i++)
{ {
// Shift of 4 to make place for new nucleotide // 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; seq_b[i/2] |= NUC_N_4b;
break; break;
default: default:
obi_set_errno(OBI_ENCODE_ERROR); // TODO obi_set_errno(OBI_ENCODE_ERROR);
obidebug(1, "\nInvalid nucleotide base when encoding (not IUPAC)"); obidebug(1, "\nInvalid nucleotide base when encoding (not IUPAC)");
return NULL; 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)); seq = (char*) malloc((length_seq+1) * sizeof(char));
if (seq == NULL) 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"); obidebug(1, "\nError allocating memory for a decoded DNA sequence");
return NULL; return NULL;
} }
@ -337,7 +331,7 @@ char* decode_seq_on_4_bits(byte_t* seq_b, int32_t length_seq)
seq[i] = 'n'; seq[i] = 'n';
break; break;
default: default:
obi_set_errno(OBI_DECODE_ERROR); // TODO obi_set_errno(OBI_DECODE_ERROR);
obidebug(1, "\nInvalid nucleotide base when decoding"); obidebug(1, "\nInvalid nucleotide base when decoding");
return NULL; return NULL;
} }