Use of uninitialised value of size 4
Hey everybody I am new here and I need your help.
I have a problem with a C program I am trying to implement.
I am new to this programming language and I believe the answer is so simple but I just cannot notice what goes wrong.
(Instead of the actual program I am giving a test one which containts the parts I get the error).
Code:
#include <stdio.h>
#include <stdlib.h>
void insertionSort(int number, int *array[number])
{
for (int i = 0; i < (number - 1); i++) {
int value = *array[i];
int done = 0;
int j = i - 1;
do {
if ((*array[j]) > value) {
*array[j+1] = *array[j];
j--;
if (j > 0) done = 1;
} // if
else done = 1;
} while (!done);
*array[j + 1] = value;
} // for
} // insertionSort
int main (int argc, char ** argv)
{
int num = 6;
int *array[6];
for (int i = 0; i < 6; i++) {
array[i] = ( int * ) malloc(sizeof(int));
}
*array[0] = 50;
*array[1] = 80;
*array[2] = 1000;
*array[3] = -6;
*array[4] = 4;
*array[5] = 8;
insertionSort(num, array);
for (int i = 0; i < 6; i++) {
printf("%d\n", *array[i]);
}
}
This program compiles correctly but then I get a Segmentation fault.
Running valgrind I get the following:
Quote:
==21634== Memcheck, a memory error detector.
==21634== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==21634== Using LibVEX rev 1884, a library for dynamic binary translation.
==21634== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==21634== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==21634== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==21634== For more details, rerun with: -v
==21634==
==21634== Use of uninitialised value of size 4
==21634== at 0x8048431: insertionSort (testTest.c:11)
==21634== by 0x804852B: main (testTest.c:37)
==21634==
==21634== Use of uninitialised value of size 4
==21634== at 0x8048451: insertionSort (testTest.c:12)
==21634== by 0x804852B: main (testTest.c:37)
==21634==
==21634== Use of uninitialised value of size 4
==21634== at 0x8048453: insertionSort (testTest.c:12)
==21634== by 0x804852B: main (testTest.c:37)
==21634==
==21634== Use of uninitialised value of size 4
==21634== at 0x8048489: insertionSort (testTest.c:18)
==21634== by 0x804852B: main (testTest.c:37)
==21634==
==21634== Invalid read of size 4
==21634== at 0x8048411: insertionSort (testTest.c:7)
==21634== by 0x804852B: main (testTest.c:37)
==21634== Address 0x50 is not stack'd, malloc'd or (recently) free'd
==21634==
==21634== Process terminating with default action of signal 11 (SIGSEGV)
==21634== Access not within mapped region at address 0x50
==21634== at 0x8048411: insertionSort (testTest.c:7)
==21634== by 0x804852B: main (testTest.c:37)
==21634== If you believe this happened as a result of a stack overflow in your
==21634== program's main thread (unlikely but possible), you can try to increase
==21634== the size of the main thread stack using the --main-stacksize= flag.
==21634== The main thread stack size used in this run was 10485760.
==21634==
==21634== ERROR SUMMARY: 13 errors from 5 contexts (suppressed: 12 from 1)
==21634== malloc/free: in use at exit: 24 bytes in 6 blocks.
==21634== malloc/free: 6 allocs, 0 frees, 24 bytes allocated.
==21634== For counts of detected errors, rerun with: -v
==21634== Use --track-origins=yes to see where uninitialised values come from
==21634== searching for pointers to 6 not-freed blocks.
==21634== checked 58,792 bytes.
==21634==
==21634== LEAK SUMMARY:
==21634== definitely lost: 0 bytes in 0 blocks.
==21634== possibly lost: 0 bytes in 0 blocks.
==21634== still reachable: 24 bytes in 6 blocks.
==21634== suppressed: 0 bytes in 0 blocks.
==21634== Rerun with --leak-check=full to see details of leaked memory.
Segmentation fault
I cannot understand what value is uninitialized.
I know that I make things complicated but it is importan to use an array of pointers to int instead of just an array of ints.
Any ideas ?