Help with this segmentation fault

I compile with no errors, then I run this and I get a segmentation fault. When I use gdb to try debugging, I get this:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x0000000100000000
0x0000000100000cc8 in isPal (Val=10201, Pow=4) at Palindrome.cc:53
53 S_Vals[D] = value;//Store each individual value found in the number inside the array
6: Pow = 4
4: power = 10000
3: Val = 10201
2: S_Vals = 0x100000000 "????\a"
1: value = 1
(gdb) print S_Vals
$1 = 0x100000000 "????\a"

Here is the code around where the program fails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char* S_Vals;
    char* B_Vals;
    int power;
    int value;
    int length = 0;
    for (int D = 0; Pow >= 0 && Val != 0; Pow--)
    {
        power = pows_n(10, Pow);
        if (Val/power == 0)//
        {
            S_Vals[D] = 0;//This means that we have encountered a zero
            D++;
            continue;
        }
        else
        {
            value = (Val/power);


   S_Vals[D] = value;//Store each individual value found in the number inside the array


            Val = Val - (S_Vals[D] * power);
            D++;
            
        }
        
    }

Help! I don't know what I am doing wrong
Last edited on
char* S_Vals;
This creates a char-pointer and it points... at some random memory somewhere.

S_Vals[D] = 0;
And now you try to set that random memory somewhere to zero. You are trying to write onto memory that is not yours. The operating system stops you with a segFault.


When you use a pointer, it is up to you to make sure it points at memory you are allowed to use. Here is what I usually say about pointers: http://cplusplus.com/articles/EN3hAqkS/

Thanks man, I finally understand pointers a bit more now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
bool isPal(int Val, int Pow)
{
    int Pow2 = Pow;
    char array1[Pow];
    char array2[Pow];
    char *ptr = array1;//A pointer to array1
    int power = 0;
    int value = 0;
    for ( ; Pow >= 0; Pow--) 
 {
      power = pows_n(10, Pow);
        if (Val/power == 0)//
        {
            *ptr = 0;//This means that we have encountered a zero
            //printf("array1= %d ", array1[D]);
            *ptr++;//Increment the position of the pointer by 1
            continue;
        }
        else
        {
            value = (Val/power);
            *ptr = value;//Change the value currently being pointed at by pointe
r to that contained in value
            Val = Val - (value * power);
            *ptr++;//Increment the position of the pointer by 1
            
        }
        
    }

    
    int p = 0;
    while(p <= Pow2)
    {
        *ptr--;
        array2[p] = *ptr;//Set the 2nd array's first value to be the value ptr i
s pointing at currently
        if (array2[p] != array1[p])
        {
            //if both pth value of both arrays do not match, return false and break out of the while loop
            return false;
            break;
        }
        p++;
    }
    return true;
}
Last edited on
Topic archived. No new replies allowed.