Use of uninitialised value of size 4

This is a discussion on Use of uninitialised value of size 4 within the C Programming forums, part of the General Programming Boards category; Hey everybody I am new here and I need your help. I have a problem with a C program I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    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:
    ==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 ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take a close look at the value of "j" when you first enter the do loop on line 11.

    Far as I know, most C arrays start 0.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Oops!! I feel a really bad programmer right now
    And actually I had few more mistakes.
    The corrected version is:
    Code:
    void insertionSort(int number, int *array[number]) 
    {
      for (int i = 1; i < number; 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
    Thanks for your help!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    My pleasure.... And don't feel bad, you should see some of the horrific mistakes I've made

    The compiler I use bails at 100 errors with the message: "More than 100 errors. Please improve yourself" and I've seen it way too many times.
    Last edited by CommonTater; 10-26-2010 at 05:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 07:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21