简体   繁体   中英

I am getting a segmentation fault error on my C program

I am getting a segmentation fault in one of the following blocks of code, but I suspect it to be this one, xbits.c:

#include <stdio.h>
#include <math.h>

void itox(int n, char hexstring[]);
int xtoi(char hexstring[]);

void itox(int n, char hexstring[]) {
    hexstring[2*sizeof(n) + 1];
    int ratio, remainder;
    int i = 0;
    while(ratio  != 0)
    {

        ratio = n / 16;
        remainder = n % 16;
        if (remainder == 10){
            hexstring[i] = 'A';
            i++;
        }
        else if (remainder == 11){
            hexstring[i] = 'B';
            i++;
        }
        else if (remainder == 12){
            hexstring[i] = 'C';
            i++;
        }
        else if (remainder == 13){
            hexstring[i] = 'D';
            i++;
        }
        else if (remainder == 14){
            hexstring[i] = 'E';
            i++;
        }
        else if (remainder == 15){
            hexstring[i] = 'F';
            i++;
        }
        else 
            hexstring[i] = remainder;

    }   
    i++;
    hexstring[i] = '\0';
}

int xtoi(char hexstring[]) {
    int i, integer;
    int length = getLength(hexstring);
    for (i = length-2 ; i >= 0 ; i--)
    {
         integer += (int) pow((float) hexstring[i] , (float) i);
    }

    return integer;
}

int getLength (char line[])
{
    int i;

    i=0;
    while (line[i])
        ++i;
    return i;
}

The above relies on a main method, showxbits.c that I have not debugged yet, but when I used a test main method I got a segmentation fault error, and when I use gdb I get the following error.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007ad in itox ()

Which leads me to believe that the problem is in itox in the above .c file. The program is supposed to convert an int to hex and a hex back to int.

You have many errors in your code. You can catch some of them with compiler warnings. Some of them are logical errors, though.

  • ratio is uninitialised and will contain garbage when you first use it in the while condition. It should probably be initialised to n .
  • Your while loop condition will never change (the initial value aside), because ratio will always be n / 16 and n never changes. You probably don't need ratio and can work on n directly.
  • When you say hexstring[i] = remainder you don't do what you think you do. You want to assign a digit, but you assign the character with an ASCII code of zero to 9, which are mostly unprintable characters. (Zero even terminates the string.) You want

     hexstring[i] = '0' + remainder; 

    here. You can do the same for letters with 'A' . Note that in the case of a digit you don't increment i , thus overwriting the digit in the next iteration through the loop.

  • The lone line

     hexstring[2 * sizeof(n) + 1]; 

    doesn't do anything. You must allocate enough memory in the function that calls itox .

  • You are writing the letters to the string in the wrong order.
  • When you decode, you shouldn't take the hex digits by their ASCII values. You must convert them to their values from 0 to 15.
  • don't use the pow function for simple integer arithmetic. Instead, follow the pattern of itox , only the other way round: Muliply the result with 16 and add the value of the next hex digit.
  • You can, of course, roll your own strlen and call it getLength , but make sure to provide a prototype before you call it.
  • This is a minor issue, but your numbers should proably be unsigned , because there is no hex representation of negative ints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM