Tony Cicero
Tony Cicero

Reputation: 75

Seg Fault / Use of uninitialised value of size 8

I understand that Seg faults are usually caused by invalid memory access, So I believe the issue might be with how I initialized the 2d dynamic arrays that are passed to the split function. However, I have spent hours looking over it and cant seem to find the issue.

Using valgrind I get the following:

==31912== Command: ./Project1 Data.txt
==31912== 
==31912== Use of uninitialised value of size 8
==31912==    at 0x4016C6: split(long**, long**, long**, long, long) (in /home/tony/Project1)
==31912==    by 0x400E5F: main (in /home/tony/Project1)
==31912== 
==31912== Invalid read of size 8
==31912==    at 0x4016C9: split(long**, long**, long**, long, long) (in /home/tony/Project1)
==31912==    by 0x400E5F: main (in /home/tony/Project1)
==31912==  Address 0x41d7894956415741 is not stack'd, malloc'd or (recently) free'd
==31912== 
==31912== 
==31912== Process terminating with default action of signal 11 (SIGSEGV)
==31912==  General Protection Fault
==31912==    at 0x4016C9: split(long**, long**, long**, long, long) (in /home/tony/Project1)
==31912==    by 0x400E5F: main (in /home/tony/Project1)
==31912== 
==31912== HEAP SUMMARY:
==31912==     in use at exit: 1,248 bytes in 81 blocks
==31912==   total heap usage: 89 allocs, 8 frees, 97,119 bytes allocated
==31912== 
==31912== LEAK SUMMARY:
==31912==    definitely lost: 304 bytes in 1 blocks
==31912==    indirectly lost: 304 bytes in 38 blocks
==31912==      possibly lost: 0 bytes in 0 blocks
==31912==    still reachable: 640 bytes in 42 blocks
==31912==         suppressed: 0 bytes in 0 blocks
==31912== Rerun with --leak-check=full to see details of leaked memory
==31912== 
==31912== For counts of detected and suppressed errors, rerun with: -v
==31912== Use --track-origins=yes to see where uninitialised values come from
==31912== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

The split function that valgrind seems to think is the culprit is:

void split(long** arr, long** arrL, long** arrR, long median, long size){   
//split data in half by x value -- takes in array sorted by x
    for (long i = 0; i < size -1; i++){
        if (i <= median){   //split data in half by x coord 
            arrL[i][0] = arr[i][0]; //x
            arrL[i][1] = arr[i][1]; //y
        }else{
            arrR[i][0] = arr[i][0];
            arrR[i][1] = arr[i][1];
        }
    }

}

in my main function, I initialize the arrays and then call split() like so:

        arrL = new long*[median + 1];//add one to make sure there is enough room
        arrR = new long*[median + 1];
        for(long i = 0; i < median + 1; i++){
            arrL[i] = new long[1];
            arrR[i] = new long[1];
        }


    split(arr, arrL, arrR, median, size);   //splits data

Any help would be greatly appreciated!

Upvotes: 0

Views: 720

Answers (2)

CraigR
CraigR

Reputation: 57

lMain issue is your arrL and arrR arrays are of size median, yet i will be bigger than median when writing to arrR.

Either resize arrL and arrR to contain size *long objects, or for arrR use i-median for the index as per following code

void split(long** arr, long** arrL, long** arrR, long median, long size);

int _tmain(int argc, _TCHAR* argv[])
{
    int size = 10;
    int median = size / 2;

    long **arr, **arrL, **arrR;
    arr = new long*[size];
    for(long i = 0; i < size; i++){
        arr[i] = new long[2];
        arr[i][0] = i;
        arr[i][1] = i + 10;
    }

    arrL = new long*[median + 1];//add one to make sure there is enough room
    arrR = new long*[median + 1];
    for(int i = 0; i < median + 1; i++){
        arrL[i] = new long[2];
        arrR[i] = new long[2];
        arrL[i][0] = -1;
        arrL[i][1] = -1;
        arrR[i][0] = -1;
        arrR[i][1] = -1;
    }

    split(arr, arrL, arrR, median, size);

    for(int i = 0; i < median + 1; i++)
        printf("arrL %d = %ld %ld\n", i, arrL[i][0], arrL[i][1]);

    for(int i = 0; i < median + 1; i++)
        printf("arrR %d = %ld %ld\n", i, arrR[i][0], arrR[i][1]);

    return 0;
}

void split(long** arr, long** arrL, long** arrR, long median, long size){   
//split data in half by x value -- takes in array sorted by x
    for (int i = 0; i < size; i++){
        if (i < median){   //split data in half by x coord 
            arrL[i][0] = arr[i][0]; //x
            arrL[i][1] = arr[i][1]; //y
        }else{
            arrR[i - median][0] = arr[i][0];
            arrR[i - median][1] = arr[i][1];
        }
    }
}

Upvotes: 0

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

Look carefully at this line (and similar ones).

arrL[i] = new long[1];

You allocated 1 item, thus the only valid index for arrL[i] is 0. Yet in your split function, you do this:

arrL[i][1] = arr[i][1]; //y

This accesses index 1, which is out of bounds.

You should allocate 2 items, not 1.

arrL[i] = new long[2];

Upvotes: 1

Related Questions