-2

I typed these codes + I get a segmentation fault. I am trying to make my very own special version of strtol:

struct optional_int {int Value; char IsNull;};
struct optional_int StrToHex(char Str[]) {
    const char Hex[0x10] = "0123456789ABCDEF";
    unsigned int Chr = 0x00,i,j,Number = 0x00;
    unsigned char IsNull, IsNegative;
    if(Str[0x0] == '-') {
        IsNegative = 0x1;
        int N_C_Char = 0;
        while( Str[N_C_Char]  !=  '\0' ) {
            Str[N_C_Char]=Str[N_C_Char+1];//right here
            N_C_Char++;
        }
    }else{IsNegative=0;}
    printf("%sfas", Str);
    for(i = strlen(Str); i > 0; i--){
        unsigned int Successes = 0x0;
        for( j = 0; j < 0x10; j++ ) {
            if( Str[Chr]==Hex[Chr]) {
                Number+=((pow(0x10, i))*j);
                Successes++;
            }
        }
        if(Successes!=1) {
            IsNull = 1;
        }else{
            IsNull = 0;
            Number = 0;
        }
        Chr++;
    }
    if(IsNegative == 1) {
        return (struct optional_int){ Number, IsNull};
    }else{
        return (struct optional_int){-Number, IsNull};
    }
}

int main(int argc, const char *argv[]) {
    printf("asdf %x\n", StrToHex("-535").Value);
}

Whenever I give it some negative numbers, it gave me a segmentation fault core dump but I have located the issue.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 2
    Have you tried single stepping it in a debugger, examining relevant variables before executing each line? I think that's the BEST way we can help you. – paxdiablo Jan 12 '19 at 20:52
  • Can you also provide the code that calls this function? I can't see anything wrong at this code, maybe the string you pass to the function is the issue.. – Vlad Rusu Jan 12 '19 at 20:52
  • I don't how a debugger is used –  Jan 12 '19 at 20:53
  • @VladRusu ok. I will –  Jan 12 '19 at 20:53
  • Then that's the first thing you should LEARN. Otherwise you'll be back here tomorrow with a similar question, having learnt very little. – paxdiablo Jan 12 '19 at 20:54
  • @paxdiablo Would you like me to delete my Question –  Jan 12 '19 at 20:58
  • No, the question itself is not too bad, other than the sort of whiney help-me-please at the end :-) I'm just saying you would become a much better developer with some debugging practice than you would by just being handed the answer. – paxdiablo Jan 12 '19 at 21:02
  • @paxdiablo it have been deleted –  Jan 12 '19 at 21:03
  • Aside: it's not a good solution to use `pow` in an integer function. – Weather Vane Jan 12 '19 at 21:03
  • @WeatherVane Than how do i raise a number to an exponent? –  Jan 12 '19 at 21:04
  • Multiply by the `base` for each digit. For each digit position the power is `base` times larger. – Weather Vane Jan 12 '19 at 21:04
  • the i variable represents which digit, 10's place, 100's place etc. is being used, and j represents what number is in the corresponding place –  Jan 12 '19 at 21:07
  • A typical conversion method is: for each digit from the l.s. multiply an accumulator by `base` and add the current digit. This avoids needing any power of the base that a particular digit represents. – Weather Vane Jan 12 '19 at 21:10
  • @WeatherVane what is an accumulator –  Jan 12 '19 at 21:12
  • An accumulator is somewhere you accumulate a value. A variable: the result. – Weather Vane Jan 12 '19 at 21:13
  • @WeatherVane got it. So in my case `Number` is the accumulator. Is that correct –  Jan 12 '19 at 21:14
  • Yes, that's right. You have `unsigned Number` but remeber that `strtol` you mention returns `long`. – Weather Vane Jan 12 '19 at 21:15
  • Is `strtoi` the same as `strtol` except it gives me an int instead of a long? –  Jan 12 '19 at 21:17
  • Ooops sorry, I meant start with the most significant digit. And I don't know of [`strtoi`](https://stackoverflow.com/questions/6181432/why-is-there-no-strtoi-in-stdlib-h). – Weather Vane Jan 12 '19 at 21:36
  • @WeatherVane I don't quite understand –  Jan 12 '19 at 22:01

1 Answers1

1

Ok, so I figured it out. The issue is indeed the string you pass to the function. When you write "-535" the string is allocated in the data section of the program and you are not allowed to write it. When the number is negative you try to modify that string by shifting the digits over the - sign. That's why it crashes on negative numbers only.

int main(int argc, const char *argv[]) {
    char c[200];
    strcpy(c, "-535");
    printf("asdf %x\n", StrToHex(c).Value);
}

This snippet works for me in the main function. You will never be able to pass constant strings to the function or pointers that refer to this kind of strings:

char c[200] = "-535";
StrToHex(c);

will also crash.

You must provide a memory location where you have write permissions.

Another workaround to your issue would be to not change the string to delete the - but write your code to just ignore it :)

Vlad Rusu
  • 1,414
  • 12
  • 17
  • 1
    Oh, so it is like you cannot write literal values, it is like doing `1 = 3` –  Jan 12 '19 at 21:10