seg fault, cant find the error - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

seg fault, cant find the error

sufs_LNDsufs_LND Posts: 23Member
This is going to convert an ip written in hex. Wrote it just for fun, g++ gives me a segmentation fault. Anyone? :)

EDIT: It seems that the line
octet[i] = itoa(iptabel[i],buffer,10);
gives me the error.. Why?

[code]
#include
#include
#include
#include
using namespace std;

// itoa function for linux only, uncomment for use
// char* itoa( int value, char* result, int base );
int main ()
{
string hex;
cin >> hex;

int hexconverted[7][2]; // Creates array to store the converted hex in

for (int i = 0; i <= 7; i++) { // Runs convert prosess 8 times
if (hex[i] == 'a') {
hexconverted[i][2] = 10;
}
else if (hex[i] == 'b') {
hexconverted[i][2] = 11;
}
else if (hex[i] == 'c') {
hexconverted[i][2] = 12;
}
else if (hex[i] == 'd') {
hexconverted[i][2] = 13;
}
else if (hex[i] == 'e') {
hexconverted[i][2] = 14;
}
else if (hex[i] == 'f') {
hexconverted[i][2] = 15;
}
else {
int cache = (int)hex[i];
hexconverted[i][2] = cache - 48; // removes 48 bytes for some reason..
cache = 0;
}
}

int iptabel[3]; // array to store the octets of decoded hex
string octet[3]; // 4 arrays for 4 octets of the IP (for some reason I had to create 5 arrays, or else I get a segmentation fault, look into..)
string ip; // string ip, this value is to be returned to the main program

int a = 0;
int b = 1;

for (int i = 0; i <= 3; i++) { // loops 4 times
iptabel[i] = (hexconverted[a][2]*16) + hexconverted[b][2];// Multiplies the first number of the octet with 16 and then adds the second number, stores in array
a++; a++;
b++; b++;

char buffer[100]; // creates bufffer for itoa
octet[i] = itoa(iptabel[i],buffer,10); // converts from int to string and stores in array

ip += octet[i]; // loads the 4 octets from the octet array and puts into the same string..
if (i != 3) ip += "."; // ... and adds a . after each octet (not the last)
}

cout << ip;
}
// Following function is for linux only
/*
char* itoa( int value, char* result, int base ) {

if (base < 2 || base > 16) { *result = 0; return result; }

char* out = result;

int quotient = value;

do {

*out = "0123456789abcdef"[ std::abs( quotient % base ) ];

++out;

quotient /= base;

} while ( quotient );

if ( value < 0 && base == 10) *out++ = '-';

std::reverse( result, out );

*out = 0;

return result;

}
*/
[/code]

Comments

  • sufs_LNDsufs_LND Posts: 23Member
    OK, I think I got it. But I don't understand it. Anyone care to explain?

    [code]
    #include
    #include
    #include
    #include
    #include
    using namespace std;

    // itoa function for linux only, uncomment for use
    // char* itoa( int value, char* result, int base );
    int main ()
    {
    string hex;
    cin >> hex;

    int hexconverted[9][2]; // Creates array to store the converted hex in

    for (int i = 0; i <= 7; i++) { // Runs convert prosess 8 times
    if (hex[i] == 'a') {
    hexconverted[i][2] = 10;
    }
    else if (hex[i] == 'b') {
    hexconverted[i][2] = 11;
    }
    else if (hex[i] == 'c') {
    hexconverted[i][2] = 12;
    }
    else if (hex[i] == 'd') {
    hexconverted[i][2] = 13;
    }
    else if (hex[i] == 'e') {
    hexconverted[i][2] = 14;
    }
    else if (hex[i] == 'f') {
    hexconverted[i][2] = 15;
    }
    else {
    int cache = (int)hex[i];
    hexconverted[i][2] = cache - 48; // removes 48 bytes for some reason..
    cache = 0;
    }
    }

    int iptabel[4]; // array to store the octets of decoded hex
    string octet[4]; // 4 arrays for 4 octets of the IP (for some reason I had to create 5 arrays, or else I get a segmentation fault, look into..)
    string ip; // string ip, this value is to be returned to the main program

    int a = 0;
    int b = 1;

    for (int i = 0; i <= 3; i++) { // loops 4 times
    iptabel[i] = (hexconverted[a][2]*16) + hexconverted[b][2]; // Multiplies the first number of the octet with 16 and then adds the second number, stores in array
    a++; a++;
    b++; b++;

    std::ostringstream ss;
    ss << iptabel[i];
    octet[i] = ss.str(); // converts from int to string and stores in array

    ip += octet[i]; // loads the 4 octets from the octet array and puts into the same string..
    if (i != 3) ip += "."; // ... and adds a . after each octet (not the last)
    }

    cout << ip;
    }
    [/code]
  • LundinLundin Posts: 3,711Member
    I didn't read the code too careful, but I think you either had some problem with the algorithm causing iptable[i] to be a number larger than two digits, or perhaps you missed that strings need to be null-terminated in C and therefore character arrays need one additional byte of space.

    Segmentation fault almost always means you are accessing an array out-of-bounds.
Sign In or Register to comment.