user3497276
user3497276

Reputation: 13

Segmentation Fault with structs and functions


I was having some difficulty in finding and solving a problem with the dreaded segmentation error. I have created a "struct" with an array and filled with with random characters. From there I am counting the horizontal and vertical pairs.
Everything seems fine until I run function3(). From there comes the segmentation fault. I ran GDB to find the error, but I do not know why it doesn't work since I have done a similar function for function2() and it is okay with that function. I'm not sure if I am missing a pointer or not. I've played around with adding and subtracting pointers with no luck.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 12
#define COL 15

typedef struct letter_array {
    char** letters;
    struct letter_array *ltr_ptr;
} larray;

void function1 (larray * letter1);
int function2 (larray  * letter2);
int function3 (larray * letter3);
void function4 (int hor_ans, int ver_ans);

int  main ( void )
{  

larray letter_list;
int vert, hori, count;

letter_list.letters = malloc(ROW*sizeof(int*));
for(count = 0; count<ROW; count++)
    {
    letter_list.letters [count] = malloc(COL*sizeof(int));
    }

printf("\n \t\t\t *** Hello! ***");

printf("\n This program will create a random selection of 180 upper-case"
    " characters. \n\n");

function1(&letter_list);

hori = function2(&letter_list);

vert = function3(&letter_list);  //The Problem?

free(letter_list.letters);

return ( 0 ) ;
}    


void function1 (larray *letter1)  // Assign random letters to array.
{
int i, z;

    srandom((unsigned)time(NULL));

for(i=0; i<ROW; i++)
 { 
    for(z=0; z<COL; z++)
    {
    letter1->letters[i][z] = random( )%26+'A';
    printf("%c ", letter1->letters[i][z]);
    }
 printf("\n");
 }

return ;
}

int function2 (larray * letter2)  //Count horizontal pairs.
{
int a,b;
int m=0;
    for(a=0; a<ROW; a++)
    {
       for(b=0; b<COL; b++)
       {
        if (letter2->letters[a][b] == (letter2->letters[a][b+1]))
        m++;
       }
    }

 return (m);
 }


 int function3 (larray * letter3)  //Count vertical pairs.
 {
 int a,b;
 int n=0;
    for (a=0; a<ROW; a++)
    {
       for(b=0; b<COL; b++)
       {
        if (letter3->letters[a][b] == (letter3->letters[a+1][b])) //THE Problem..?
        n++;
       }
    }
return (n);

In GDB...

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400ad8 in function3 (letter3=0x7fffffffd8a0)
xxx                 if (letter3->letters[a][b] == (letter3->letters[a+1][b]))
(gdb) backtrace
#0  0x0000000000400ad8 in function3 (letter3=0x7fffffffd8a0) 
#1  0x000000000040088f in main () 
(gdb) up
#1  0x000000000040088f in main () 
xxx      vert = function3(&letter_list);

Thank you for your help!

Upvotes: 0

Views: 949

Answers (1)

Floris
Floris

Reputation: 46365

It's pretty obvious. GDB tells you exactly where to look. In your function3 you do

for (a=0; a<ROW; a++)

and then you try to access

letter3->letters[a+1][b]

here, a+1 causes the segmentation fault (you run off the edge of your array).

Upvotes: 4

Related Questions