user2035045
user2035045

Reputation: 27

Cannot find bounds of Current function

I'm implementing an insertion sort on a 16384 array on C.

The sorting algorithm is functioning properly but when the free(inser) command is encountered, the debugger is outputting the Cannot find bounds of current function error.

Can this be caused because I'm using a 32-bit mingw on a 64-bit machine?

int main(void) {
    int i,t,j;
    int *inser = malloc(sizeof(int)*16384);
    int *shell = malloc(sizeof(int)*16384);
    srand(time(NULL));

    for(i=0;i<=16384;i++){
        *(inser+i) = rand()% 17000;;
        *(shell+i) = *(inser+i);
    }


    for(i=1;i<=16384;i++){
        j = i-1;
        while((*(inser+i)<*(inser+j)) && (j >=0)){
            t = *(inser+i);
            *(inser+i) = *(inser+j);
            *(inser+j) = t;
            j--;
            i--;
        }
    }

    for(i=0;i<=16384;i++){
        printf("%d\t",*(inser+i));
    }

    free(inser);
    free(shell);

    return 0;
}

Upvotes: 1

Views: 6777

Answers (6)

Back2Lobby
Back2Lobby

Reputation: 583

Make sure your debugger and compiler executable's paths are towards same MingW build

A bit Explanation:

I encountered the same error. I have already installed MingW package on my windows. As you know CodeBlocks also come with MingW in some packages. So, I had two MingW in My Windows. However, I noticed the path for the compiler installation directory (in Settings/Compiler/Toolchain Executables/) was set to different one and the debugger (in Settings/Debugger/Default -> Executable Path) was set to different MingW version. I changed them to same build that I installed before CodeBlocks explicitly. Now, it is working fine. There may be many reasons for an erorr/bug. It worked for me and I hope it will also help someone else...

Upvotes: 1

chux
chux

Reputation: 153303

Change the 3 for() loop terminations. As @Joachim Pileborg & @BLUEPIXY point out, the for() loops are executing once too often.

for (i = 0; i < 16384; i++) {
... 
for (i = 1; i < 16384; i++) {
...
for (i = 0; i < 16384; i++) {

Avoid naked magic numbers

#define ASIZE (16384)
int *inser = malloc(ASIZE * sizeof(int));
...
for (i = 0; i < ASIZE; i++) {

See @Emmet answer for indexing of -1 concern.

Upvotes: 0

Emmet
Emmet

Reputation: 6391

Apart from the loop bounds error, which has been pointed out by others, look at the line marked “WARNING”, below (I also cleaned up your code to make it more readable). On this line, when j starts off as zero, j becomes -1. This value will then be used here:

while( inser[i] < inser[j] && j >= 0 ) { ... }

The logical “and”, &&, is a shortcut operator: its left-hand side (LHS) is always evaluated, and the right-hand side is only evaluated if the LHS evaluates to “true”. So inser[-1] will always be evaluated after the last inner loop iteration because j has been decremented from 0 to -1 by j-- before the final loop test where j>=0 will fail, but not before inser[j] has been evaluated.

You could exchange the operands to && to avoid this problem, getting this:

while( j>=0 && inser[i] < inser[j] ) { ... }

Other than that, I can't say if your (corrected) code will behave as intended.

Turn on all compiler warnings and you might catch a few errors.

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

/* Avoid magic numbers */
#define ARRAY_SIZE   (16384)
#define RAND_CEILING (17000)

int main(void) {
    int i;          /* Indexing and iteration variable. */
    int j;          /* Indexing and iteration variable. */ 
    int t;          /* Temporary variable for swapping. */
    int *inser;     /* Sorted array.                    */
    int *shell;     /* Original array.                  */


    /* Always check the return value of malloc() */
    inser = malloc(ARRAY_SIZE*sizeof(*inser));
    if( inser == NULL ) {
        fprintf(stderr, "Call to malloc() failed for 'inser'.\n");
        exit( EXIT_FAILURE );
    }

    shell = malloc(ARRAY_SIZE*sizeof(*shell));
    if( shell == NULL ) {
        fprintf(stderr, "Call to malloc() failed for 'shell'.\n");
        exit( EXIT_FAILURE );
    }


    /* Seed the PRNG */
    srand(time(NULL));


    /* Correct the bounds on the iteration */
    for(i=0; i<ARRAY_SIZE; i++) {
        inser[i] = shell[i] = rand() % RAND_CEILING;
    }


    /* Sort 'inser' */
    for(i=1; i<ARRAY_SIZE; i++) {
        j = i-1;
        while( inser[i] < inser[j] && j >= 0 ) {
            t = inser[i];
            inser[i] = inser[j];
            inser[j] = t;
            j--;                       /* WARNING: 'j' becomes -1 here */
            i--;
        }
    }


    /* Dump 'inser' to stdout */
    for(i=0; i<ARRAY_SIZE; i++) {
        printf("%d\t", inser[i]);
    }


    /* Cleanup */
    free(inser);
    free(shell);

    return EXIT_SUCCESS;
}

Upvotes: 1

Austin
Austin

Reputation: 101

Your first for loop is going to run 16385 times, since it will run once for every number between (inclusive) 0 and 16384. Since your array has 16384 elements you will be overrunning it. This is what Joachim and BLUEPIXY are getting at. This bytes.com question explains why that causes the gdb error.

Upvotes: 0

Carl Martin
Carl Martin

Reputation: 26

Just allocate the buffers on the stack

int inser[16384];
int shell[16384];

The heap is evil son.

Upvotes: 0

Related Questions