1

I keep getting a segmentation fault, but I'm not sure what that means or how to tell what is causing it (I'm very new to programming and C). In this function, called by main.c, I need to determine the index of the smallest number in eacg row of a two-dimentional array.

Here is my code:

#include "my.h"

void findfirstsmall (int x, int y, int** a)
{
    int i;
    int j;
    int small;  

    small = y - 1;


printf("x = %3d, y = %3d\n", x, y);                      //trying to debug


    printf("f.  The first index of the smallest number is: \n");
    for(i = 0; i < x; i++)
        {
           for(j = 0; j < y; i++)          <---------- needs to be j, for any future readers
               {
                  if(a[i][small] > a[i][j])
                        small = j;
printf("small = %3d\n", small);                          //trying to debug
               }
           printf("Row: %4d, Index: %4d\n", i, small);
           small = y - 1;
           printf("\n");
        }
    printf("\n");
    return;
}

It prints correctly for the first row, but not the second. This is my array:

56 7 25 89 4
-23 -56 2 99 -12

This is what I am getting when I run the program:

x =   2, y =   5 
f.  The first index of the smallest number is:  
small =   4  small =   0 
Segmentation fault

This is in C. Thanks in advance for the help!

Piseagan
  • 521
  • 3
  • 6
  • 12
  • 2
    Segmentation Faults generally occur because you are accessing memory incorrectly. So for instance, here it is probably happening because your logic for reading the array is causing the program to read outside of the 'a' array. Could you include your definitions in 'main.c' as well? – loganfsmyth Nov 05 '11 at 02:18
  • Yes, show us your code for the parameters you pass and how you determine them, especially the array. Also, you can use a debugger to step through your code one line at a time to see where it crashes. – Seth Carnegie Nov 05 '11 at 02:19
  • Is `a` meant to be `int **`, not `int *` plus a row stride? – ta.speot.is Nov 05 '11 at 02:25
  • 1
    You should read [this question](http://stackoverflow.com/questions/2346806/what-is-segmentation-fault) for more information on what a segmentation fault is – Wipqozn Nov 05 '11 at 02:27
  • 1
    Post also how you allocate `a`. The problem could be that it is not properly `malloc`ed – Shahbaz Nov 05 '11 at 03:00
  • @Piseagan, generally please only ask questions of the form "what is a ...", if you don't get satisfying answers using search engine. Also `SO` has this nice "search" field on the top right. – Jens Gustedt Nov 05 '11 at 08:52

4 Answers4

5

Fix the typo:

       for(j = 0; j < y; j++)
                         ^^
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
3

A segmentation error means that you are accessing memory that you do not own.

As a instant guess without looking at your code - it is an off-by-one-error. Remember C arrays are zero based.

I will look at your code more soon.

Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
3
printf("f.  The first index of the smallest number is: \n");
for(i = 0; i < x; i++)
    {
       for(j = 0; j < y; i++) // my guess is that you increment "i" instead of "j"
           {
Mr_Hic-up
  • 389
  • 2
  • 11
0

Note that there is a difference between a two-dimensional array and an array of pointers (see this question). Depending on what you're doing in main(), this could be your problem. For example, the following would not work with the function as is, since it passes a pointer to memory containing an array of arrays:

int arr[2][5] = { {  56,   7,  25,  89,   4 },
                  { -23, -56,   2,  99, -12 } };
findfirstsmall (2, 5, arr);

However, this would be ok, since it passes an array of pointers to the start of each sub-array of arr:

int arr[2][5] = { {  56,   7,  25,  89,   4 },
                  { -23, -56,   2,  99, -12 } };
int *tmp[2];
tmp[0] = &arr[0][0];
tmp[1] = &arr[1][0];
findfirstsmall (2, 5, tmp);
Community
  • 1
  • 1
Dmitri
  • 9,175
  • 2
  • 27
  • 34