Thread: use of uninitialized value of size 4.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    use of uninitialized value of size 4.

    Hi, I keep getting this error message for my code even though I initialized it already.

    Code:
    = Use of uninitialised value of size 4
    =    at 0x80484D9: irest_destroy (ilist_destructive.c:55)
    =    by 0x804865B: iprint (ilist_destructive.c:115)
    =    by 0x80486F7: main (ilist_destructive.c:128)
    =  Uninitialised value was created by a stack allocation
    =    at 0x8048679: main (ilist_destructive.c:122)
    = Use of uninitialised value of size 4
    =    at 0x80484F4: irest_destroy (ilist_destructive.c:58)
    =    by 0x804865B: iprint (ilist_destructive.c:115)
    =    by 0x80486F7: main (ilist_destructive.c:128)
    =  Uninitialised value was created by a stack allocation
    =    at 0x8048679: main (ilist_destructive.c:122)
    = Invalid read of size 4
    =    at 0x80484D9: irest_destroy (ilist_destructive.c:55)
    =    by 0x804865B: iprint (ilist_destructive.c:115)
    =    by 0x80486F7: main (ilist_destructive.c:128)
    =  Address 0x4 is not stack'd, malloc'd or (recently) free'd
    = Process terminating with default action of signal 11 (SIGSEGV)
    =  Access not within mapped region at address 0x4
    =    at 0x80484D9: irest_destroy (ilist_destructive.c:55)
    =    by 0x804865B: iprint (ilist_destructive.c:115)
    =    by 0x80486F7: main (ilist_destructive.c:128)
    =  If you believe this happened as a result of a stack
    =  overflow in your program's main thread (unlikely but
    =  possible), you can try to increase the size of the
    =  main thread stack using the --main-stacksize= flag.
    =  The main thread stack size used in this run was 16777216.
    why is it doing that???!?

    following is my code. this code should start at line 46.
    Code:
    ilist irest_destroy(ilist il){
       // modifies il to remove the first element, and returns the modified ilist
       // frees the memory associated with the first element
       // references to il cease to be valid ilists
       // the result (if non-empty) must eventually be consumed by one of:
       //     icons_destroy, irest_destroy, idelete
       if (il == NULL) return NULL;
       ilist r = il->rest;
       ilist head = malloc(sizeof(struct ilist_ADT));
       head->first = r->first;
       head->rest = NULL;
       ilist l_end = head;
       for (ilist a = r->rest; a != NULL; a=a->rest){
       ilist tmp = malloc(sizeof(struct ilist_ADT));
       tmp->first = a->first;
       tmp->rest=NULL;
       l_end->rest = tmp;
       l_end=tmp;
       }
       free(il);
       return head;
    }
    this is the structure definition used in the above code:
    Code:
    struct ilist_ADT{
      struct ilist_ADT *rest;
      int first;   
    };
    
    typedef ilist_ADT *ilist;
    Last edited by Sucy White; 06-24-2012 at 03:47 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It might help if you showed the exact error message and pointed out the line that the line number refers to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    please refresh. the position of my code should start at line 46.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    struct ilist_ADT{
      struct ilist_ADT *rest;
      int first;   
    };
    
    typedef ilist_ADT *ilist;
    Is this a typo? The last line should be
    Code:
    typedef struct ilist_ADT *ilist;

    Code:
       ilist r = il->rest;
       ilist head = malloc(sizeof(struct ilist_ADT));
       head->first = r->first;
    Have you run the code through a debugger, so you can tell when the segfault comes up? Could it be that there is only one element left in "il" and "il->rest" points to NULL?

    BTW if you want to remove the first element of the list (as your comments suggest), you don't need 5 pointers and 15 lines.

    Bye, Andreas

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > = Address 0x4 is not stack'd, malloc'd or (recently) free'd
    Yes, your pointer is NULL, and the member offset inside your struct is 4 bytes.

    Which looking at your struct, corresponds exactly to "int first"

    > = Invalid read of size 4
    > = at 0x80484D9: irest_destroy (ilist_destructive.c:55)
    > = by 0x804865B: iprint (ilist_destructive.c:115)
    > = by 0x80486F7: main (ilist_destructive.c:128)
    Start with line 55 of ilist_destructive.c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of uninitialized value of size 4
    By JimmyRP in forum C Programming
    Replies: 4
    Last Post: 03-08-2011, 12:01 AM
  2. Uninitialized value of char *
    By joelem in forum C Programming
    Replies: 6
    Last Post: 07-17-2009, 10:04 AM
  3. Uninitialized variables
    By Rife in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2009, 12:55 PM
  4. uninitialized var in list
    By @nthony in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2008, 12:09 PM
  5. How To Chk Uninitialized Pointer
    By u_peerless in forum C Programming
    Replies: 11
    Last Post: 06-19-2008, 10:11 AM

Tags for this Thread