Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359 https://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See https://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96
  1. Remove unnecessary code. Neither the cast nor the multiplication by sizeof(char) * needed (it is always 1). It you want to show the scaling by the type use sizeof *result instead.

     // char * result = (char *) malloc(sizeof(char) * (i + 1 + negative));
     char * result = malloc(i + 1 + negative);
     // or 
     char * result = malloc(sizeof *reuslt * (i + 1 + negative));
    
  2. Little helper functions like char toCharacter(int v) should be static as they are not meant to be called by outside functions.

  3. Agree with @JS1 that code should pass in the buffer. Also recommend passing in the buffer size. Return NULL when size is insufficient.

     char *convertBase(char *dest, size_t size, long long int value, int base);
    
  4. MAX_LLI_REP 63 assumes long long is about 64 bits. C only requires long long to be at least 64 bits. Aside from the values being wrong for a 64-bit long long as pointed out by @JS1, the value should be based on the size of long long and not the assumption it is 64-bits.

     // #define MAX_LLI_REP 63
     #define MAX_LLI_REP (sizeof(long long)*CHAR_BIT + 2)
    
  5. Should you care about pre-C99 compatibility, some_negative_int/base and some_negative_int%base have implementation defined results that breaks this code. Converting all to positive values, except when value is 2's complement LLONG_MIN, solves this. LLONG_MIN needs special code - it just depends on what degree of portability you want.

     if (value < -LLONG_MAX) {
     // The details of this get into just how portable code needs to be.
     Special_TBD_Code();
     }
     else {
     quotient = value >= 0 ? value: -value;
     ...
     }
    
  6. A now rare concern is -0, possible when long long is signed magnitude or 1's complement. I do not see a portable solution. The following offers some portability. See http://stackoverflow.com/q/19869976/2410359

     #if LLONG_MIN == -LLONG_MAX
     if (value == 0) {
     long long pz = 0; 
     if (memcmp(&value, &pz, sizeof value) == 0) strcpy(c,"0");
     else strcpy(c,"-0");
     }
     #endif 
    
lang-c

AltStyle によって変換されたページ (->オリジナル) /