简体   繁体   中英

Getting a C++ segmentation fault

I'm in a linux server and when I try to execute the program it's returning a segmentation fault. when i use gdb to try and find out why, it returns..

Starting program: /home/cups/k

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401128 in search(int) ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64 libgcc-4.4.7-17.el6.x86_64 libstdc++-4.4.7-17.el6.x86_64

I couldn't quite interpret this. In my program i have a function called "search()" but i don't see anything that would cause a seg fault. here's the function def:

int search (int bit_type) {                                               // SEARCH FOR A CONSEC NUMBER (of type BIT_TYPE) TO SEE IF ALREADY ENCOUNTERED

    for (int i = 1; i <= MAX[bit_type]; i++) {               //GO THRU ALL ENCOUNTERED CONSEC NUMBERS SO FAR (for type BIT_TYPE)
        if (consec == r[bit_type][i])                           // IF: FOUND
        return i;                                                                       //                      -----> RETURN INDEX OF RECORDED CONSEC_NUM
    }
    // IF: NOT FOUND
    r[bit_type][++MAX[bit_type]] = consec;                          //                      -----> INCREMENT MAX[bit_type]  &  RECORD NEW CONSEC_NUM -------> ARRAY[MAX]
    n[bit_type][MAX[bit_type]] = 1;
    return (MAX[bit_prev]);                                                          //                      -----> RETURN THE NEWLY FILLED INDEX 
}

global functions:

int MAX[2];
int r[2][200];
int n[2][200];

The comments are pretty useless to you guys since you don't have the rest of the program.. but you can just ignore them.

But do you guys see anything I missed?

From the link to your code here , here is just one error:

 int *tmp = new int[MAX[0]];
 for (int y = 0; y <= MAX[0]; y++) {
     tmp[y] = 1;
}

You are going out-of-bounds on the last iteration. You allocated an array with MAX[0] items, and on the last iteration you're accessing tmp[MAX[0]] .

That loop should be:

 int *tmp = new int[MAX[0]];
 for (int y = 0; y < MAX[0]; y++) {
     tmp[y] = 1;
}

or better yet:

 #include <algorithm>
    //...
    std::fill(tmp, tmp + MAX[0], 1);  // no loop needed

or skip the dynamic allocation using new[] and use std::vector :

  #include <vector>
  //...
  std::vector<int> tmp(MAX[0], 1);

In general, you have multiple loops that do this:

for (int i = 1; i <= number_of_items_in_array; ++i )

and then you access your arrays with array[i] . It is the <= in that for loop condition that is suspicious since it will try to access the array with an out-of-bounds index on the last iteration.

Another example is this:

long sum(int arr_r[], int arr_n[], int limit)
{
    long tot = 0;
    for (int i = 1; i <= limit; i++)
    {
        tot += (arr_r[i])*(arr_n[i]);
    }
    return tot;
}

Here, limit is the number of elements in the array, and you access arr_r[i] on the last iteration, causing undefined behavior.

Arrays are indexed starting from 0 and up to n - 1 , where n is the total number of elements. Trying to fake 1-based arrays as you're attempting to do almost always results in these types of errors somewhere inside of the code base.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM