Use of uninitialized value of size 4

This is a discussion on Use of uninitialized value of size 4 within the C Programming forums, part of the General Programming Boards category; Im pretty new to C, and am looking for an explanation of why I am getting this error message. Code: ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Use of uninitialized value of size 4

    Im pretty new to C, and am looking for an explanation of why I am getting this error message.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct node {
       int first;
       struct node *rest;
    };
    
    struct node *cons(int fst, struct node *rst) {
       struct node *new = malloc(sizeof(struct node));
       new->first = fst;
       new->rest = rst;
       return new;
    }
    
    
    struct node *insert(struct node *lst1, int n) {
       struct node *temp;
       temp->first = n;
       temp->rest = lst1;
       return temp;
    }
    
    
    int main(void) {
       struct node *a = cons(2,cons(3,cons(6,NULL)));
       struct node *c = insert(a, 10);
    }

    This is the valgrind message I get:

    Code:
    = Use of uninitialised value of size 4
    =    at 0x80483FB: insert (test.c:19)
    =    by 0x804845F: main (test.c:27)
    =  Uninitialised value was created by a stack allocation
    =    at 0x80483F2: insert (test.c:17)
    = Process terminating with default action of signal 11 (SIGSEGV)
    =  Bad permissions for mapped region at address 0x8049FF4
    =    at 0x80483FB: insert (test.c:19)
    =    by 0x804845F: main (test.c:27)
    I know this is a simple problem but I just don't understand what is going on!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In 'insert':
    Code:
    struct node *temp;
       temp->first = n;
    'temp' doesn't point at anything. I.e.: You are using it uninitialized.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    how would I make it point to something? Like does it have to point at a specific type?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JimmyRP View Post
    how would I make it point to something? Like does it have to point at a specific type?
    You need to allocate memory for the structure...

    temp is of type struct node so you need to use
    Code:
    struct node *temp = malloc(sizeof(node));
    Last edited by CommonTater; 03-07-2011 at 10:31 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by JimmyRP View Post
    how would I make it point to something? Like does it have to point at a specific type?
    How is 'insert' any different than 'cons'? You need to have an idea of what the function is supposed to be doing before you start making it try to do something.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of uninitialised value of size 4
    By dot_pro in forum C Programming
    Replies: 3
    Last Post: 10-26-2010, 05:32 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  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