I have been trying to write a function that will insert commas into a binary number.
Below is my best attempt. It does work if I do NOT try to free() the memory. If I try to free() the memory, I get an error.
I am puzzled. Please let me know what I am doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int insertCommasIntoBinaryNumber(char* outstring, char* instring)
{
char *ptr, *optr;
int i, length, commas;
// move ptr to end of instring
for ( ptr = instring; *ptr; ptr++ );
//calculate offset with commas
length = ptr - instring;
commas = ( length - 1 ) / 8;
optr = outstring + length + commas;
//copy instring into outstring backwards inserting commas
*optr-- = *ptr--;
for ( i = 1; ptr >= instring; i++ )
{
*optr-- = *ptr--;
if ( ( i % 8 ) == 0 )
*optr-- = ',';
}
}
int main (void)
{
const int arrayDimension = 100;
char* instring = (char*) malloc(sizeof(char) * arrayDimension);
char* outstring = (char*) malloc(sizeof(char) * arrayDimension);
strncpy(instring, "111111110101010100001100", arrayDimension-1);
insertCommasIntoBinaryNumber(outstring, instring);
/* show the result */
printf ( "%s\n", outstring );
free(instring);
free(outstring);
}
Here is the output:
11111111,01010101,00001100
*** Error in `./a.out': free(): invalid next size (fast): 0x0000000000bc8010 ***
P.S. Many thanks for letting me know where the code was crashing on the 24th iteration. I soon realized that I was not calculating the number of commas needed correctly and not keeping track of the number of commas being inserted. After I did that, the code below appears to work fine now.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int insertCommasIntoBinaryNumber(char* const outString, const char* const inString)
{
char const *iptr; // iptr will be a pointer to the
// constant inString char array.
char *optr;
int i, commaCount;
// move iptr to end of inString
for ( iptr = inString; *iptr; iptr++ );
// Calculate Number of Commas Needed
const int inStringLength = iptr - inString;
const double totalNumberOfCommasFP = (( inStringLength ) / 8.0) - 1.0;
const int totalNumberOfCommas = (int) ceil(totalNumberOfCommasFP);
// Set optr
optr = outString + inStringLength + totalNumberOfCommas;
//copy inString into outString backwards inserting commas
*optr-- = *iptr--;
commaCount = 0;
for ( i = 1; iptr >= inString; i++ )
{
*optr-- = *iptr--;
if ( ( ( i % 8 ) == 0 ) && (commaCount < totalNumberOfCommas) )
{
*optr-- = ',';
commaCount++;
}
}
}
int main (void)
{
const char testString[] = "111111110101010100001100";
const int inStringArrayDimension = strlen(testString) + 1;
char * inString = (char*) malloc(sizeof(char) * inStringArrayDimension);
strncpy(inString, testString, inStringArrayDimension);
const int inStringLength = (int) strlen(inString);
const double totalNumberOfCommasFP = (( inStringLength ) / 8.0) - 1.0;
const int totalNumberOfCommas = (int) ceil(totalNumberOfCommasFP);
const int outStringArrayDimension = inStringArrayDimension + totalNumberOfCommas;
char* outString = (char*) malloc(sizeof(char) * outStringArrayDimension);
insertCommasIntoBinaryNumber(outString, inString);
/* show the result */
printf ( "%s\n", outString );
free(inString);
free(outString);
exit (EXIT_SUCCESS);
}
And here is the output:
11111111,01010101,00001100